diff --git a/src/addon.cpp b/src/addon.cpp index 204086e..36d3239 100644 --- a/src/addon.cpp +++ b/src/addon.cpp @@ -145,32 +145,6 @@ void uWS_cfg(const FunctionCallbackInfo &args) { } } -/* This has to be called in beforeExit, but exit also seems okay */ -void uWS_free(const FunctionCallbackInfo &args) { - /* Holder is exports */ - Local exports = args.Holder(); - - /* See if we even have free anymore */ - if (exports->Get(args.GetIsolate()->GetCurrentContext(), String::NewFromUtf8(args.GetIsolate(), "free", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked() == Undefined(args.GetIsolate())) { - return; - } - - /* Once we free uWS we remove the uWS.free function from exports */ - exports->Set(args.GetIsolate()->GetCurrentContext(), String::NewFromUtf8(args.GetIsolate(), "free", NewStringType::kNormal).ToLocalChecked(), Undefined(args.GetIsolate())).ToChecked(); - - /* We get the External holding perContextData */ - PerContextData *perContextData = (PerContextData *) Local::Cast(args.Data())->Value(); - - /* Freeing apps here, it could be done earlier but not sooner */ - perContextData->apps.clear(); - perContextData->sslApps.clear(); - /* Freeing the loop here means we give time for our timers to close, etc */ - uWS::Loop::get()->free(); - - /* We can safely delete this since we no longer can call uWS.free */ - delete perContextData; -} - /* todo: Put this function and all inits of it in its own header */ void uWS_us_listen_socket_close(const FunctionCallbackInfo &args) { // this should take int ssl first @@ -374,7 +348,7 @@ void uWS_unlock(const FunctionCallbackInfo &args) { kvMutex.unlock(); } -void Main(Local exports) { +PerContextData *Main(Local exports) { /* We only care if it is defined, not what it says */ experimental_fastcall = getenv("EXPERIMENTAL_FASTCALL") != nullptr; @@ -403,7 +377,6 @@ void Main(Local exports) { /* uWS namespace */ exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "App", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_App, externalPerContextData)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked(); exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "SSLApp", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_App, externalPerContextData)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked(); - exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "free", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_free, externalPerContextData)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked(); /* Temporary KV store */ exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "getString", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_getString)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked(); @@ -441,6 +414,8 @@ void Main(Local exports) { /* Listen options */ exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "LIBUS_LISTEN_EXCLUSIVE_PORT", NewStringType::kNormal).ToLocalChecked(), Integer::NewFromUnsigned(isolate, LIBUS_LISTEN_EXCLUSIVE_PORT)).ToChecked(); + + return perContextData; } /* This is required when building as a Node.js addon */ @@ -451,6 +426,22 @@ NODE_MODULE_INITIALIZER(Local exports, Local module, LocalGetIsolate())); /* Register vanilla V8 addon */ - Main(exports); + PerContextData *perContextData = Main(exports); + + /* We cannot rely on process.exit or process.beforeExit when it comes to WorkerThreads */ + node::AddEnvironmentCleanupHook(context->GetIsolate(), [](void *arg) { + + PerContextData *perContextData = (PerContextData *) arg; + + /* Freeing apps here, it could be done earlier but not sooner */ + perContextData->apps.clear(); + perContextData->sslApps.clear(); + /* Freeing the loop here means we give time for our timers to close, etc */ + uWS::Loop::get()->free(); + + /* We can safely delete this since we no longer can call uWS.free */ + delete perContextData; + + }, perContextData); } #endif diff --git a/src/uws.js b/src/uws.js index 81c20b7..079baea 100644 --- a/src/uws.js +++ b/src/uws.js @@ -1,5 +1,5 @@ /* - * Authored by Alex Hultman, 2018-2019. + * Authored by Alex Hultman, 2018-2020. * Intellectual property of third-party. * Licensed under the Apache License, Version 2.0 (the "License"); @@ -25,7 +25,6 @@ module.exports = (() => { }); }; } - process.on('exit', uWS.free); return uWS; } catch (e) { throw new Error('This version of µWS is not compatible with your Node.js build:\n\n' + e.toString());