Skip to content

Commit 07654c4

Browse files
authored
fix: use Node's microtasks policy in node_main.cc (electron#23153)
Fixes electron#21515.
1 parent 3ada079 commit 07654c4

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

shell/app/node_main.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ int NodeMain(int argc, char* argv[]) {
146146
JavascriptEnvironment gin_env(loop);
147147

148148
v8::Isolate* isolate = gin_env.isolate();
149+
// TODO(ckerr) and/or TODO(codebytere) use node::SetIsolateMiscHandlers()
150+
node::IsolateSettings is;
151+
isolate->SetMicrotasksPolicy(is.policy);
152+
149153
v8::Isolate::Scope isolate_scope(isolate);
150154
v8::Locker locker(isolate);
151155
node::Environment* env = nullptr;

spec-main/node-spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,4 +307,17 @@ describe('node feature', () => {
307307
const result = childProcess.spawnSync(process.execPath, [path.resolve(fixtures, 'api', 'electron-main-module', 'app.asar')]);
308308
expect(result.status).to.equal(0);
309309
});
310+
311+
it('handles Promise timeouts correctly', (done) => {
312+
const scriptPath = path.join(fixtures, 'module', 'node-promise-timer.js');
313+
const child = childProcess.spawn(process.execPath, [scriptPath], {
314+
env: { ELECTRON_RUN_AS_NODE: 'true' }
315+
});
316+
emittedOnce(child, 'exit').then(([code, signal]) => {
317+
expect(code).to.equal(0);
318+
expect(signal).to.equal(null);
319+
child.kill();
320+
done();
321+
});
322+
});
310323
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const waitMs = (msec) => new Promise((resolve) => setTimeout(resolve, msec));
2+
3+
const intervalMsec = 100;
4+
const numIterations = 2;
5+
let curIteration = 0;
6+
let promise;
7+
8+
for (let i = 0; i < numIterations; i++) {
9+
promise = (promise || waitMs(intervalMsec)).then(() => {
10+
++curIteration;
11+
return waitMs(intervalMsec);
12+
});
13+
}
14+
15+
// https://github.com/electron/electron/issues/21515 was about electron
16+
// exiting before promises finished. This test sets the pending exitCode
17+
// to failure, then resets it to success only if all promises finish.
18+
process.exitCode = 1;
19+
promise.then(() => {
20+
if (curIteration === numIterations) {
21+
process.exitCode = 0;
22+
}
23+
});

0 commit comments

Comments
 (0)