Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use Node's microtasks policy in node_main.cc #23324

Merged
merged 1 commit into from Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions shell/app/node_main.cc
Expand Up @@ -78,6 +78,7 @@ int NodeMain(int argc, char* argv[]) {

// Initialize gin::IsolateHolder.
JavascriptEnvironment gin_env(loop);
gin_env.isolate()->SetMicrotasksPolicy(v8::MicrotasksPolicy::kExplicit);

node::IsolateData* isolate_data =
node::CreateIsolateData(gin_env.isolate(), loop, gin_env.platform());
Expand Down
23 changes: 23 additions & 0 deletions 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;
}
});
13 changes: 13 additions & 0 deletions spec/node-spec.js
Expand Up @@ -690,4 +690,17 @@ describe('node feature', () => {
const result = ChildProcess.spawnSync(remote.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();
});
});
});