From 313f9030d202be013c44157fcb785beb05425db6 Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Mon, 18 Nov 2024 13:43:23 -0600 Subject: [PATCH] fix: `utilityProcess` pid should be `undefined` after exit (#44693) fix: utilityProcess pid should be undefined after exit Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Shelley Vohr --- docs/api/utility-process.md | 4 ++++ .../api/electron_api_utility_process.cc | 2 ++ spec/api-utility-process-spec.ts | 20 ++++++++++++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/docs/api/utility-process.md b/docs/api/utility-process.md index 1cb6bfc12304..67c876ff1ffa 100644 --- a/docs/api/utility-process.md +++ b/docs/api/utility-process.md @@ -92,6 +92,8 @@ the child process exits, then the value is `undefined` after the `exit` event is ```js const child = utilityProcess.fork(path.join(__dirname, 'test.js')) +console.log(child.pid) // undefined + child.on('spawn', () => { console.log(child.pid) // Integer }) @@ -101,6 +103,8 @@ child.on('exit', () => { }) ``` +**Note:** You can use the `pid` to determine if the process is currently running. + #### `child.stdout` A `NodeJS.ReadableStream | null` that represents the child process's stdout. diff --git a/shell/browser/api/electron_api_utility_process.cc b/shell/browser/api/electron_api_utility_process.cc index cb3ef43018d1..dcb8b4515abb 100644 --- a/shell/browser/api/electron_api_utility_process.cc +++ b/shell/browser/api/electron_api_utility_process.cc @@ -254,6 +254,8 @@ void UtilityProcessWrapper::HandleTermination(uint64_t exit_code) { if (pid_ != base::kNullProcessId) GetAllUtilityProcessWrappers().Remove(pid_); + + pid_ = base::kNullProcessId; CloseConnectorPort(); EmitWithoutEvent("exit", exit_code); Unpin(); diff --git a/spec/api-utility-process-spec.ts b/spec/api-utility-process-spec.ts index 2765fdc059fb..87938286d514 100644 --- a/spec/api-utility-process-spec.ts +++ b/spec/api-utility-process-spec.ts @@ -210,13 +210,31 @@ describe('utilityProcess module', () => { it('is valid when child process launches successfully', async () => { const child = utilityProcess.fork(path.join(fixturesPath, 'empty.js')); await once(child, 'spawn'); - expect(child.pid).to.not.be.null(); + expect(child).to.have.property('pid').that.is.a('number'); }); it('is undefined when child process fails to launch', async () => { const child = utilityProcess.fork(path.join(fixturesPath, 'does-not-exist.js')); expect(child.pid).to.be.undefined(); }); + + it('is undefined before the child process is spawned succesfully', async () => { + const child = utilityProcess.fork(path.join(fixturesPath, 'empty.js')); + expect(child.pid).to.be.undefined(); + await once(child, 'spawn'); + child.kill(); + }); + + it('is undefined when child process is killed', async () => { + const child = utilityProcess.fork(path.join(fixturesPath, 'empty.js')); + await once(child, 'spawn'); + + expect(child).to.have.property('pid').that.is.a('number'); + expect(child.kill()).to.be.true(); + + await once(child, 'exit'); + expect(child.pid).to.be.undefined(); + }); }); describe('stdout property', () => {