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 2019-02-03 20:20:52 +01:00
parent 39eca841c3
commit b1cc1af2bd
No known key found for this signature in database
GPG Key ID: 9C63F3A6CD2AD8F9

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.