fix: improve telemetry worker stderr cleanup#3648
Merged
Conversation
Replace setImmediate + destroy() with process.stderr.end() callback to ensure all buffered writes are flushed before exiting. Add fallback exit in finally block to prevent worker from hanging if end() fails. This properly releases the inherited stderr file descriptor in debug mode, preventing the parent process from being blocked.
Only call process.stderr.end() when DEBUG env var is set, mirroring the parent process logic where stderr is inherited only in DEBUG mode. When stderr is ignored, closing it is unnecessary and could cause issues.
k80bowman
approved these changes
Apr 7, 2026
Contributor
k80bowman
left a comment
There was a problem hiding this comment.
Thanks for continuing to iterate on this!
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.
Summary
Improved the telemetry worker's exit logic to properly flush stderr before exiting. The previous implementation used
setImmediate()+process.stderr.destroy(), which did not guarantee that buffered writes were flushed, potentially causing debug messages to be lost and the parent process to hang when stderr is inherited in DEBUG mode.The new implementation:
process.stderr.end()with a callback to ensure all buffered data is flushed before exitsetImmediate()in a finally block to prevent the worker from hanging ifend()failsType of Change
Patch Updates (patch semver update)
Testing
Notes:
This change affects the telemetry worker process that runs in the background when DEBUG mode is enabled. The issue would manifest as the parent CLI process hanging or debug messages being truncated.
Steps:
DEBUG=heroku:analyticsto verify telemetry worker exits cleanly