Skip to content

Commit 8757c27

Browse files
committed
src: use lock for c-ares library init/cleanup
PR-URL: ayojs#82
1 parent 6d5b9ea commit 8757c27

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

src/cares_wrap.cc

+5
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ using v8::Value;
7070

7171
namespace {
7272

73+
Mutex ares_library_mutex;
74+
7375
inline uint16_t cares_get_16bit(const unsigned char* p) {
7476
return static_cast<uint32_t>(p[0] << 8U) | (static_cast<uint32_t>(p[1]));
7577
}
@@ -496,6 +498,7 @@ void ChannelWrap::Setup() {
496498

497499
int r;
498500
if (!library_inited_) {
501+
Mutex::ScopedLock lock(ares_library_mutex);
499502
// Multiple calls to ares_library_init() increase a reference counter,
500503
// so this is a no-op except for the first call to it.
501504
r = ares_library_init(ARES_LIB_INIT_ALL);
@@ -509,6 +512,7 @@ void ChannelWrap::Setup() {
509512
ARES_OPT_FLAGS | ARES_OPT_SOCK_STATE_CB);
510513

511514
if (r != ARES_SUCCESS) {
515+
Mutex::ScopedLock lock(ares_library_mutex);
512516
ares_library_cleanup();
513517
return env()->ThrowError(ToErrorCodeString(r));
514518
}
@@ -526,6 +530,7 @@ void ChannelWrap::Setup() {
526530

527531
ChannelWrap::~ChannelWrap() {
528532
if (library_inited_) {
533+
Mutex::ScopedLock lock(ares_library_mutex);
529534
// This decreases the reference counter increased by ares_library_init().
530535
ares_library_cleanup();
531536
}

0 commit comments

Comments
 (0)