Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

worker: fix crash when SharedArrayBuffer outlives creating thread #28788

Closed

Conversation

addaleax
Copy link
Member

Use the parent thread’s ArrayBuffer::Allocator when creating a
Worker instance, as that allocator is guaranteed to outlive the
Worker itself.

Fixes: #28777
Fixes: #28773

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. worker Issues and PRs related to Worker support. labels Jul 20, 2019
@nodejs-github-bot
Copy link
Collaborator

@Trott
Copy link
Member

Trott commented Jul 20, 2019

The two failures on other worker tests in CI might be related?

@addaleax
Copy link
Member Author

@Trott I don’t know about test-worker-memory – I doubt it – but the other one is almost certainly unrelated, I’m looking into it right now 👍

@addaleax
Copy link
Member Author

Oh, yes, the test-worker-console-listeners failure is related – it happens because after this patch, all threads share a single zero_fill_field for the array buffer allocator, and so it can happen that an ArrayBuffer is not zero filled when it should be, because a different thread does want uninitialized memory.

I’ll figure out something for that.

Use the parent thread’s `ArrayBuffer::Allocator` when creating a
Worker instance, as that allocator is guaranteed to outlive the
Worker itself.

This requires making the zero-fill flag a thread_local variable
in order to avoid race conditions between different threads.
A test for that behaviour is added as well.

Fixes: nodejs#28777
Fixes: nodejs#28773
@addaleax addaleax force-pushed the worker-sharedarraybuffer-childthread branch from 446d7c5 to 14d4ad0 Compare July 21, 2019 11:28
@addaleax
Copy link
Member Author

I’ve fixed that issue by now by using a thread_local but I’m not really happy with that as a solution … I’ll think about it a bit more. My ideal solution would be to turn the zero-fill field into an atomic counter and increasing/decreasing that from the threads, but I’m having a hard time thinking of a way to make it atomically accessible both from JS and C++.

@Trott
Copy link
Member

Trott commented Jul 22, 2019

I’ve fixed that issue by now by using a thread_local but I’m not really happy with that as a solution … I’ll think about it a bit more. My ideal solution would be to turn the zero-fill field into an atomic counter and increasing/decreasing that from the threads, but I’m having a hard time thinking of a way to make it atomically accessible both from JS and C++.

Good to land this in the meantime or you'd like to think on this bit some more?

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@addaleax
Copy link
Member Author

@Trott I strongly dislike this solution, and I’m still thinking about it … but haven’t been able to come up with somebody better so far. And hopefully, in the worst case we can get rid of it anyway when V8 removes ArrayBuffer::Allocator altogether in the future…

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@Trott
Copy link
Member

Trott commented Aug 7, 2019

AIX failure in CI is genuine?

@addaleax
Copy link
Member Author

addaleax commented Aug 7, 2019

Hm … I’m assuming that it’s because of the thread_local definition here, but I wouldn’t know how to fix that. I’d ping the platform team but maybe that’s just another good reason to throw this solution away…

@addaleax
Copy link
Member Author

addaleax commented Aug 17, 2019

Sorry it took me so long (the HTTP/2 security stuff got preference treatment here), but I came up with a solution that isn’t ideal either but definitely better than this and which should pass CI (→ #29190). I’ll close this PR as the code changes are completely different from the ones in this one.

@addaleax addaleax closed this Aug 17, 2019
@addaleax addaleax deleted the worker-sharedarraybuffer-childthread branch August 17, 2019 22:08
addaleax added a commit to addaleax/node that referenced this pull request Aug 17, 2019
Keep a reference to the `ArrayBuffer::Allocator` alive for at least
as long as a `SharedArrayBuffer` allocated by it lives.

Refs: nodejs#28788
Fixes: nodejs#28777
Fixes: nodejs#28773
addaleax added a commit that referenced this pull request Aug 19, 2019
Keep a reference to the `ArrayBuffer::Allocator` alive for at least
as long as a `SharedArrayBuffer` allocated by it lives.

Refs: #28788
Fixes: #28777
Fixes: #28773

PR-URL: #29190
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
targos pushed a commit that referenced this pull request Aug 20, 2019
Keep a reference to the `ArrayBuffer::Allocator` alive for at least
as long as a `SharedArrayBuffer` allocated by it lives.

Refs: #28788
Fixes: #28777
Fixes: #28773

PR-URL: #29190
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. worker Issues and PRs related to Worker support.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

segfault when using SharedArrayBuffer from an unrefed worker thread Access Violation
5 participants