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

[v12.x backport] src: improve embedder API #35241

Closed
wants to merge 37 commits into from

Commits on Sep 23, 2020

  1. src: make FreeEnvironment() perform all necessary cleanup

    Make the calls `stop_sub_worker_contexts()`, `RunCleanup()`
    part of the public API for easier embedding.
    
    (Note that calling `RunAtExit()` is idempotent because the
    at-exit callback queue is cleared after each call.)
    
    PR-URL: nodejs#30467
    Reviewed-By: James M Snell <[email protected]>
    Reviewed-By: Gireesh Punathil <[email protected]>
    addaleax committed Sep 23, 2020
    Configuration menu
    Copy the full SHA
    ebdf84a View commit details
    Browse the repository at this point in the history
  2. src: fix memory leak in CreateEnvironment when bootstrap fails

    PR-URL: nodejs#30467
    Reviewed-By: James M Snell <[email protected]>
    Reviewed-By: Gireesh Punathil <[email protected]>
    addaleax committed Sep 23, 2020
    Configuration menu
    Copy the full SHA
    3f9fa28 View commit details
    Browse the repository at this point in the history
  3. src: move worker_context from Environment to IsolateData

    Workers are fully in control of their Isolates, and this helps
    avoid a problem with later changes to `CreateEnvironment()`
    because now we can run the boostrapping code inside the latter.
    
    PR-URL: nodejs#30467
    Reviewed-By: James M Snell <[email protected]>
    Reviewed-By: Gireesh Punathil <[email protected]>
    addaleax committed Sep 23, 2020
    Configuration menu
    Copy the full SHA
    459973c View commit details
    Browse the repository at this point in the history
  4. src: associate is_main_thread() with worker_context()

    In our codebase, the assumption generally is that `!is_main_thread()`
    means that the current Environment belongs to a Node.js Worker thread.
    
    PR-URL: nodejs#30467
    Reviewed-By: James M Snell <[email protected]>
    Reviewed-By: Gireesh Punathil <[email protected]>
    addaleax committed Sep 23, 2020
    Configuration menu
    Copy the full SHA
    2c87b17 View commit details
    Browse the repository at this point in the history
  5. src: align worker and main thread code with embedder API

    This addresses some long-standing TODOs by Joyee and me about
    making the embedder API more powerful and us less reliant on
    internal APIs for creating the main thread and Workers.
    
    PR-URL: nodejs#30467
    Reviewed-By: James M Snell <[email protected]>
    Reviewed-By: Gireesh Punathil <[email protected]>
    addaleax committed Sep 23, 2020
    Configuration menu
    Copy the full SHA
    0e9f4a9 View commit details
    Browse the repository at this point in the history
  6. src: provide a variant of LoadEnvironment taking a callback

    This allows embedders to flexibly control how they start JS code
    rather than using `third_party_main`.
    
    PR-URL: nodejs#30467
    Reviewed-By: James M Snell <[email protected]>
    Reviewed-By: Gireesh Punathil <[email protected]>
    addaleax committed Sep 23, 2020
    Configuration menu
    Copy the full SHA
    c694f08 View commit details
    Browse the repository at this point in the history
  7. src: add LoadEnvironment() variant taking a string

    Allow passing a string as the main module rather than using
    the callback variant.
    
    PR-URL: nodejs#30467
    Reviewed-By: James M Snell <[email protected]>
    Reviewed-By: Gireesh Punathil <[email protected]>
    addaleax committed Sep 23, 2020
    Configuration menu
    Copy the full SHA
    8a1255c View commit details
    Browse the repository at this point in the history
  8. test: re-enable cctest that was commented out

    Refs: nodejs#31910
    
    PR-URL: nodejs#30467
    Reviewed-By: James M Snell <[email protected]>
    Reviewed-By: Gireesh Punathil <[email protected]>
    addaleax committed Sep 23, 2020
    Configuration menu
    Copy the full SHA
    bb689cf View commit details
    Browse the repository at this point in the history
  9. src: add unique_ptr equivalent of CreatePlatform

    This makes this bit of the embedder situation a bit easier to use.
    
    PR-URL: nodejs#30467
    Reviewed-By: James M Snell <[email protected]>
    Reviewed-By: Gireesh Punathil <[email protected]>
    addaleax committed Sep 23, 2020
    Configuration menu
    Copy the full SHA
    ad95b85 View commit details
    Browse the repository at this point in the history
  10. src: make InitializeNodeWithArgs() official public API

    This is a decent replacement for the to-be-deprecated Init() API.
    
    PR-URL: nodejs#30467
    Reviewed-By: James M Snell <[email protected]>
    Reviewed-By: Gireesh Punathil <[email protected]>
    addaleax committed Sep 23, 2020
    Configuration menu
    Copy the full SHA
    33ecd3b View commit details
    Browse the repository at this point in the history
  11. src: add ability to look up platform based on Environment*

    This should eventually remove any necessity to use the global-state
    `GetMainThreadMultiIsolatePlatform()`.
    
    PR-URL: nodejs#30467
    Reviewed-By: James M Snell <[email protected]>
    Reviewed-By: Gireesh Punathil <[email protected]>
    addaleax committed Sep 23, 2020
    Configuration menu
    Copy the full SHA
    31528f3 View commit details
    Browse the repository at this point in the history
  12. src: allow non-Node.js TracingControllers

    We do not need a Node.js-provided `v8::TracingController`, generally.
    Loosen that restriction in order to make it easier for embedders
    to provide their own subclass of `v8::TracingController`,
    or none at all.
    
    Refs: electron/electron@9c36576
    
    PR-URL: nodejs#30467
    Reviewed-By: James M Snell <[email protected]>
    Reviewed-By: Gireesh Punathil <[email protected]>
    addaleax committed Sep 23, 2020
    Configuration menu
    Copy the full SHA
    eca25d0 View commit details
    Browse the repository at this point in the history
  13. src: fix what a dispose without checking

    If created platform with CreatePlatform, the crash occurs because
    it does not check if it was initialized to v8_platform
    when DisposePlatform was called.
    
    Refs: nodejs#31260
    Co-authored-by: Anna Henningsen <[email protected]>
    PR-URL: nodejs#30467
    Reviewed-By: James M Snell <[email protected]>
    Reviewed-By: Gireesh Punathil <[email protected]>
    Jichan and addaleax committed Sep 23, 2020
    Configuration menu
    Copy the full SHA
    691a575 View commit details
    Browse the repository at this point in the history
  14. src: shutdown platform from FreePlatform()

    There is currently no way to properly do this.
    
    PR-URL: nodejs#30467
    Reviewed-By: James M Snell <[email protected]>
    Reviewed-By: Gireesh Punathil <[email protected]>
    addaleax committed Sep 23, 2020
    Configuration menu
    Copy the full SHA
    1c5d55a View commit details
    Browse the repository at this point in the history
  15. src,test: add full-featured embedder API test

    PR-URL: nodejs#30467
    Reviewed-By: James M Snell <[email protected]>
    Reviewed-By: Gireesh Punathil <[email protected]>
    addaleax committed Sep 23, 2020
    Configuration menu
    Copy the full SHA
    a02c5d0 View commit details
    Browse the repository at this point in the history
  16. doc: add basic embedding example documentation

    PR-URL: nodejs#30467
    Reviewed-By: James M Snell <[email protected]>
    Reviewed-By: Gireesh Punathil <[email protected]>
    addaleax committed Sep 23, 2020
    Configuration menu
    Copy the full SHA
    0547551 View commit details
    Browse the repository at this point in the history
  17. test: add extended embedder cctest

    Add an embedder cctest that also covers a multi-Environment situation,
    including worker_threads-style inspector support.
    
    Co-authored-by: Joyee Cheung <[email protected]>
    
    PR-URL: nodejs#30467
    Reviewed-By: James M Snell <[email protected]>
    Reviewed-By: Gireesh Punathil <[email protected]>
    addaleax committed Sep 23, 2020
    Configuration menu
    Copy the full SHA
    a7ada43 View commit details
    Browse the repository at this point in the history
  18. test: wait for message from parent in embedding cctest

    I’ve seen this fail a few times in CI, presumably because the
    inspector commmand did not reach the child thread in time.
    Explicitly waiting seems to solve that.
    
    Refs: nodejs#30467
    
    PR-URL: nodejs#32563
    Reviewed-By: Colin Ihrig <[email protected]>
    Reviewed-By: James M Snell <[email protected]>
    addaleax committed Sep 23, 2020
    Configuration menu
    Copy the full SHA
    1a8e9ee View commit details
    Browse the repository at this point in the history
  19. embedding: provide hook for custom process.exit() behaviour

    Embedders may not want to terminate the process when `process.exit()`
    is called. This provides a hook for more flexible handling of that
    situation.
    
    Refs: nodejs#30467 (comment)
    
    PR-URL: nodejs#32531
    Reviewed-By: Colin Ihrig <[email protected]>
    Reviewed-By: Gireesh Punathil <[email protected]>
    Reviewed-By: James M Snell <[email protected]>
    addaleax committed Sep 23, 2020
    Configuration menu
    Copy the full SHA
    6d231db View commit details
    Browse the repository at this point in the history
  20. embedding: make Stop() stop Workers

    This makes sense given that terminating execution of the parent thread
    this way likely also is supposed to stop all running Worker threads
    spawned by it.
    
    PR-URL: nodejs#32531
    Reviewed-By: Colin Ihrig <[email protected]>
    Reviewed-By: Gireesh Punathil <[email protected]>
    Reviewed-By: James M Snell <[email protected]>
    addaleax committed Sep 23, 2020
    Configuration menu
    Copy the full SHA
    93a23b9 View commit details
    Browse the repository at this point in the history
  21. test: use InitializeNodeWithArgs in cctest

    Refs: nodejs@d7f1107
    Fixes: nodejs#30257
    
    PR-URL: nodejs#32406
    Reviewed-By: Michaël Zasso <[email protected]>
    Reviewed-By: Colin Ihrig <[email protected]>
    Reviewed-By: Jiawen Geng <[email protected]>
    Reviewed-By: Matheus Marchini <[email protected]>
    Reviewed-By: David Carlier <[email protected]>
    Reviewed-By: James M Snell <[email protected]>
    addaleax committed Sep 23, 2020
    Configuration menu
    Copy the full SHA
    72f42bf View commit details
    Browse the repository at this point in the history
  22. test: use common.buildType in embedding test

    This un-breaks testing in the case of `./configure --debug-node`.
    
    PR-URL: nodejs#32422
    Reviewed-By: Richard Lau <[email protected]>
    Reviewed-By: Colin Ihrig <[email protected]>
    Reviewed-By: Luigi Pinca <[email protected]>
    Reviewed-By: James M Snell <[email protected]>
    addaleax committed Sep 23, 2020
    Configuration menu
    Copy the full SHA
    b931e68 View commit details
    Browse the repository at this point in the history
  23. src: initialize inspector before RunBootstrapping()

    This is necessary for `--inspect-brk-node` to work, and for the
    inspector to be aware of scripts created before bootstrapping.
    
    Fixes: nodejs#32648
    Refs: nodejs#30467 (comment)
    
    PR-URL: nodejs#32672
    Reviewed-By: Gus Caplan <[email protected]>
    Reviewed-By: Eugene Ostroukhov <[email protected]>
    Reviewed-By: Joyee Cheung <[email protected]>
    Reviewed-By: David Carlier <[email protected]>
    Reviewed-By: James M Snell <[email protected]>
    addaleax committed Sep 23, 2020
    Configuration menu
    Copy the full SHA
    9aa4cbe View commit details
    Browse the repository at this point in the history
  24. worker: unify custom error creation

    Mostly, this introduces a pattern that makes sure that if a custom
    error is reported, `stopped_` will be set to `true` correctly in
    every cast, which was previously missing for the
    `NewContext().IsEmpty()` case (which led to a hard crash from the
    `Worker` destructor).
    
    This also leaves TODO comments for a few cases in which
    `ERR_WORKER_OUT_OF_MEMORY` was not used in accordance with the
    documentation for that error code (or according to its intention).
    Fixing that is semver-major.
    
    PR-URL: nodejs#33084
    Reviewed-By: Gireesh Punathil <[email protected]>
    Reviewed-By: Colin Ihrig <[email protected]>
    Reviewed-By: James M Snell <[email protected]>
    addaleax committed Sep 23, 2020
    Configuration menu
    Copy the full SHA
    0a2f5e9 View commit details
    Browse the repository at this point in the history
  25. Configuration menu
    Copy the full SHA
    b910fc6 View commit details
    Browse the repository at this point in the history
  26. Configuration menu
    Copy the full SHA
    1ee495d View commit details
    Browse the repository at this point in the history
  27. Configuration menu
    Copy the full SHA
    30b6c2f View commit details
    Browse the repository at this point in the history
  28. Configuration menu
    Copy the full SHA
    6e63159 View commit details
    Browse the repository at this point in the history
  29. src: make Environment::interrupt_data_ atomic

    Otherwise this potentially leads to race conditions when used in a
    multi-threaded environment (i.e. when used for its very purpose).
    
    PR-URL: nodejs#32523
    Reviewed-By: Matheus Marchini <[email protected]>
    Reviewed-By: James M Snell <[email protected]>
    addaleax committed Sep 23, 2020
    Configuration menu
    Copy the full SHA
    8f464fe View commit details
    Browse the repository at this point in the history
  30. src: fix cleanup hook removal for InspectorTimer

    Fix this to account for the fact that `Stop()` may already have been
    called from a cleanup hook when the `inspector::Agent` is deleted
    along with the `Environment`, at which point cleanup hooks are no
    longer available.
    
    PR-URL: nodejs#32523
    Reviewed-By: Matheus Marchini <[email protected]>
    Reviewed-By: James M Snell <[email protected]>
    addaleax committed Sep 23, 2020
    Configuration menu
    Copy the full SHA
    a74f870 View commit details
    Browse the repository at this point in the history
  31. src: use env->RequestInterrupt() for inspector io thread start

    This cleans up `Agent::RequestIoThreadStart()` significantly.
    
    PR-URL: nodejs#32523
    Reviewed-By: Matheus Marchini <[email protected]>
    Reviewed-By: James M Snell <[email protected]>
    addaleax committed Sep 23, 2020
    Configuration menu
    Copy the full SHA
    a2ebe11 View commit details
    Browse the repository at this point in the history
  32. src: use env->RequestInterrupt() for inspector MainThreadInterface

    This simplifies the code significantly, and removes the dependency of
    the inspector code on the availability of a `MultiIsolatePlatform`
    (by removing the dependency on a platform altogether).
    
    It also fixes a memory leak that occurs when `RequestInterrupt()`
    is used, but the interrupt handler is never called before the Isolate
    is destroyed.
    
    One downside is that this leads to a slight change in timing, because
    inspector messages are not dispatched in a re-entrant way. This means
    having to harden one test to account for that possibility by making
    sure that the stack is always clear through a `setImmediate()`.
    This does not affect the assertion made by the test, which is that
    messages will not be delivered synchronously while other code is
    executing.
    
    nodejs#32415
    
    PR-URL: nodejs#32523
    Reviewed-By: Matheus Marchini <[email protected]>
    Reviewed-By: James M Snell <[email protected]>
    addaleax committed Sep 23, 2020
    Configuration menu
    Copy the full SHA
    83015b0 View commit details
    Browse the repository at this point in the history
  33. src: flush V8 interrupts from Environment dtor

    This avoids an edge-case memory leak.
    
    Refs: nodejs#32523 (comment)
    
    PR-URL: nodejs#32523
    Reviewed-By: Matheus Marchini <[email protected]>
    Reviewed-By: James M Snell <[email protected]>
    addaleax committed Sep 23, 2020
    Configuration menu
    Copy the full SHA
    2b0315f View commit details
    Browse the repository at this point in the history
  34. src: add callback scope for native immediates

    This ensures that microtasks scheduled by native immediates are run
    after the tasks are done. In particular, this affects the inspector
    integration since 6f9f546.
    
    Fixes: nodejs#33002
    Refs: nodejs#32523
    
    PR-URL: nodejs#34366
    Reviewed-By: Benjamin Gruenbaum <[email protected]>
    Reviewed-By: Gerhard Stöbich <[email protected]>
    Reviewed-By: James M Snell <[email protected]>
    addaleax committed Sep 23, 2020
    Configuration menu
    Copy the full SHA
    aa3e118 View commit details
    Browse the repository at this point in the history
  35. Configuration menu
    Copy the full SHA
    c386bd1 View commit details
    Browse the repository at this point in the history
  36. Configuration menu
    Copy the full SHA
    b7f9f3d View commit details
    Browse the repository at this point in the history
  37. test,doc: add missing uv_setup_args() calls

    libuv 1.39.0 will begin requiring uv_setup_args() to be called
    before attempting to access the process title. This commit adds
    uv_setup_args() calls that were missing in order for the test
    suite to pass (and updates the documentation).
    
    PR-URL: nodejs#34751
    Reviewed-By: Richard Lau <[email protected]>
    Reviewed-By: David Carlier <[email protected]>
    Reviewed-By: Rich Trott <[email protected]>
    cjihrig authored and addaleax committed Sep 23, 2020
    Configuration menu
    Copy the full SHA
    fa4439d View commit details
    Browse the repository at this point in the history