Skip to content

Commit 5eed20b

Browse files
addaleaxcodebytere
authored andcommittedJun 9, 2020
worker: fix race condition in node_messaging.cc
`AddToIncomingQueue()` relies on `owner_` only being modified with `mutex_` being locked, but in these two places, that didn’t happen. Modify them to use `Detach()` instead, which has the same effect as setting `owner_ = nullptr` here, but does it with proper locking. This race condition probably only shows up in practice when Node.js is compiled in debug mode, because the compiler eliminates the duplicate load in `AddToIncomingQueue()` when compiling with optimizations enabled. PR-URL: #33429 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: David Carlier <devnexen@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent c797c7c commit 5eed20b

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed
 

‎src/node_messaging.cc

+3-5
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,7 @@ void MessagePortData::Disentangle() {
494494
}
495495

496496
MessagePort::~MessagePort() {
497-
if (data_)
498-
data_->owner_ = nullptr;
497+
if (data_) Detach();
499498
}
500499

501500
MessagePort::MessagePort(Environment* env,
@@ -692,10 +691,9 @@ void MessagePort::OnMessage() {
692691
void MessagePort::OnClose() {
693692
Debug(this, "MessagePort::OnClose()");
694693
if (data_) {
695-
data_->owner_ = nullptr;
696-
data_->Disentangle();
694+
// Detach() returns move(data_).
695+
Detach()->Disentangle();
697696
}
698-
data_.reset();
699697
}
700698

701699
std::unique_ptr<MessagePortData> MessagePort::Detach() {

0 commit comments

Comments
 (0)
Please sign in to comment.