From 323306b8a5f6b485f2ff69af1953f77ceb8ab273 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sun, 14 Nov 2021 17:09:49 +0530 Subject: [PATCH] process: add `getActiveResourcesInfo()` This is supposed to be a public alternative of the private APIs, `process._getActiveResources()` and `process._getActiveHandles()`. When called, it returns an array of objects containing the `type`, `asyncId` and the `triggerAsyncId` of the currently active `requests`, `handles` and `timers`. Signed-off-by: Darshan Sen --- doc/api/process.md | 69 +++++++++++++++++++ lib/internal/bootstrap/node.js | 46 ++++++++++++- lib/internal/timers.js | 11 +-- src/node_process_methods.cc | 57 +++++++++++++++ test/parallel/test-handle-wrap-isrefed.js | 26 +++++++ ...getactiveresources-track-active-handles.js | 44 ++++++++++++ ...etactiveresources-track-active-requests.js | 10 +++ .../test-process-getactiveresources.js | 20 ++++++ .../test-net-connect-econnrefused.js | 5 +- 9 files changed, 278 insertions(+), 10 deletions(-) create mode 100644 test/parallel/test-process-getactiveresources-track-active-handles.js create mode 100644 test/parallel/test-process-getactiveresources-track-active-requests.js create mode 100644 test/parallel/test-process-getactiveresources.js diff --git a/doc/api/process.md b/doc/api/process.md index 28db286bd90fd2..5d042977f96307 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -1817,6 +1817,71 @@ a code. Specifying a code to [`process.exit(code)`][`process.exit()`] will override any previous setting of `process.exitCode`. +## `process.getActiveResourcesInfo()` + + + +> Stability: 1 - Experimental + +* Returns: {Array} of objects containing the `type`, `asyncId` and the + `triggerAsyncId` of the currently active requests, handles and timers. + +The `process.getActiveResourcesInfo()` method can be called at any moment and it +would return an array of objects containing some information about all the +resources that are currently keeping the event loop alive. This can be useful +while debugging. + +The same functionality can be implemented using [`async_hooks`][] by setting up +an [`AsyncHook`][] that keeps a track of the calls made to [`init` callbacks][] +and [`destroy` callbacks][] but this function should be preferred because it +doesn't incur the overhead of creating any [`AsyncHook`][]. + +```mjs +import { getActiveResourcesInfo } from 'process'; +import { setTimeout } from 'timers'; + +console.log('Before:', getActiveResourcesInfo()); +setTimeout(() => {}, 1000); +console.log('After:', getActiveResourcesInfo()); +// Prints: +// Before: [ +// { type: 'CloseReq', asyncId: 6, triggerAsyncId: 0 }, +// { type: 'TTYWrap', asyncId: 7, triggerAsyncId: 0 }, +// { type: 'TTYWrap', asyncId: 9, triggerAsyncId: 0 }, +// { type: 'TTYWrap', asyncId: 10, triggerAsyncId: 0 } +// ] +// After: [ +// { type: 'CloseReq', asyncId: 6, triggerAsyncId: 0 }, +// { type: 'TTYWrap', asyncId: 7, triggerAsyncId: 0 }, +// { type: 'TTYWrap', asyncId: 9, triggerAsyncId: 0 }, +// { type: 'TTYWrap', asyncId: 10, triggerAsyncId: 0 }, +// { type: 'Timeout', asyncId: 12, triggerAsyncId: 0 } +// ] +``` + +```cjs +const { getActiveResourcesInfo } = require('process'); +const { setTimeout } = require('timers'); + +console.log('Before:', getActiveResourcesInfo()); +setTimeout(() => {}, 1000); +console.log('After:', getActiveResourcesInfo()); +// Prints: +// Before: [ +// { type: 'TTYWrap', asyncId: 2, triggerAsyncId: 1 }, +// { type: 'TTYWrap', asyncId: 4, triggerAsyncId: 1 }, +// { type: 'TTYWrap', asyncId: 5, triggerAsyncId: 1 } +// ] +// After: [ +// { type: 'TTYWrap', asyncId: 2, triggerAsyncId: 1 }, +// { type: 'TTYWrap', asyncId: 4, triggerAsyncId: 1 }, +// { type: 'TTYWrap', asyncId: 5, triggerAsyncId: 1 }, +// { type: 'Timeout', asyncId: 7, triggerAsyncId: 1 } +// ] +``` + ## `process.getegid()`