[receiver/kafkareceiver] fix: Kafka receiver blocking shutdown #35767
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.
Description
Fixes an issue where the Kafka receiver would block on shutdown.
There was an earlier fix for this issue here. This does solve the issue, but it was only applied to the traces receiver, not the logs or metrics receiver.
The issue is this go routine in the
Start()
functions for logs and metrics:The
consumeLoop()
function returns acontext.Canceled
error whenShutdown()
is called, which is expected. Howevercomponentstatus.ReportStatus()
blocks while attempting to report this error. The reason/bug for this can be found here.The previously mentioned PR fixed this for the traces receiver by checking if the error returned by
consumeLoop()
iscontext.Canceled
:Additionally, this is
consumeLoop()
for the traces receiver, with the logs and metrics versions being identical:This does fix the issue, however the only error that can be returned by
consumeLoop()
is a canceled context. When we create the context and cancel function, we usecontext.Background()
:This context is only used by
consumeLoop()
and the cancel function is only called inShutdown()
.Because
consumeLoop()
can only return acontext.Canceled
error, this PR removes this unused code for the logs, metrics, and traces receivers. Instead,consumeLoop()
still logs thecontext.Canceled
error but it does not return any error and the go routine simply just callsconsumeLoop()
.Additional motivation for removing the call to
componentstatus.ReportStatus()
is the underlying function called by it,componentstatus.Report()
says it does not need to be called duringShutdown()
orStart()
as the service already does so for the given component, comment here. Even if there wasn't a bug causing this call to block, the component still shouldn't call it since it would only be called duringShutdown()
.Link to tracking issue
Fixes #30789
Testing
Tested in a build of the collector with these changes scraping logs from a Kafka instance. When the collector is stopped and
Shutdown()
gets called, the receiver did not block and the collector stopped gracefully as expected.