diff options
author | Anna Henningsen <anna@addaleax.net> | 2020-12-13 23:43:40 +0300 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2020-12-14 18:50:23 +0300 |
commit | 67643e1c2ecc380a0d92550eb7ce2d283c8c6184 (patch) | |
tree | 82e0671534ff11c25dfc314962b0610183cc5d34 | |
parent | 68b6c1a30d3a153bf9ba55f1fad477d78f4cf682 (diff) |
worker: fix broadcast channel SharedArrayBuffer passing
Make sure that `SharedArrayBuffer`s can be deserialized on multiple
receiving ends. As a drive-by, also fix the condition of the
internal assertion that should occur if there are transferables
passed to multiple destinations.
PR-URL: https://github.com/nodejs/node/pull/36501
Reviewed-By: Michaƫl Zasso <targos@protonmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: James M Snell <jasnell@gmail.com>
-rw-r--r-- | src/node_messaging.cc | 5 | ||||
-rw-r--r-- | src/node_messaging.h | 3 | ||||
-rw-r--r-- | test/parallel/test-worker-broadcastchannel.js | 19 |
3 files changed, 18 insertions, 9 deletions
diff --git a/src/node_messaging.cc b/src/node_messaging.cc index 03c6d5aaa76..5f054c41c8e 100644 --- a/src/node_messaging.cc +++ b/src/node_messaging.cc @@ -157,8 +157,7 @@ MaybeLocal<Value> Message::Deserialize(Environment* env, // Attach all transferred SharedArrayBuffers to their new Isolate. for (uint32_t i = 0; i < shared_array_buffers_.size(); ++i) { Local<SharedArrayBuffer> sab = - SharedArrayBuffer::New(env->isolate(), - std::move(shared_array_buffers_[i])); + SharedArrayBuffer::New(env->isolate(), shared_array_buffers_[i]); shared_array_buffers.push_back(sab); } @@ -1309,7 +1308,7 @@ Maybe<bool> SiblingGroup::Dispatch( // Transferables cannot be used when there is more // than a single destination. - if (size() > 2 && message->transferables().size()) { + if (size() > 2 && message->has_transferables()) { if (error != nullptr) *error = "Transferables cannot be used with multiple destinations."; return Nothing<bool>(); diff --git a/src/node_messaging.h b/src/node_messaging.h index bb30c78d865..ad065659772 100644 --- a/src/node_messaging.h +++ b/src/node_messaging.h @@ -94,6 +94,9 @@ class Message : public MemoryRetainer { const std::vector<std::unique_ptr<TransferData>>& transferables() const { return transferables_; } + bool has_transferables() const { + return !transferables_.empty() || !array_buffers_.empty(); + } void MemoryInfo(MemoryTracker* tracker) const override; diff --git a/test/parallel/test-worker-broadcastchannel.js b/test/parallel/test-worker-broadcastchannel.js index 1f104a5edf4..b26fbc3769d 100644 --- a/test/parallel/test-worker-broadcastchannel.js +++ b/test/parallel/test-worker-broadcastchannel.js @@ -111,12 +111,19 @@ assert.throws(() => new BroadcastChannel(), { { const bc1 = new BroadcastChannel('channel3'); const bc2 = new BroadcastChannel('channel3'); - bc2.postMessage(new SharedArrayBuffer(10)); - bc1.addEventListener('message', common.mustCall(({ data }) => { - assert(data instanceof SharedArrayBuffer); - bc1.close(); - bc2.close(); - })); + const bc3 = new BroadcastChannel('channel3'); + bc3.postMessage(new SharedArrayBuffer(10)); + let received = 0; + for (const bc of [bc1, bc2]) { + bc.addEventListener('message', common.mustCall(({ data }) => { + assert(data instanceof SharedArrayBuffer); + if (++received === 2) { + bc1.close(); + bc2.close(); + bc3.close(); + } + })); + } } { |