fix: use Node's microtasks policy in node_main.cc (#23153)
Fixes #21515.
This commit is contained in:
parent
3ada079fe3
commit
07654c47ec
3 changed files with 40 additions and 0 deletions
|
@ -146,6 +146,10 @@ int NodeMain(int argc, char* argv[]) {
|
||||||
JavascriptEnvironment gin_env(loop);
|
JavascriptEnvironment gin_env(loop);
|
||||||
|
|
||||||
v8::Isolate* isolate = gin_env.isolate();
|
v8::Isolate* isolate = gin_env.isolate();
|
||||||
|
// TODO(ckerr) and/or TODO(codebytere) use node::SetIsolateMiscHandlers()
|
||||||
|
node::IsolateSettings is;
|
||||||
|
isolate->SetMicrotasksPolicy(is.policy);
|
||||||
|
|
||||||
v8::Isolate::Scope isolate_scope(isolate);
|
v8::Isolate::Scope isolate_scope(isolate);
|
||||||
v8::Locker locker(isolate);
|
v8::Locker locker(isolate);
|
||||||
node::Environment* env = nullptr;
|
node::Environment* env = nullptr;
|
||||||
|
|
|
@ -307,4 +307,17 @@ describe('node feature', () => {
|
||||||
const result = childProcess.spawnSync(process.execPath, [path.resolve(fixtures, 'api', 'electron-main-module', 'app.asar')]);
|
const result = childProcess.spawnSync(process.execPath, [path.resolve(fixtures, 'api', 'electron-main-module', 'app.asar')]);
|
||||||
expect(result.status).to.equal(0);
|
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();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
23
spec/fixtures/module/node-promise-timer.js
vendored
Normal file
23
spec/fixtures/module/node-promise-timer.js
vendored
Normal file
|
@ -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;
|
||||||
|
}
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue