forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
async_hooks: run destroy cbs before normal exit
Run destroy callbacks before a normal application exit happens. Fixes: nodejs#13262
- Loading branch information
Showing
5 changed files
with
74 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
// Test that async ids that are added to the destroy queue while running a | ||
// `destroy` callback are handled correctly. | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const net = require('net'); | ||
const async_hooks = require('async_hooks'); | ||
|
||
const initCalls = new Set(); | ||
let destroyTcpWrapCallCount = 0; | ||
let srv2; | ||
|
||
async_hooks.createHook({ | ||
init: common.mustCallAtLeast((id, provider, triggerId) => { | ||
if (provider === 'TCPWRAP') | ||
initCalls.add(id); | ||
}, 2), | ||
destroy: common.mustCallAtLeast((id) => { | ||
if (!initCalls.has(id)) return; | ||
|
||
switch (destroyTcpWrapCallCount++) { | ||
case 0: | ||
// Trigger the second `destroy` call. | ||
srv2.close(); | ||
break; | ||
case 2: | ||
assert.fail('More than 2 destroy() invocations'); | ||
break; | ||
} | ||
}, 2) | ||
}).enable(); | ||
|
||
// Create a server to trigger the first `destroy` callback. | ||
net.createServer().listen(0).close(); | ||
srv2 = net.createServer().listen(0); | ||
|
||
process.on('exit', () => assert.strictEqual(destroyTcpWrapCallCount, 2)); |
27 changes: 27 additions & 0 deletions
27
test/parallel/test-async-hooks-top-level-clearimmediate.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict'; | ||
|
||
// Regression test for https://github.com/nodejs/node/issues/13262 | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const async_hooks = require('async_hooks'); | ||
|
||
let seenId, seenResource; | ||
|
||
async_hooks.createHook({ | ||
init: common.mustCall((id, provider, triggerId, resource) => { | ||
seenId = id; | ||
seenResource = resource; | ||
assert.strictEqual(provider, 'Immediate'); | ||
assert.strictEqual(triggerId, 1); | ||
}), | ||
before: common.mustNotCall(), | ||
after: common.mustNotCall(), | ||
destroy: common.mustCall((id) => { | ||
assert.strictEqual(seenId, id); | ||
}) | ||
}).enable(); | ||
|
||
const immediate = setImmediate(common.mustNotCall()); | ||
assert.strictEqual(immediate, seenResource); | ||
clearImmediate(immediate); |