worker: add mutex lock to MessagePort ctor

Automated tooling for race condition detection reports this as a
possible problem. It’s unlikely that something bad would happen
here (beyond maybe calling `TriggerAsync()` twice), but adding this
should be okay.

PR-URL: https://github.com/nodejs/node/pull/25911
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Anna Henningsen committed 2019-02-06 15:53:10 +01:00
1 parent 2009f18064
commit e70aa30ebd
1 file changed
+5
+5
View File
@@ -533,6 +533,11 @@ MessagePort* MessagePort::New(
if (data) {
port->Detach();
port->data_ = std::move(data);
// This lock is here to avoid race conditions with the `owner_` read
// in AddToIncomingQueue(). (This would likely be unproblematic without it,
// but it's better to be safe than sorry.)
Mutex::ScopedLock lock(port->data_->mutex_);
port->data_->owner_ = port;
// If the existing MessagePortData object had pending messages, this is
// the easiest way to run that queue.