Skip to content

refactor(controller): encapsulate context cancellation logic#1051

Merged
christos68k merged 4 commits into
open-telemetry:mainfrom
rogercoll:move_ctx_cancelation_responsablity
Jan 14, 2026
Merged

refactor(controller): encapsulate context cancellation logic#1051
christos68k merged 4 commits into
open-telemetry:mainfrom
rogercoll:move_ctx_cancelation_responsablity

Conversation

@rogercoll
Copy link
Copy Markdown
Contributor

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 Start context (or SIGTERM cancellation).

@rogercoll rogercoll requested review from a team as code owners January 8, 2026 17:22
Comment thread internal/controller/controller.go Outdated
Comment thread internal/controller/controller.go Outdated
Comment thread internal/controller/controller.go Outdated
rogercoll and others added 3 commits January 9, 2026 09:09
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>
intervals := times.New(c.config.ReporterInterval, c.config.MonitorInterval,
c.config.ProbabilisticInterval)

ctx, c.cancelFunc = context.WithCancel(ctx)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid shadowing conflicts, the new context should be named differently to the context that is given as argument.

Copy link
Copy Markdown
Contributor Author

@rogercoll rogercoll Jan 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@rogercoll rogercoll requested a review from florianl January 13, 2026 16:57
@christos68k christos68k merged commit d325cd4 into open-telemetry:main Jan 14, 2026
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants