Fix recursive nextTick calls, drain nextTickQueue before exit
This commit is contained in:
parent
914c6f2bc3
commit
9f0aae77b1
@ -24,6 +24,12 @@
|
|||||||
using namespace v8;
|
using namespace v8;
|
||||||
|
|
||||||
/* These two are definitely static */
|
/* These two are definitely static */
|
||||||
|
|
||||||
|
/* Warning: having nextTickQueue items at loop fallthrough is not allowed.
|
||||||
|
* Process will crash/hang due to destruction of V8 resouces after V8 itself
|
||||||
|
* has been destroyed. Either enforce nextTick calls to keep the loop rolling
|
||||||
|
* via for instance setTimeout or setImmediate, or make sure to drain completely
|
||||||
|
* the queue at process.on('beforeExit'). */
|
||||||
std::vector<UniquePersistent<Function>> nextTickQueue;
|
std::vector<UniquePersistent<Function>> nextTickQueue;
|
||||||
Isolate *isolate;
|
Isolate *isolate;
|
||||||
|
|
||||||
@ -38,17 +44,32 @@ void nextTick(const FunctionCallbackInfo<Value> &args) {
|
|||||||
nextTickQueue.emplace_back(UniquePersistent<Function>(isolate, Local<Function>::Cast(args[0])));
|
nextTickQueue.emplace_back(UniquePersistent<Function>(isolate, Local<Function>::Cast(args[0])));
|
||||||
}
|
}
|
||||||
|
|
||||||
void emptyNextTickQueue(Isolate *isolate) {
|
/* Used for debugging */
|
||||||
|
void print(const FunctionCallbackInfo<Value> &args) {
|
||||||
|
NativeString nativeString(isolate, args[0]);
|
||||||
|
std::cout << nativeString.getString() << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Does not guarantee empty queue because of recursive nextTick calls.
|
||||||
|
* Should return int queueSize after calling queued items, so that
|
||||||
|
* proper while(processNextTickQueueImpl()) can be done */
|
||||||
|
int processNextTickQueueImpl(Isolate *isolate) {
|
||||||
if (nextTickQueue.size()) {
|
if (nextTickQueue.size()) {
|
||||||
|
/* Swap queues for recursive calls */
|
||||||
|
std::vector<UniquePersistent<Function>> currentNextTickQueue = std::move(nextTickQueue);
|
||||||
|
|
||||||
HandleScope hs(isolate);
|
HandleScope hs(isolate);
|
||||||
|
for (UniquePersistent<Function> &f : currentNextTickQueue) {
|
||||||
for (UniquePersistent<Function> &f : nextTickQueue) {
|
|
||||||
Local<Function>::New(isolate, f)->Call(isolate->GetCurrentContext()->Global(), 0, nullptr);
|
Local<Function>::New(isolate, f)->Call(isolate->GetCurrentContext()->Global(), 0, nullptr);
|
||||||
f.Reset();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
nextTickQueue.clear();
|
return nextTickQueue.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* It is possible to call this at process.beforeExit until it returns 0. */
|
||||||
|
void processNextTickQueue(const FunctionCallbackInfo<Value> &args) {
|
||||||
|
args.GetReturnValue().Set(Integer::New(isolate, processNextTickQueueImpl(isolate)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* todo: Put this function and all inits of it in its own header */
|
/* todo: Put this function and all inits of it in its own header */
|
||||||
@ -62,12 +83,12 @@ void Main(Local<Object> exports) {
|
|||||||
|
|
||||||
/* Register our own nextTick handlers */
|
/* Register our own nextTick handlers */
|
||||||
uWS::Loop::defaultLoop()->setPostHandler([](uWS::Loop *) {
|
uWS::Loop::defaultLoop()->setPostHandler([](uWS::Loop *) {
|
||||||
emptyNextTickQueue(isolate);
|
processNextTickQueueImpl(isolate);
|
||||||
});
|
});
|
||||||
|
|
||||||
/* We also do need it on pre */
|
/* We also do need it on pre */
|
||||||
uWS::Loop::defaultLoop()->setPreHandler([](uWS::Loop *) {
|
uWS::Loop::defaultLoop()->setPreHandler([](uWS::Loop *) {
|
||||||
emptyNextTickQueue(isolate);
|
processNextTickQueueImpl(isolate);
|
||||||
});
|
});
|
||||||
|
|
||||||
/* Hook up our timers */
|
/* Hook up our timers */
|
||||||
@ -77,6 +98,8 @@ void Main(Local<Object> exports) {
|
|||||||
exports->Set(String::NewFromUtf8(isolate, "App"), FunctionTemplate::New(isolate, uWS_App<uWS::App>)->GetFunction());
|
exports->Set(String::NewFromUtf8(isolate, "App"), FunctionTemplate::New(isolate, uWS_App<uWS::App>)->GetFunction());
|
||||||
exports->Set(String::NewFromUtf8(isolate, "SSLApp"), FunctionTemplate::New(isolate, uWS_App<uWS::SSLApp>)->GetFunction());
|
exports->Set(String::NewFromUtf8(isolate, "SSLApp"), FunctionTemplate::New(isolate, uWS_App<uWS::SSLApp>)->GetFunction());
|
||||||
exports->Set(String::NewFromUtf8(isolate, "nextTick"), FunctionTemplate::New(isolate, nextTick)->GetFunction());
|
exports->Set(String::NewFromUtf8(isolate, "nextTick"), FunctionTemplate::New(isolate, nextTick)->GetFunction());
|
||||||
|
exports->Set(String::NewFromUtf8(isolate, "processNextTickQueue"), FunctionTemplate::New(isolate, processNextTickQueue)->GetFunction());
|
||||||
|
exports->Set(String::NewFromUtf8(isolate, "print"), FunctionTemplate::New(isolate, print)->GetFunction());
|
||||||
|
|
||||||
/* Expose some µSockets functions directly under uWS namespace */
|
/* Expose some µSockets functions directly under uWS namespace */
|
||||||
exports->Set(String::NewFromUtf8(isolate, "us_listen_socket_close"), FunctionTemplate::New(isolate, uWS_us_listen_socket_close)->GetFunction());
|
exports->Set(String::NewFromUtf8(isolate, "us_listen_socket_close"), FunctionTemplate::New(isolate, uWS_us_listen_socket_close)->GetFunction());
|
||||||
|
16
src/uws.js
16
src/uws.js
@ -18,8 +18,20 @@
|
|||||||
module.exports = (() => {
|
module.exports = (() => {
|
||||||
try {
|
try {
|
||||||
const uWS = require(`./uws_${process.platform}_${process.versions.modules}.node`);
|
const uWS = require(`./uws_${process.platform}_${process.versions.modules}.node`);
|
||||||
/* We are not compatible with Node.js domain */
|
/* We are not compatible with Node.js nextTick and/or domains */
|
||||||
process.nextTick = (f, ...args) => uWS.nextTick(() => f(...args));
|
process.nextTick = (f, ...args) => {
|
||||||
|
uWS.nextTick(() => {
|
||||||
|
f(...args);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
process.on('beforeExit', () => {
|
||||||
|
if (uWS.processNextTickQueue()) {
|
||||||
|
setImmediate(() => {
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
/* process.nextTick = setImmediate; */
|
||||||
return uWS;
|
return uWS;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw new Error('This version of µWS is not compatible with your Node.js build.\n\n' + e.toString());
|
throw new Error('This version of µWS is not compatible with your Node.js build.\n\n' + e.toString());
|
||||||
|
Loading…
Reference in New Issue
Block a user