diff --git a/shell/app/node_main.cc b/shell/app/node_main.cc index 873be8b7c8379..09cff8e53ab40 100644 --- a/shell/app/node_main.cc +++ b/shell/app/node_main.cc @@ -89,6 +89,7 @@ int NodeMain(int argc, char* argv[]) { JavascriptEnvironment gin_env(loop); v8::Isolate* isolate = gin_env.isolate(); + isolate->SetMicrotasksPolicy(v8::MicrotasksPolicy::kExplicit); node::IsolateData* isolate_data = node::CreateIsolateData(isolate, loop, gin_env.platform()); diff --git a/spec-main/node-spec.ts b/spec-main/node-spec.ts index f21b4a2367670..cd1fd6b7fba71 100644 --- a/spec-main/node-spec.ts +++ b/spec-main/node-spec.ts @@ -260,4 +260,17 @@ describe('node feature', () => { const result = childProcess.spawnSync(process.execPath, [path.resolve(fixtures, 'api', 'electron-main-module', 'app.asar')]); expect(result.status).to.equal(0); }); + + it('handles Promise timeouts correctly', (done) => { + const scriptPath = path.join(fixtures, 'module', 'node-promise-timer.js'); + const child = childProcess.spawn(process.execPath, [scriptPath], { + env: { ELECTRON_RUN_AS_NODE: 'true' } + }); + emittedOnce(child, 'exit').then(([code, signal]) => { + expect(code).to.equal(0); + expect(signal).to.equal(null); + child.kill(); + done(); + }); + }); }); diff --git a/spec/fixtures/module/node-promise-timer.js b/spec/fixtures/module/node-promise-timer.js new file mode 100644 index 0000000000000..b21c24f5dfc36 --- /dev/null +++ b/spec/fixtures/module/node-promise-timer.js @@ -0,0 +1,23 @@ +const waitMs = (msec) => new Promise((resolve) => setTimeout(resolve, msec)); + +const intervalMsec = 100; +const numIterations = 2; +let curIteration = 0; +let promise; + +for (let i = 0; i < numIterations; i++) { + promise = (promise || waitMs(intervalMsec)).then(() => { + ++curIteration; + return waitMs(intervalMsec); + }); +} + +// https://github.com/electron/electron/issues/21515 was about electron +// exiting before promises finished. This test sets the pending exitCode +// to failure, then resets it to success only if all promises finish. +process.exitCode = 1; +promise.then(() => { + if (curIteration === numIterations) { + process.exitCode = 0; + } +});