Skip to content
This repository was archived by the owner on Aug 31, 2018. It is now read-only.

worker: implement SharedArrayBuffer/MessagePort transferring - #106

Merged
addaleax merged 2 commits into
latestfrom
messageport-transfer
Oct 16, 2017
Merged

addaleax merged 2 commits into
latestfrom
messageport-transfer

Conversation

@addaleax

Copy link
Copy Markdown
Contributor

These have already been reviewed by @Qard but I would like to give them a bit of public review time on their own :) They can be merged pretty soon, though.

// Create a SharedArrayBuffer object for a specific Environment and Context.
// The created SharedArrayBuffer will be in externalized mode and has
// a hidden object attached to it, during whose lifetime the reference
// count is increased by 1.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Qard pointed out in #58 (rightfully) that this is somewhat awkward phrasing … I’ll need a bit of time to come up with sth better, I am afraid 😄

Support passing `MessagePort` instances through other `MessagePort`s,
as expected by the `MessagePort` spec.

PR-URL: #106
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Logic is added to the `MessagePort` mechanism that
attaches hidden objects to those instances when they are transferred
that track their lifetime and maintain a reference count, to make
sure that memory is freed at the appropriate times.

PR-URL: #106
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
@addaleax
addaleax force-pushed the messageport-transfer branch from df2416b to b8ff855 Compare October 16, 2017 19:25
@addaleax
addaleax merged commit b8ff855 into latest Oct 16, 2017
@addaleax
addaleax deleted the messageport-transfer branch October 16, 2017 19:31
->HasInstance(lifetime_partner)) {
if (!source->IsExternal()) {
env->ThrowError("Found internalized SharedArrayBuffer with "
"lifetime partner object");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assume this is an internal bug if this happens? If so we should make it clear that this error should be reported back to us.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I guess so, yes … you could mess with this using process.binding() but I don’t think that really counts :)

// If this is an external SharedArrayBuffer but we do not see a lifetime
// partner object, we did not externalize it. In that case, there is no
// way to serialize it.
env->ThrowError("Cannot serialize externalized SharedArrayBuffer");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests would be nice although not necessary. TypeError.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, right. I think this is going to require an addon test

Comment thread src/node_messaging.cc
return WriteMessagePort(Unwrap<MessagePort>(object));
}

env_->ThrowError("Cannot serialize unknown type of host object");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests would be nice although not necessary.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I know it’s kind of unfortunate that they are in a separate commit – the thing is, it just makes a lot more sense to test these methods when actually transferring data between workers…

Comment thread src/node_messaging.cc
}
}

env_->ThrowError("MessagePort was not listed in transferList");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test needed. Also TypeError.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, at least Firefox throws a special DataCloneError, which is a DOMException and therefore a plain Error…

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DOMException isn't really encouraged for new errors any more. TypeErrors and other error types that map to JS are the future for the web. Plus we don't really care about compatibility, do we?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, not really the point :) Still, why TypeError? I don’t really think the semantics here are “this thing has the wrong type”, more like, “you missed an entry in a list, please add it”, which doesn’t seem like a type mismatch to me…

Plus we don't really care about compatibility, do we?

Well … for MessagePorts/MessageChannels we kind of do? 😄 Maybe not in this first iteration but it should be doable to get some degree of compatibility

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still, why TypeError?

TypeError is used to indicate an unsuccessful operation when none of the other NativeError objects are an appropriate indication of the failure cause.

https://tc39.github.io/ecma262/#sec-native-error-types-used-in-this-standard-typeerror

Either way I just realized require('v8').Serializer.prototype._getDataCloneError is also Error, so I guess consistency trumps all.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://tc39.github.io/ecma262/#sec-native-error-types-used-in-this-standard-typeerror

I think that’s because the language spec isn’t really concerned with these kinds of errors?

Either way I just realized require('v8').Serializer.prototype._getDataCloneError is also Error, so I guess consistency trumps all.

I mean … that’s because I wrote it that way. 😄 We definitely can change that if you think it makes sense

Comment thread src/node_messaging.cc
}
seen_shared_array_buffers_.push_back(shared_array_buffer);
msg_->AddSharedArrayBuffer(reference);
return Just(i);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the returned ID actually used anywhere in Ayo code or is it just for V8?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only for V8, to match the objects, really… the matching call on the deserializing side is deserializer.TransferSharedArrayBuffer(i, sab);

@TimothyGu

Copy link
Copy Markdown
Member

Sorry about my late review... I've been kinda busy the past couple of days.

addaleax added a commit to addaleax/node that referenced this pull request May 18, 2018
Support passing `MessagePort` instances through other `MessagePort`s,
as expected by the `MessagePort` spec.

Thanks to Stephen Belanger for reviewing this change in its original PR.

Refs: ayojs/ayo#106
addaleax added a commit to addaleax/node that referenced this pull request May 18, 2018
Logic is added to the `MessagePort` mechanism that
attaches hidden objects to those instances when they are transferred
that track their lifetime and maintain a reference count, to make
sure that memory is freed at the appropriate times.

Thanks to Stephen Belanger for reviewing this change in its original PR.

Refs: ayojs/ayo#106
addaleax added a commit to addaleax/node that referenced this pull request May 22, 2018
Support passing `MessagePort` instances through other `MessagePort`s,
as expected by the `MessagePort` spec.

Thanks to Stephen Belanger for reviewing this change in its original PR.

Refs: ayojs/ayo#106
addaleax added a commit to addaleax/node that referenced this pull request May 22, 2018
Logic is added to the `MessagePort` mechanism that
attaches hidden objects to those instances when they are transferred
that track their lifetime and maintain a reference count, to make
sure that memory is freed at the appropriate times.

Thanks to Stephen Belanger for reviewing this change in its original PR.

Refs: ayojs/ayo#106
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants