GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — async_hooks: ensure AsyncLocalStore instances work isolated · nodejs/node@b33e8d2

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal/async_local_storage
    • async_context_frame.js
  • test
    • async-hooks
      • test-async-local-storage-gcable.js
    • parallel
      • test-async-local-storage-isolation.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/async_local_storage/async_context_frame.js‎

Copy file name to clipboardExpand all lines: lib/internal/async_local_storage/async_context_frame.js
+6-2Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const {
4+
ObjectIs,
45
ReflectApply,
56
} = primordials;
67

@@ -53,12 +54,15 @@ class AsyncLocalStorage {
5354
}
5455

5556
run(data, fn, ...args) {
56-
const prior = AsyncContextFrame.current();
57+
const prior = this.getStore();
58+
if (ObjectIs(prior, data)) {
59+
return ReflectApply(fn, null, args);
60+
}
5761
this.enterWith(data);
5862
try {
5963
return ReflectApply(fn, null, args);
6064
} finally {
61-
AsyncContextFrame.set(prior);
65+
this.enterWith(prior);
6266
}
6367
}
6468

Collapse file

‎test/async-hooks/test-async-local-storage-gcable.js‎

Copy file name to clipboardExpand all lines: test/async-hooks/test-async-local-storage-gcable.js
+8-1Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
'use strict';
2-
// Flags: --expose_gc
2+
// Flags: --expose_gc --expose-internals
33

44
// This test ensures that AsyncLocalStorage gets gced once it was disabled
55
// and no strong references remain in userland.
66

77
const common = require('../common');
88
const { AsyncLocalStorage } = require('async_hooks');
9+
const AsyncContextFrame = require('internal/async_context_frame');
910
const { onGC } = require('../common/gc');
1011

1112
let asyncLocalStorage = new AsyncLocalStorage();
@@ -16,5 +17,11 @@ asyncLocalStorage.run({}, () => {
1617
onGC(asyncLocalStorage, { ongc: common.mustCall() });
1718
});
1819

20+
if (AsyncContextFrame.enabled) {
21+
// This disable() is needed to remove reference form AsyncContextFrame
22+
// created during exit of run() to the AsyncLocalStore instance.
23+
asyncLocalStorage.disable();
24+
}
25+
1926
asyncLocalStorage = null;
2027
global.gc();
Collapse file

‎test/parallel/test-async-local-storage-isolation.js‎

Copy file name to clipboard
+67Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
'use strict';
2+
const common = require('../common');
3+
const { AsyncLocalStorage } = require('node:async_hooks');
4+
const assert = require('node:assert');
5+
6+
// Verify that ALS instances are independent of each other.
7+
8+
{
9+
// Verify als2.enterWith() and als2.run inside als1.run()
10+
const als1 = new AsyncLocalStorage();
11+
const als2 = new AsyncLocalStorage();
12+
13+
assert.strictEqual(als1.getStore(), undefined);
14+
assert.strictEqual(als2.getStore(), undefined);
15+
16+
als1.run('store1', common.mustCall(() => {
17+
assert.strictEqual(als1.getStore(), 'store1');
18+
assert.strictEqual(als2.getStore(), undefined);
19+
20+
als2.run('store2', common.mustCall(() => {
21+
assert.strictEqual(als1.getStore(), 'store1');
22+
assert.strictEqual(als2.getStore(), 'store2');
23+
}));
24+
assert.strictEqual(als1.getStore(), 'store1');
25+
assert.strictEqual(als2.getStore(), undefined);
26+
27+
als2.enterWith('store3');
28+
assert.strictEqual(als1.getStore(), 'store1');
29+
assert.strictEqual(als2.getStore(), 'store3');
30+
}));
31+
32+
assert.strictEqual(als1.getStore(), undefined);
33+
assert.strictEqual(als2.getStore(), 'store3');
34+
}
35+
36+
{
37+
// Verify als1.disable() has no side effects to als2 and als3
38+
const als1 = new AsyncLocalStorage();
39+
const als2 = new AsyncLocalStorage();
40+
const als3 = new AsyncLocalStorage();
41+
42+
als3.enterWith('store3');
43+
44+
als1.run('store1', common.mustCall(() => {
45+
assert.strictEqual(als1.getStore(), 'store1');
46+
assert.strictEqual(als2.getStore(), undefined);
47+
assert.strictEqual(als3.getStore(), 'store3');
48+
49+
als2.run('store2', common.mustCall(() => {
50+
assert.strictEqual(als1.getStore(), 'store1');
51+
assert.strictEqual(als2.getStore(), 'store2');
52+
assert.strictEqual(als3.getStore(), 'store3');
53+
54+
als1.disable();
55+
assert.strictEqual(als1.getStore(), undefined);
56+
assert.strictEqual(als2.getStore(), 'store2');
57+
assert.strictEqual(als3.getStore(), 'store3');
58+
}));
59+
assert.strictEqual(als1.getStore(), undefined);
60+
assert.strictEqual(als2.getStore(), undefined);
61+
assert.strictEqual(als3.getStore(), 'store3');
62+
}));
63+
64+
assert.strictEqual(als1.getStore(), undefined);
65+
assert.strictEqual(als2.getStore(), undefined);
66+
assert.strictEqual(als3.getStore(), 'store3');
67+
}

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.