Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6e66371

Browse filesBrowse files
legendecasdanielleadams
authored andcommitted
node-api: document node-api shutdown finalization
As status quo, the cleanup hooks are invoked before the `napi_finalize` callbacks at the exit of Node.js environments. This gives addons a chance to release their resource in a proper order manually. Document this behavior explicitly to advocate the usage on cleanup hooks instead of relying on the implied invocation of `napi_finalize` callbacks at shutdown. PR-URL: #45903 Fixes: #45088 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michael Dawson <midawson@redhat.com>
1 parent 9a11788 commit 6e66371
Copy full SHA for 6e66371

File tree

Expand file treeCollapse file tree

1 file changed

+31
-14
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+31
-14
lines changed
Open diff view settings
Collapse file

‎doc/api/n-api.md‎

Copy file name to clipboardExpand all lines: doc/api/n-api.md
+31-14Lines changed: 31 additions & 14 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -478,11 +478,11 @@ may be called multiple times, from multiple contexts, and even concurrently from
478478
multiple threads.
479479

480480
Native addons may need to allocate global state which they use during
481-
their entire life cycle such that the state must be unique to each instance of
482-
the addon.
481+
their life cycle of an Node.js environment such that the state can be
482+
unique to each instance of the addon.
483483

484-
To this end, Node-API provides a way to allocate data such that its life cycle
485-
is tied to the life cycle of the Agent.
484+
To this end, Node-API provides a way to associate data such that its life cycle
485+
is tied to the life cycle of a Node.js environment.
486486

487487
### `napi_set_instance_data`
488488

@@ -510,11 +510,11 @@ napi_status napi_set_instance_data(napi_env env,
510510

511511
Returns `napi_ok` if the API succeeded.
512512

513-
This API associates `data` with the currently running Agent. `data` can later
514-
be retrieved using `napi_get_instance_data()`. Any existing data associated with
515-
the currently running Agent which was set by means of a previous call to
516-
`napi_set_instance_data()` will be overwritten. If a `finalize_cb` was provided
517-
by the previous call, it will not be called.
513+
This API associates `data` with the currently running Node.js environment. `data`
514+
can later be retrieved using `napi_get_instance_data()`. Any existing data
515+
associated with the currently running Node.js environment which was set by means
516+
of a previous call to `napi_set_instance_data()` will be overwritten. If a
517+
`finalize_cb` was provided by the previous call, it will not be called.
518518

519519
### `napi_get_instance_data`
520520

@@ -532,13 +532,13 @@ napi_status napi_get_instance_data(napi_env env,
532532

533533
* `[in] env`: The environment that the Node-API call is invoked under.
534534
* `[out] data`: The data item that was previously associated with the currently
535-
running Agent by a call to `napi_set_instance_data()`.
535+
running Node.js environment by a call to `napi_set_instance_data()`.
536536

537537
Returns `napi_ok` if the API succeeded.
538538

539539
This API retrieves data that was previously associated with the currently
540-
running Agent via `napi_set_instance_data()`. If no data is set, the call will
541-
succeed and `data` will be set to `NULL`.
540+
running Node.js environment via `napi_set_instance_data()`. If no data is set,
541+
the call will succeed and `data` will be set to `NULL`.
542542

543543
## Basic Node-API data types
544544

@@ -1799,11 +1799,11 @@ If still valid, this API returns the `napi_value` representing the
17991799
JavaScript `Object` associated with the `napi_ref`. Otherwise, result
18001800
will be `NULL`.
18011801

1802-
### Cleanup on exit of the current Node.js instance
1802+
### Cleanup on exit of the current Node.js environment
18031803

18041804
While a Node.js process typically releases all its resources when exiting,
18051805
embedders of Node.js, or future Worker support, may require addons to register
1806-
clean-up hooks that will be run once the current Node.js instance exits.
1806+
clean-up hooks that will be run once the current Node.js environment exits.
18071807

18081808
Node-API provides functions for registering and un-registering such callbacks.
18091809
When those callbacks are run, all resources that are being held by the addon
@@ -1929,6 +1929,22 @@ the hook from being executed, unless it has already started executing.
19291929
This must be called on any `napi_async_cleanup_hook_handle` value obtained
19301930
from [`napi_add_async_cleanup_hook`][].
19311931

1932+
### Finalization on the exit of the Node.js environment
1933+
1934+
The Node.js environment may be torn down at an arbitrary time as soon as
1935+
possible with JavaScript execution disallowed, like on the request of
1936+
[`worker.terminate()`][]. When the environment is being torn down, the
1937+
registered `napi_finalize` callbacks of JavaScript objects, Thread-safe
1938+
functions and environment instance data are invoked immediately and
1939+
independently.
1940+
1941+
The invocation of `napi_finalize` callbacks are scheduled after the manually
1942+
registered cleanup hooks. In order to ensure a proper order of addon
1943+
finalization during environment shutdown to avoid use-after-free in the
1944+
`napi_finalize` callback, addons should register a cleanup hook with
1945+
`napi_add_env_cleanup_hook` and `napi_add_async_cleanup_hook` to manually
1946+
release the allocated resource in a proper order.
1947+
19321948
## Module registration
19331949

19341950
Node-API modules are registered in a manner similar to other modules
@@ -6431,6 +6447,7 @@ the add-on's file name during loading.
64316447
[`process.release`]: process.md#processrelease
64326448
[`uv_ref`]: https://docs.libuv.org/en/v1.x/handle.html#c.uv_ref
64336449
[`uv_unref`]: https://docs.libuv.org/en/v1.x/handle.html#c.uv_unref
6450+
[`worker.terminate()`]: worker_threads.md#workerterminate
64346451
[async_hooks `type`]: async_hooks.md#type
64356452
[context-aware addons]: addons.md#context-aware-addons
64366453
[docs]: https://github.com/nodejs/node-addon-api#api-documentation

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.