[fix][issue 1098] check batchBuilder in case batch is disabled #1099
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #1098
Motivation
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0x8a8eaa]
goroutine 1 [running]:
github.com/apache/pulsar-client-go/pulsar.(*partitionProducer).internalFlushCurrentBatch(0xc00c6ccc60)
/go/pkg/mod/github.com/apache/[email protected]/pulsar/producer_partition.go:869 +0x2a
github.com/apache/pulsar-client-go/pulsar.(*partitionProducer).runEventsLoop(0xc00c6ccc60)
/go/pkg/mod/github.com/apache/[email protected]/pulsar/producer_partition.go:457 +0x1d7
created by github.com/apache/pulsar-client-go/pulsar.newPartitionProducer in goroutine 5713
/go/pkg/mod/github.com/apache/[email protected]/pulsar/producer_partition.go:203 +0x9bb
when the producer option set to DisableBatching: true, the time.ticker wil be created and close after a while, when ticker.Stop() over 10ms (default value) since created it will cause the ticker still tick, and cause nil pointer dereference eventually.
Modifications
Add one more check to evaluate if
batchBuilder
is nil, in non-batch scenario, it should return immediate.To understand the behaviour of ticker, write a small test code of ticker that ticker.C (the timer) sometime is called while the Stop() is called. That's why it's good to check the batchBuilder. If the batchBuilder object fails to be created, it should be caught at line 294 at producer_partition.go. So if batch is enabled, it should not be nil. Therefore we can use the nil check to stop any non-batch calls to proceed with the flush.
Verifying this change
This change is a trivial rework / code cleanup without any test coverage.
Does this pull request potentially affect one of the following parts:
If
yes
was chosen, please highlight the changesDocumentation