diff options
author | 2022-12-04 08:32:51 -0800 | |
---|---|---|
committer | 2022-12-04 08:32:51 -0800 | |
commit | 10cd5aaa1504d178079e64dd15c97122fb2e1c53 (patch) | |
tree | 1671907b21703ff57cddd781d6a46f84a84772e4 | |
parent | f4668e45f855d7eb95ea2dd995a5a75faff03618 (diff) | |
download | bun-10cd5aaa1504d178079e64dd15c97122fb2e1c53.tar.gz bun-10cd5aaa1504d178079e64dd15c97122fb2e1c53.tar.zst bun-10cd5aaa1504d178079e64dd15c97122fb2e1c53.zip |
Fix race condition in child_process
-rw-r--r-- | src/bun.js/child_process.exports.js | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/src/bun.js/child_process.exports.js b/src/bun.js/child_process.exports.js index fdeec31f8..70a0f4f01 100644 --- a/src/bun.js/child_process.exports.js +++ b/src/bun.js/child_process.exports.js @@ -867,7 +867,6 @@ function checkExecSyncError(ret, args, cmd) { //------------------------------------------------------------------------------ export class ChildProcess extends EventEmitter { #handle; - #handleExited; #exited = false; #closesNeeded = 1; #closesGot = 0; @@ -1061,28 +1060,37 @@ export class ChildProcess extends EventEmitter { const bunStdio = getBunStdioFromOptions(stdio); var env = options.envPairs || undefined; - if (env === process.env) env = undefined; + this.#encoding = options.encoding || undefined; + this.#stdioOptions = bunStdio; + var hasEmittedSpawn = false; this.#handle = Bun.spawn({ cmd: spawnargs, stdin: bunStdio[0], stdout: bunStdio[1], stderr: bunStdio[2], cwd: options.cwd || undefined, - env, + env: env || process.env, onExit: (handle, exitCode, signalCode, err) => { this.#handle = handle; - this.#handleOnExit(exitCode, signalCode, err); + this.pid = this.#handle.pid; + + if (!hasEmittedSpawn) { + hasEmittedSpawn = true; + process.nextTick(onSpawnNT, this); + process.nextTick(this.#handleOnExit, exitCode, signalCode, err); + } else { + this.#handleOnExit(exitCode, signalCode, err); + } }, lazy: true, }); - - this.#handleExited = this.#handle.exited; - this.#encoding = options.encoding || undefined; - this.#stdioOptions = bunStdio; this.pid = this.#handle.pid; - process.nextTick(onSpawnNT, this); + if (!hasEmittedSpawn) { + process.nextTick(onSpawnNT, this); + hasEmittedSpawn = true; + } // const ipc = stdio.ipc; // const ipcFd = stdio.ipcFd; |