Skip to content

Commit a43675c

Browse files
committed
Clean exit, remove forcefully_free
1 parent 319eea9 commit a43675c

7 files changed

Lines changed: 33 additions & 38 deletions

File tree

‎examples/GracefulShutdown.js‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,3 @@ const app = uWS./*SSL*/App({
3737
console.log('Failed to listen to port ' + port);
3838
}
3939
});
40-
41-
/* This is questionable */
42-
process.on('beforeExit', () => {
43-
console.log('Forcefully freeing app now');
44-
app.forcefully_free();
45-
});

‎src/AppWrapper.h‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,6 @@ void uWS_App_listen(const FunctionCallbackInfo<Value> &args) {
206206
args.GetReturnValue().Set(args.Holder());
207207
}
208208

209-
/* Mostly indended for debugging memory leaks */
210-
template <typename APP>
211-
void uWS_App_forcefully_free(const FunctionCallbackInfo<Value> &args) {
212-
APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0);
213-
214-
delete app;
215-
}
216-
217209
template <typename APP>
218210
void uWS_App(const FunctionCallbackInfo<Value> &args) {
219211
Local<FunctionTemplate> appTemplate = FunctionTemplate::New(isolate);
@@ -336,11 +328,15 @@ void uWS_App(const FunctionCallbackInfo<Value> &args) {
336328
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "ws"), FunctionTemplate::New(isolate, uWS_App_ws<APP>));
337329
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "listen"), FunctionTemplate::New(isolate, uWS_App_listen<APP>));
338330

339-
/* forcefully_free is unsafe for end-users to use, but nice to track memory leaks with ASAN */
340-
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "forcefully_free"), FunctionTemplate::New(isolate, uWS_App_forcefully_free<APP>));
341-
342331
Local<Object> localApp = appTemplate->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
343332
localApp->SetAlignedPointerInInternalField(0, app);
344333

334+
/* Add this to our delete list */
335+
if constexpr (std::is_same<APP, uWS::SSLApp>::value) {
336+
sslApps.emplace_back(app);
337+
} else {
338+
apps.emplace_back(app);
339+
}
340+
345341
args.GetReturnValue().Set(localApp);
346342
}

‎src/addon.cpp‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,30 @@ using namespace v8;
2525

2626
/* These two are definitely static */
2727
Isolate *isolate;
28+
bool valid = true;
29+
30+
/* We hold all apps until free */
31+
std::vector<std::unique_ptr<uWS::App>> apps;
32+
std::vector<std::unique_ptr<uWS::SSLApp>> sslApps;
2833

2934
#include "Utilities.h"
3035
#include "WebSocketWrapper.h"
3136
#include "HttpResponseWrapper.h"
3237
#include "HttpRequestWrapper.h"
3338
#include "AppWrapper.h"
3439

35-
/* Used for debugging */
36-
void print(const FunctionCallbackInfo<Value> &args) {
37-
NativeString nativeString(isolate, args[0]);
38-
std::cout << nativeString.getString() << std::endl;
40+
/* Todo: Apps should be freed once the GC says so BUT ALWAYS before freeing the loop */
41+
42+
/* This has to be called in beforeExit, but exit also seems okay */
43+
void uWS_free(const FunctionCallbackInfo<Value> &args) {
44+
if (valid) {
45+
/* Freeing apps here, it could be done earlier but not sooner */
46+
apps.clear();
47+
sslApps.clear();
48+
/* Freeing the loop here means we give time for our timers to close, etc */
49+
uWS::Loop::get()->free();
50+
valid = false;
51+
}
3952
}
4053

4154
/* todo: Put this function and all inits of it in its own header */
@@ -50,13 +63,13 @@ void Main(Local<Object> exports) {
5063
/* We want this so that we can redefine process.nextTick to using the V8 native microtask queue */
5164
isolate->SetMicrotasksPolicy(MicrotasksPolicy::kAuto);
5265

53-
/* Hook up our timers */
54-
us_loop_integrate((us_loop *) uWS::Loop::defaultLoop());
66+
/* Integrate with existing libuv loop, we just pass a boolean basically */
67+
uWS::Loop::get((void *) 1);
5568

5669
/* uWS namespace */
5770
exports->Set(String::NewFromUtf8(isolate, "App"), FunctionTemplate::New(isolate, uWS_App<uWS::App>)->GetFunction());
5871
exports->Set(String::NewFromUtf8(isolate, "SSLApp"), FunctionTemplate::New(isolate, uWS_App<uWS::SSLApp>)->GetFunction());
59-
exports->Set(String::NewFromUtf8(isolate, "print"), FunctionTemplate::New(isolate, print)->GetFunction());
72+
exports->Set(String::NewFromUtf8(isolate, "free"), FunctionTemplate::New(isolate, uWS_free)->GetFunction());
6073

6174
/* Expose some µSockets functions directly under uWS namespace */
6275
exports->Set(String::NewFromUtf8(isolate, "us_listen_socket_close"), FunctionTemplate::New(isolate, uWS_us_listen_socket_close)->GetFunction());

‎src/uws.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ module.exports = (() => {
2424
f(...args);
2525
});
2626
};
27+
/* You are not allowed to use the lib past here */
28+
process.on('exit', () => {
29+
uWS.free();
30+
});
31+
2732
return uWS;
2833
} catch (e) {
2934
throw new Error('This version of µWS is not compatible with your Node.js build.\n\n' + e.toString());

‎tests/Autobahn.js‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,3 @@ listenWithSettings({
7171
ssl: false,
7272
compression: uWS.DEDICATED_COMPRESSOR
7373
});
74-
75-
/* This is required to check for memory leaks */
76-
process.on('exit', () => {
77-
apps.forEach((a) => {
78-
a.app.forcefully_free();
79-
});
80-
});

‎tests/Hammer.js‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,3 @@ const app = uWS./*SSL*/App({
201201
listenSocket = token;
202202
}
203203
});
204-
205-
/* Yes we do this crazy thing here */
206-
process.on('beforeExit', () => {
207-
uWS.print('Exiting now');
208-
app.forcefully_free();
209-
});

0 commit comments

Comments
 (0)