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

node,timers: fix beforeExit with unrefed timers #2795

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3961,18 +3961,26 @@ static void StartNodeInstance(void* arg) {
{
SealHandleScope seal(isolate);
bool more;
uv_loop_t* event_loop = env->event_loop();

do {
v8::platform::PumpMessageLoop(default_platform, isolate);
more = uv_run(env->event_loop(), UV_RUN_ONCE);
more = uv_run(event_loop, UV_RUN_ONCE);

if (more == false) {
v8::platform::PumpMessageLoop(default_platform, isolate);
EmitBeforeExit(env);

// If there are only unrefed handles left, we need to run an
// extra event loop turn to purge the unrefed handles.
if (event_loop->active_handles == 0 &&
event_loop->handle_queue[0] != event_loop->handle_queue[1])
uv_run(event_loop, UV_RUN_NOWAIT);

// Emit `beforeExit` if the loop became alive either after emitting
// event, or after running some callbacks.
more = uv_loop_alive(env->event_loop());
if (uv_run(env->event_loop(), UV_RUN_NOWAIT) != 0)
more = uv_loop_alive(event_loop);
if (uv_run(event_loop, UV_RUN_NOWAIT) != 0)
more = true;
}
} while (more == true);
Expand Down
20 changes: 10 additions & 10 deletions test/parallel/test-beforeexit-event.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
var assert = require('assert');
var net = require('net');
var fs = require('fs');
var util = require('util');
var common = require('../common');
var revivals = 0;
Expand All @@ -23,18 +23,18 @@ function tryTimer() {
setTimeout(function() {
console.log('timeout cb, do another once beforeExit');
revivals++;
process.once('beforeExit', tryListen);
process.once('beforeExit', tryFile);
}, 1);
}

function tryListen() {
console.log('create a server');
net.createServer()
.listen(common.PORT)
.on('listening', function() {
revivals++;
this.close();
});
function tryFile() {
console.log('open a file');
fs.open(__filename, 'r', (err, fd) => {
if (err) throw err;

revivals++;
fs.closeSync(fd);
});
}

process.on('exit', function() {
Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-timers-unrefed-in-beforeexit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

require('../common');
const assert = require('assert');

var once = 0;

process.on('beforeExit', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use common.mustCall(..., 1), you don't need the once variable at all.

if (once > 1)
throw new RangeError('beforeExit should only have been called once!');

setTimeout(() => {}, 1).unref();
once++;
});

process.on('exit', (code) => {
if (code !== 0) return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why ignoring if the process fails?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not. But if the process has already failed we want that error, not the assertion.


assert.strictEqual(once, 1);
});