refactor(controller): encapsulate context cancellation logic#1051
Merged
christos68k merged 4 commits intoJan 14, 2026
Merged
Conversation
christos68k
approved these changes
Jan 8, 2026
Co-authored-by: Christos Kalkanis <christos.kalkanis@elastic.co>
Co-authored-by: Christos Kalkanis <christos.kalkanis@elastic.co>
Co-authored-by: Christos Kalkanis <christos.kalkanis@elastic.co>
florianl
reviewed
Jan 9, 2026
| intervals := times.New(c.config.ReporterInterval, c.config.MonitorInterval, | ||
| c.config.ProbabilisticInterval) | ||
|
|
||
| ctx, c.cancelFunc = context.WithCancel(ctx) |
Member
There was a problem hiding this comment.
To avoid shadowing conflicts, the new context should be named differently to the context that is given as argument.
Contributor
Author
There was a problem hiding this comment.
In this case we intentionally want to override the parent context and prevent its use further down the function. I think that reusing ctx here aligns with Google best practices (“It’s OK to do this when the original value is no longer needed”). Since the derived context fully replaces the parent, keeping the same name makes that intent clear.
Nonetheless, I don't have a strong opinion. I can name it differently if that feels clearer or easier to follow.
florianl
approved these changes
Jan 14, 2026
This was referenced Jan 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
When the Controller is used as a library (e.g., within the OpenTelemetry Collector), the context passed to Start() is often long-lived and not cancelled when the component is shut down. Previously, this caused background tasks, such as startTraceHandling and trace handling loops, to leak or persist after Shutdown() was called.
This change moves the creation of a cancellable context inside Controller.Start(). The Controller now manages its own lifecycle using an internal CancelFunc.
Differently than the profiler's main, the collectors start/shutdown workflow does not explicitly cancel the
Startcontext (or SIGTERM cancellation).