src: Node implementation of v8::Platform#14001
src: Node implementation of v8::Platform#14001matthewloring wants to merge 8 commits intonodejs:masterfrom matthewloring:new-node-platform
Conversation
|
/cc @jeisinger @nodejs/v8 |
|
v8/v8@3d8e87a was reverted upstream in V8 due to tsan failures in multi-isolate scenarios. It should be safe for use by Node.js but we can also swap out that commit once it re-lands in V8 before landing this PR. (edit by @addaleax: turned the git commit ref into a v8/v8@… ref) |
addaleax
left a comment
There was a problem hiding this comment.
Commented with a few style nits but I think that’s it.
src/node_platform.cc
Outdated
src/node_platform.cc
Outdated
src/node_platform.cc
Outdated
There was a problem hiding this comment.
tiny style nit: uv_work_t* req (like below)
src/node_platform.cc
Outdated
There was a problem hiding this comment.
static_cast here and in the one above as well. ;)
There was a problem hiding this comment.
That has landed and this PR has been rebased.
src/node.cc
Outdated
There was a problem hiding this comment.
This needs to be updated to the new prototype.
src/node.cc
Outdated
There was a problem hiding this comment.
@bnoordhuis Is there a guideline anywhere for C++ code indentation? I (and others possibly) am usually fairly uncertain where the 4-space rule should apply, and where 2-space.
There was a problem hiding this comment.
I’m not sure we’ve written it done somewhere, but generally, use 4 spaces if it’s a statement continuation, 2 spaces if it’s indentation for a block/loop body/conditional statement body/etc.
There was a problem hiding this comment.
I've updated to 4 spaces.
src/node.cc
Outdated
There was a problem hiding this comment.
They are now freed in Dispose.
src/node_platform.cc
Outdated
src/node_platform.cc
Outdated
There was a problem hiding this comment.
1e9 is a little easier to parse for humans. Can you punctuate the comment?
src/node_platform.h
Outdated
There was a problem hiding this comment.
Can you move these to the .cc file and use fully qualified names here? Rule of thumb: no using declarations in headers.
src/node_platform.h
Outdated
There was a problem hiding this comment.
Can you call this e.g. num_threads_?
src/node_platform.cc
Outdated
There was a problem hiding this comment.
Maybe these internal functions could be made static or part of an anonymous namespace?
There was a problem hiding this comment.
Good point – they might be best written as lambdas, for readability?
There was a problem hiding this comment.
I found that with lambdas there were too many variables named handle. I've made them static for now. I can switch to lambdas if that is preferred.
src/node_platform.h
Outdated
There was a problem hiding this comment.
Are node_mutex.h and queue actually used anywhere within the file?
|
Thanks for the comments! I'm currently looking into some intermittent crashes in GC brought on by this change. I'll address these comments once I figure that out. |
src/node_platform.h
Outdated
There was a problem hiding this comment.
Should this just be Isolate* or should the others be v8::Isolate*
There was a problem hiding this comment.
These should all be v8::Isolate.
src/node_platform.h
Outdated
There was a problem hiding this comment.
Would be nice if the API for NodePlatform did not depend on v8 specific types like v8::TracingController and v8::Isolate (below) but it is still a good start just be abstracting NodePlatform itself.
There was a problem hiding this comment.
This was discussed briefly in #12980. This is not an attempt to define a general VM interface. The only goal is to provide a new implementation of v8::Platform as the default platform which is currently used by Node will may allow the process to exit during wasm module compilation.
src/node_platform.cc
Outdated
There was a problem hiding this comment.
ASAN complains about this line, it looks like uv_run() tries to delete this handle as well.
There was a problem hiding this comment.
This needs to call uv_close() and not free the memory until the close callback is called.
There was a problem hiding this comment.
Ok, does the same logic apply to the uv_work_t in AfterBackgroundTask?
src/node.cc
Outdated
There was a problem hiding this comment.
Style nit: four space indent.
src/node.cc
Outdated
There was a problem hiding this comment.
Can you set tracing_agent_ = nullptr;?
src/node_platform.cc
Outdated
There was a problem hiding this comment.
This needs to call uv_close() and not free the memory until the close callback is called.
src/node_platform.cc
Outdated
There was a problem hiding this comment.
Can you call new uv_work_t() here? Coverity tends to complain about heap-allocating structures without initializing them. Same a few lines below with the new uv_timer_t call.
src/node_platform.cc
Outdated
src/node_platform.h
Outdated
There was a problem hiding this comment.
Maybe make these const since they'll never change.
There was a problem hiding this comment.
I've made the thread count const. uv_timer_init and uv_queue_work don't like const loops.
There was a problem hiding this comment.
I mean uv_loop_t* const loop, i.e., non-reseatable pointer. Not a big thing though.
src/tracing/trace_event.cc
Outdated
There was a problem hiding this comment.
Since you're here: g_controller? We use underscore suffix convention really only for data members.
|
Trying the CI: https://ci.nodejs.org/job/node-test-pull-request/9287/ |
|
The CI looks mostly green. The repl timeout seems unrelated. The One option would be to drop all V8 background tasks queued during the execution of |
|
some background tasks from the GC will count down semaphores while the main thread waits for the semaphore, so not running background tasks will result in a deadlock on the main thread. |
|
If background tasks are allowed to be queue during the |
|
Flushing all background tasks after |
addaleax
left a comment
There was a problem hiding this comment.
Just reaffirming that this still LGTM
|
For an update on this, I worked with the V8 team to stop the main thread from blocking on background task completion (reflected in the most recent 3 backports on the PR). A more desirable fix would be to let background tasks started inside The current solution will only be problematic if V8 background tasks started inside I've started a fresh CI here: https://ci.nodejs.org/job/node-test-pull-request/9486/ |
|
It looks like there is one remaining related failure which is on windows: https://ci.nodejs.org/job/node-test-binary-windows/10290/RUN_SUBSET=1,VS_VERSION=vs2017,label=win2016/tapResults/. I can try to get my hands on a windows machine but if anyone on @nodejs/platform-windows can reproduce this or has thoughts on why libuv is unhappy I'd appreciate the help. |
|
After a few more fixes, it looks like |
Original commit message: [heap] Move SweeperTask to CancelableTask This mitigates the problem of blocking on the main thread when the platform is unable to execute background tasks in a timely manner. Bug: v8:6655 Change-Id: Icdaae744ee73146b86b9a28c8035138746721971 Reviewed-on: https://chromium-review.googlesource.com/595467 Commit-Queue: Michael Lippautz <mlippautz@chromium.org> Reviewed-by: Ulan Degenbaev <ulan@chromium.org> Cr-Commit-Position: refs/heads/master@{nodejs#47036} PR-URL: nodejs#14001 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Original commit message: Make CancelableTask ids unique They were only limited to 32 bit when using the internal Hashmap. Since this has changed alreay some time ago, we can switch to 64 bit ids and check that we never overflow. Bug: Change-Id: Ia6c6d02d6b5e555c6941185a79427dc4aa2a1d62 Reviewed-on: https://chromium-review.googlesource.com/598229 Commit-Queue: Michael Lippautz <mlippautz@chromium.org> Reviewed-by: Ulan Degenbaev <ulan@chromium.org> Cr-Commit-Position: refs/heads/master@{nodejs#47085} PR-URL: nodejs#14001 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Original commit message: [heap] Move UnmapFreeMemoryTask to CancelableTask This mitigates the problem of blocking on the main thread when the platform is unable to execute background tasks in a timely manner. Bug: v8:6671 Change-Id: I741d4b7594e8d62721dad32cbfb19551ffacd0c3 Reviewed-on: https://chromium-review.googlesource.com/599528 Commit-Queue: Michael Lippautz <mlippautz@chromium.org> Reviewed-by: Ulan Degenbaev <ulan@chromium.org> Cr-Commit-Position: refs/heads/master@{nodejs#47126} PR-URL: nodejs#14001 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
| if (uv_run(env.event_loop(), UV_RUN_NOWAIT) != 0) | ||
| more = true; | ||
| } | ||
| uv_run(env.event_loop(), UV_RUN_DEFAULT); |
There was a problem hiding this comment.
Is everyone here sure this doesn't break something edge-casey with beforeExit and order of operations?
I'm trying to dig into it but have pretty much run out of time this week.
It seems to me that in some case(s) before, uv callbacks may have been called twice before the next beforeExit, which could alter things at the very least like an addon that uses a uv_check_t handle?
|
I'm assuming that this is not applicable to the 6.x branch. Please let me know if I am mistaken /cc @nodejs/v8 @nodejs/tsc |
Original commit message: [heap] Move SweeperTask to CancelableTask This mitigates the problem of blocking on the main thread when the platform is unable to execute background tasks in a timely manner. Bug: v8:6655 Change-Id: Icdaae744ee73146b86b9a28c8035138746721971 Reviewed-on: https://chromium-review.googlesource.com/595467 Commit-Queue: Michael Lippautz <mlippautz@chromium.org> Reviewed-by: Ulan Degenbaev <ulan@chromium.org> Cr-Commit-Position: refs/heads/master@{nodejs#47036} PR-URL: nodejs#14001 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Original commit message: Make CancelableTask ids unique They were only limited to 32 bit when using the internal Hashmap. Since this has changed alreay some time ago, we can switch to 64 bit ids and check that we never overflow. Bug: Change-Id: Ia6c6d02d6b5e555c6941185a79427dc4aa2a1d62 Reviewed-on: https://chromium-review.googlesource.com/598229 Commit-Queue: Michael Lippautz <mlippautz@chromium.org> Reviewed-by: Ulan Degenbaev <ulan@chromium.org> Cr-Commit-Position: refs/heads/master@{nodejs#47085} PR-URL: nodejs#14001 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Original commit message: [heap] Move UnmapFreeMemoryTask to CancelableTask This mitigates the problem of blocking on the main thread when the platform is unable to execute background tasks in a timely manner. Bug: v8:6671 Change-Id: I741d4b7594e8d62721dad32cbfb19551ffacd0c3 Reviewed-on: https://chromium-review.googlesource.com/599528 Commit-Queue: Michael Lippautz <mlippautz@chromium.org> Reviewed-by: Ulan Degenbaev <ulan@chromium.org> Cr-Commit-Position: refs/heads/master@{nodejs#47126} PR-URL: nodejs#14001 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
V8 modified the platform API to accept a tracing controller at platform creation time that is required to be present for the lifetime of the platform if tracing will every be enabled. This will simplify the implementation of a v8::Platform subclass for node. PR-URL: nodejs/node#14001 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Original commit message: [heap] Move SweeperTask to CancelableTask This mitigates the problem of blocking on the main thread when the platform is unable to execute background tasks in a timely manner. Bug: v8:6655 Change-Id: Icdaae744ee73146b86b9a28c8035138746721971 Reviewed-on: https://chromium-review.googlesource.com/595467 Commit-Queue: Michael Lippautz <mlippautz@chromium.org> Reviewed-by: Ulan Degenbaev <ulan@chromium.org> Cr-Commit-Position: refs/heads/master@{#47036} Backport-PR-URL: #15393 PR-URL: #14001 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Original commit message: Make CancelableTask ids unique They were only limited to 32 bit when using the internal Hashmap. Since this has changed alreay some time ago, we can switch to 64 bit ids and check that we never overflow. Bug: Change-Id: Ia6c6d02d6b5e555c6941185a79427dc4aa2a1d62 Reviewed-on: https://chromium-review.googlesource.com/598229 Commit-Queue: Michael Lippautz <mlippautz@chromium.org> Reviewed-by: Ulan Degenbaev <ulan@chromium.org> Cr-Commit-Position: refs/heads/master@{#47085} Backport-PR-URL: #15393 PR-URL: #14001 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Original commit message: [heap] Move UnmapFreeMemoryTask to CancelableTask This mitigates the problem of blocking on the main thread when the platform is unable to execute background tasks in a timely manner. Bug: v8:6671 Change-Id: I741d4b7594e8d62721dad32cbfb19551ffacd0c3 Reviewed-on: https://chromium-review.googlesource.com/599528 Commit-Queue: Michael Lippautz <mlippautz@chromium.org> Reviewed-by: Ulan Degenbaev <ulan@chromium.org> Cr-Commit-Position: refs/heads/master@{#47126} Backport-PR-URL: #15393 PR-URL: #14001 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Original commit message: [heap] Move SweeperTask to CancelableTask This mitigates the problem of blocking on the main thread when the platform is unable to execute background tasks in a timely manner. Bug: v8:6655 Change-Id: Icdaae744ee73146b86b9a28c8035138746721971 Reviewed-on: https://chromium-review.googlesource.com/595467 Commit-Queue: Michael Lippautz <mlippautz@chromium.org> Reviewed-by: Ulan Degenbaev <ulan@chromium.org> Cr-Commit-Position: refs/heads/master@{#47036} Backport-PR-URL: #15393 PR-URL: #14001 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Original commit message: Make CancelableTask ids unique They were only limited to 32 bit when using the internal Hashmap. Since this has changed alreay some time ago, we can switch to 64 bit ids and check that we never overflow. Bug: Change-Id: Ia6c6d02d6b5e555c6941185a79427dc4aa2a1d62 Reviewed-on: https://chromium-review.googlesource.com/598229 Commit-Queue: Michael Lippautz <mlippautz@chromium.org> Reviewed-by: Ulan Degenbaev <ulan@chromium.org> Cr-Commit-Position: refs/heads/master@{#47085} Backport-PR-URL: #15393 PR-URL: #14001 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Original commit message: [heap] Move UnmapFreeMemoryTask to CancelableTask This mitigates the problem of blocking on the main thread when the platform is unable to execute background tasks in a timely manner. Bug: v8:6671 Change-Id: I741d4b7594e8d62721dad32cbfb19551ffacd0c3 Reviewed-on: https://chromium-review.googlesource.com/599528 Commit-Queue: Michael Lippautz <mlippautz@chromium.org> Reviewed-by: Ulan Degenbaev <ulan@chromium.org> Cr-Commit-Position: refs/heads/master@{#47126} Backport-PR-URL: #15393 PR-URL: #14001 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Original commit message: [heap] Move SweeperTask to CancelableTask This mitigates the problem of blocking on the main thread when the platform is unable to execute background tasks in a timely manner. Bug: v8:6655 Change-Id: Icdaae744ee73146b86b9a28c8035138746721971 Reviewed-on: https://chromium-review.googlesource.com/595467 Commit-Queue: Michael Lippautz <mlippautz@chromium.org> Reviewed-by: Ulan Degenbaev <ulan@chromium.org> Cr-Commit-Position: refs/heads/master@{#47036} Backport-PR-URL: #15393 PR-URL: #14001 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Original commit message: Make CancelableTask ids unique They were only limited to 32 bit when using the internal Hashmap. Since this has changed alreay some time ago, we can switch to 64 bit ids and check that we never overflow. Bug: Change-Id: Ia6c6d02d6b5e555c6941185a79427dc4aa2a1d62 Reviewed-on: https://chromium-review.googlesource.com/598229 Commit-Queue: Michael Lippautz <mlippautz@chromium.org> Reviewed-by: Ulan Degenbaev <ulan@chromium.org> Cr-Commit-Position: refs/heads/master@{#47085} Backport-PR-URL: #15393 PR-URL: #14001 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Original commit message: [heap] Move UnmapFreeMemoryTask to CancelableTask This mitigates the problem of blocking on the main thread when the platform is unable to execute background tasks in a timely manner. Bug: v8:6671 Change-Id: I741d4b7594e8d62721dad32cbfb19551ffacd0c3 Reviewed-on: https://chromium-review.googlesource.com/599528 Commit-Queue: Michael Lippautz <mlippautz@chromium.org> Reviewed-by: Ulan Degenbaev <ulan@chromium.org> Cr-Commit-Position: refs/heads/master@{#47126} Backport-PR-URL: #15393 PR-URL: #14001 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Node.js currently uses the V8 implementation of the DefaultPlatform
which schedules VM tasks on a V8 managed thread pool. Since the Node.js
event loop is not aware of these tasks, the Node.js process may exit
while there are outstanding VM tasks. This will become problematic once
asynchronous wasm compilation lands in V8.
This PR introduces a Node.js specific implementation of the v8::Platform
on top of libuv so that the event loop is aware of outstanding VM tasks.
Fixes: #12980
Checklist
make -j4 test(UNIX), orvcbuild test(Windows) passesAffected core subsystem(s)
src, tracing