Skip to content

Commit 5f75991

Browse files
committed
Work around ObjectWrap destruction bug
Code from nodejs/node-addon-api#475
1 parent 61158b9 commit 5f75991

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/disruptor.cc

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ class Disruptor : public Napi::ObjectWrap<Disruptor>
134134
Napi::Value GetPendingSeqNext(const Napi::CallbackInfo& info);
135135
Napi::Value GetPendingSeqNextEnd(const Napi::CallbackInfo& info);
136136
Napi::Value GetElementSize(const Napi::CallbackInfo& info);
137+
138+
void ThrowErrnoError(const Napi::CallbackInfo& info,
139+
const char *msg,
140+
bool constructing = false);
137141
};
138142

139143
void NullCallback(const Napi::CallbackInfo& info)
@@ -352,8 +356,22 @@ class CloseFD
352356
}
353357
};
354358

355-
void ThrowErrnoError(const Napi::CallbackInfo& info, const char *msg)
359+
void Disruptor::ThrowErrnoError(const Napi::CallbackInfo& info,
360+
const char *msg,
361+
bool constructing)
356362
{
363+
// Work around bug in ObjectWrap destruction
364+
// https://github.com/nodejs/node-addon-api/pull/475
365+
// Remove this code and 'constructing' parameter when the PR is merged
366+
if (constructing && !IsEmpty())
367+
{
368+
Napi::Object object = Value();
369+
if (!object.IsEmpty())
370+
{
371+
napi_remove_wrap(Env(), object, nullptr);
372+
}
373+
}
374+
357375
int errnum = errno;
358376
char buf[1024] = {0};
359377
auto errmsg = strerror_r(errnum, buf, sizeof(buf));
@@ -383,7 +401,7 @@ Disruptor::Disruptor(const Napi::CallbackInfo& info) :
383401
S_IRUSR | S_IWUSR)));
384402
if (*shm_fd < 0)
385403
{
386-
ThrowErrnoError(info, "Failed to open shared memory object");
404+
ThrowErrnoError(info, "Failed to open shared memory object", true);
387405
}
388406

389407
// Allow space for all the elements,
@@ -397,7 +415,7 @@ Disruptor::Disruptor(const Napi::CallbackInfo& info) :
397415
// Note: ftruncate initializes to null bytes.
398416
if (init && (ftruncate(*shm_fd, shm_size) < 0))
399417
{
400-
ThrowErrnoError(info, "Failed to size shared memory"); //LCOV_EXCL_LINE
418+
ThrowErrnoError(info, "Failed to size shared memory", true); //LCOV_EXCL_LINE
401419
}
402420

403421
// Map the shared memory
@@ -408,7 +426,7 @@ Disruptor::Disruptor(const Napi::CallbackInfo& info) :
408426
0);
409427
if (shm_buf == MAP_FAILED)
410428
{
411-
ThrowErrnoError(info, "Failed to map shared memory"); //LCOV_EXCL_LINE
429+
ThrowErrnoError(info, "Failed to map shared memory", true); //LCOV_EXCL_LINE
412430

413431
}
414432

0 commit comments

Comments
 (0)