Skip to content

Commit 6def8b2

Browse files
committed
Make sure to handle double uWS.free
1 parent d98516e commit 6def8b2

2 files changed

Lines changed: 19 additions & 19 deletions

File tree

‎Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Windows:
1+
default:
22
$(CC) build.c
33
./a.out || build.exe

‎src/addon.cpp‎

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,28 +47,28 @@ bool BooleanValue(Isolate *isolate, Local<Value> value) {
4747

4848
/* This has to be called in beforeExit, but exit also seems okay */
4949
void uWS_free(const FunctionCallbackInfo<Value> &args) {
50-
51-
/* We get the External holding perContextData */
52-
PerContextData *perContextData = (PerContextData *) Local<External>::Cast(args.Data())->Value();
50+
/* Holder is exports */
51+
Local<Object> exports = args.Holder();
5352

54-
/* Todo: this will always be true */
55-
if (perContextData) {
56-
/* Freeing apps here, it could be done earlier but not sooner */
57-
perContextData->apps.clear();
58-
perContextData->sslApps.clear();
59-
/* Freeing the loop here means we give time for our timers to close, etc */
60-
uWS::Loop::get()->free();
53+
/* See if we even have free anymore */
54+
if (exports->Get(String::NewFromUtf8(args.GetIsolate(), "free", NewStringType::kNormal).ToLocalChecked()) == Undefined(args.GetIsolate())) {
55+
return;
56+
}
6157

58+
/* Once we free uWS we remove the uWS.free function from exports */
59+
exports->Set(args.GetIsolate()->GetCurrentContext(), String::NewFromUtf8(args.GetIsolate(), "free", NewStringType::kNormal).ToLocalChecked(), Undefined(args.GetIsolate()));
6260

63-
// we need to mark this
64-
delete perContextData;
61+
/* We get the External holding perContextData */
62+
PerContextData *perContextData = (PerContextData *) Local<External>::Cast(args.Data())->Value();
6563

66-
// we can override the exports->free function to null after!
64+
/* Freeing apps here, it could be done earlier but not sooner */
65+
perContextData->apps.clear();
66+
perContextData->sslApps.clear();
67+
/* Freeing the loop here means we give time for our timers to close, etc */
68+
uWS::Loop::get()->free();
6769

68-
//args.Data()
69-
70-
//Local<External>::Cast(args.Data())->
71-
}
70+
/* We can safely delete this since we no longer can call uWS.free */
71+
delete perContextData;
7272
}
7373

7474
/* todo: Put this function and all inits of it in its own header */
@@ -83,7 +83,7 @@ void Main(Local<Object> exports) {
8383
Isolate *isolate = exports->GetIsolate();
8484

8585
/* We want this so that we can redefine process.nextTick to using the V8 native microtask queue */
86-
// todo: setting this might be crashing nodejs?
86+
/* Settings this crashes Node.js while debugging with breakpoints */
8787
isolate->SetMicrotasksPolicy(MicrotasksPolicy::kAuto);
8888

8989
/* Init the template objects, SSL and non-SSL, store it in per context data */

0 commit comments

Comments
 (0)