From 22901d81c905873486de676c8ca9d13f11503878 Mon Sep 17 00:00:00 2001 From: Sebastian Mayr Date: Thu, 9 Nov 2017 22:57:04 +0100 Subject: async_hooks: add destroy event for gced AsyncResources In cases where libraries create AsyncResources which may be emitting more events depending on usage, the only way to ensure that destroy is called properly is by calling it when the resource gets garbage collected. Fixes: https://github.com/nodejs/node/issues/16153 PR-URL: https://github.com/nodejs/node/pull/16998 Fixes: https://github.com/nodejs/node/issues/16153 Reviewed-By: Andreas Madsen Reviewed-By: Anna Henningsen Reviewed-By: James M Snell --- doc/api/async_hooks.md | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'doc/api/async_hooks.md') diff --git a/doc/api/async_hooks.md b/doc/api/async_hooks.md index ff002218b5..dd04b7b28d 100644 --- a/doc/api/async_hooks.md +++ b/doc/api/async_hooks.md @@ -543,12 +543,14 @@ will occur and the process will abort. The following is an overview of the `AsyncResource` API. ```js -const { AsyncResource } = require('async_hooks'); +const { AsyncResource, executionAsyncId } = require('async_hooks'); // AsyncResource() is meant to be extended. Instantiating a // new AsyncResource() also triggers init. If triggerAsyncId is omitted then // async_hook.executionAsyncId() is used. -const asyncResource = new AsyncResource(type, triggerAsyncId); +const asyncResource = new AsyncResource( + type, { triggerAsyncId: executionAsyncId(), requireManualDestroy: false } +); // Call AsyncHooks before callbacks. asyncResource.emitBefore(); @@ -566,11 +568,17 @@ asyncResource.asyncId(); asyncResource.triggerAsyncId(); ``` -#### `AsyncResource(type[, triggerAsyncId])` +#### `AsyncResource(type[, options])` * `type` {string} The type of async event. -* `triggerAsyncId` {number} The ID of the execution context that created this - async event. +* `options` {Object} + * `triggerAsyncId` {number} The ID of the execution context that created this + async event. **Default:** `executionAsyncId()` + * `requireManualDestroy` {boolean} Disables automatic `emitDestroy` when the + object is garbage collected. This usually does not need to be set (even if + `emitDestroy` is called manually), unless the resource's asyncId is retrieved + and the sensitive API's `emitDestroy` is called with it. + **Default:** `false` Example usage: -- cgit v1.2.1