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 2 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
1 change: 1 addition & 0 deletions src/handle_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class HandleWrap : public AsyncWrap {
private:
friend class Environment;
friend void GetActiveHandles(const v8::FunctionCallbackInfo<v8::Value>&);
friend bool HasUnrefedTimerHandles(Environment* env);
static void OnClose(uv_handle_t* handle);
ListNode<HandleWrap> handle_wrap_queue_;
unsigned int flags_;
Expand Down
17 changes: 17 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,18 @@ void GetActiveHandles(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(ary);
}

bool HasUnrefedTimerHandles(Environment* env) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: more consistent style to have two lines between functions.


Copy link
Contributor

Choose a reason for hiding this comment

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

nit: remove empty line.

HandleScope handle_scope(env->isolate());
Copy link
Contributor

Choose a reason for hiding this comment

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

The HandleScope can be dropped. No objects are being created.


for (auto w : *env->handle_wrap_queue()) {
if (!w->persistent().IsEmpty() && w->flags_ & HandleWrap::kUnref)
return true;
}

return false;
}


static void Abort(const FunctionCallbackInfo<Value>& args) {
ABORT();
Expand Down Expand Up @@ -3969,6 +3981,11 @@ static void StartNodeInstance(void* arg) {
v8::platform::PumpMessageLoop(default_platform, isolate);
EmitBeforeExit(env);

// We need to run an extra event loop turn to purge unrefed handles.
bool unrefedTimers = HasUnrefedTimerHandles(env);
if (unrefedTimers == true)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@trevnorris would it better for me to inline the HasUnrefedTimerHandles(env)?

Copy link
Contributor

Choose a reason for hiding this comment

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

naw. looks good as it is.

uv_run(env->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());
Expand Down
17 changes: 17 additions & 0 deletions test/parallel/test-timers-unrefed-in-beforeexit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'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.

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);
});