node: Fix race condition with accessing stdioOptions #16670
Merged
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.
What does this PR do?
At Anthropic, we are using Bun for an internal CLI-based project that makes fairly significant use of node's
child_process
module. Intermittently across both macOS and Linux, we would see a crash inexec
/spawn
with a stack similar to:We believe that this is a race condition / ordering issue:
But at this point, this.#handle hasn't been set yet - it's set right after accessing stdio. So if the process exits very quickly, the onExit callback could run before the handle is properly set up, leading to this error.
This is particularly likely when:
This change ensures that:
This should prevent the race condition where stdio is accessed before the handle is set, which was causing the TypeError: undefined is not an object (evaluating 'this.#stdioOptions') error
How did you verify your code works?
Because this is a fairly tight race condition, it is difficult to verify outside of certain machines. We have a coworker who for whatever reason would hit this issue every time, and this patch fixes it. The full release test suite was also run locally and child_process tests all pass.