-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
async_hooks: fix leak in AsyncLocalStorage exit
If exit is called and then run or enterWith are called within the exit function, the als instace should not be added to the storageList additional times. The correct behaviour is to remove the instance from the storageList before executing the exit handler and then to restore it after. PR-URL: #35779 Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com> Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de> Reviewed-By: Andrey Pechkurov <apechkurov@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
- Loading branch information
Showing
2 changed files
with
36 additions
and
7 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
25 changes: 25 additions & 0 deletions
25
test/parallel/test-async-local-storage-exit-does-not-leak.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,25 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { AsyncLocalStorage } = require('async_hooks'); | ||
|
||
const als = new AsyncLocalStorage(); | ||
|
||
// Make sure _propagate function exists. | ||
assert.ok(typeof als._propagate === 'function'); | ||
|
||
// The als instance should be getting removed from the storageList in | ||
// lib/async_hooks.js when exit(...) is called, therefore when the nested runs | ||
// are called there should be no copy of the als in the storageList to run the | ||
// _propagate method on. | ||
als._propagate = common.mustNotCall('_propagate() should not be called'); | ||
|
||
const done = common.mustCall(); | ||
|
||
function run(count) { | ||
if (count === 0) return done(); | ||
als.run({}, () => { | ||
als.exit(run, --count); | ||
}); | ||
} | ||
run(100); |