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 — stream: keep async context of parked writes · nodejs/node@17ec999

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal/webstreams
    • transformstream.js
  • test/parallel
    • test-whatwg-transformstream-parked-write-async-context-legacy.js
    • test-whatwg-transformstream-parked-write-async-context.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/webstreams/transformstream.js‎

Copy file name to clipboardExpand all lines: lib/internal/webstreams/transformstream.js
+44-16Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ const {
2525
DOMException,
2626
} = internalBinding('messaging');
2727

28+
const AsyncContextFrame = require('internal/async_context_frame');
29+
const { enabledHooksExist } = require('internal/async_hooks');
30+
2831
const {
2932
customInspectSymbol: kInspect,
3033
kEmptyObject,
@@ -266,6 +269,8 @@ class TransformStreamState {
266269
pullPending = false;
267270
pendingWriteParked = false;
268271
pendingWriteChunk = undefined;
272+
pendingWriteContextFrame = undefined;
273+
pendingWriteResolve = undefined;
269274
writeContinuation = undefined;
270275
}
271276
ObjectSetPrototypeOf(TransformStreamState.prototype, null);
@@ -466,7 +471,13 @@ function transformStreamSetBackpressure(stream, backpressure) {
466471
state.readable[kState].controller[kState].pullFulfilled);
467472
}
468473
} else if (state.pendingWriteParked) {
469-
PromisePrototypeThen(kResolvedPromise, state.writeContinuation);
474+
const resolve = state.pendingWriteResolve;
475+
if (resolve !== undefined) {
476+
state.pendingWriteResolve = undefined;
477+
resolve();
478+
} else {
479+
PromisePrototypeThen(kResolvedPromise, state.writeContinuation);
480+
}
470481
}
471482
}
472483

@@ -618,31 +629,48 @@ function transformStreamDefaultSinkWriteAlgorithm(stream, chunk) {
618629
// them before invoking the write algorithm), replacing the promise
619630
// record the old code allocated and resolved per parked chunk. The
620631
// writable dispatches a single write at a time, so one pending slot
621-
// suffices.
632+
// suffices. The old reaction was registered here, so the continuation
633+
// runs in the async context captured at park time rather than in the
634+
// context of the read that lifts the backpressure.
622635
assert(!state.pendingWriteParked);
623636
state.pendingWriteParked = true;
624637
state.pendingWriteChunk = chunk;
638+
state.pendingWriteContextFrame = AsyncContextFrame.current();
625639
state.writeContinuation ??= () => {
626640
const pendingChunk = state.pendingWriteChunk;
641+
const prior = AsyncContextFrame.exchange(state.pendingWriteContextFrame);
627642
state.pendingWriteParked = false;
628643
state.pendingWriteChunk = undefined;
629-
const writableState = state.writable[kState];
630-
const writableControllerState = writableState.controller[kState];
631-
if (writableState.state === 'erroring') {
632-
const error = writableState.storedError;
644+
state.pendingWriteContextFrame = undefined;
645+
try {
646+
const writableState = state.writable[kState];
647+
const writableControllerState = writableState.controller[kState];
648+
if (writableState.state === 'erroring') {
649+
const error = writableState.storedError;
650+
PromisePrototypeThen(
651+
kResolvedPromise,
652+
() => writableControllerState.writeRejected(error));
653+
return;
654+
}
655+
assert(writableState.state === 'writable');
633656
PromisePrototypeThen(
634-
kResolvedPromise,
635-
() => writableControllerState.writeRejected(error));
636-
return;
657+
transformStreamDefaultControllerPerformTransform(
658+
controller,
659+
pendingChunk),
660+
writableControllerState.writeFulfilled,
661+
writableControllerState.writeRejected);
662+
} finally {
663+
AsyncContextFrame.set(prior);
637664
}
638-
assert(writableState.state === 'writable');
639-
PromisePrototypeThen(
640-
transformStreamDefaultControllerPerformTransform(
641-
controller,
642-
pendingChunk),
643-
writableControllerState.writeFulfilled,
644-
writableControllerState.writeRejected);
645665
};
666+
if (enabledHooksExist()) {
667+
// Async hooks (and the async_hooks-based AsyncLocalStorage) track
668+
// context through promise reactions, so register the continuation
669+
// now, in the writer's context, and resolve it on unpark.
670+
const { promise, resolve } = PromiseWithResolvers();
671+
state.pendingWriteResolve = resolve;
672+
PromisePrototypeThen(promise, state.writeContinuation);
673+
}
646674
return kParkedAlgorithmResult;
647675
}
648676
return transformStreamDefaultControllerPerformTransform(controller, chunk);
Collapse file

‎test/parallel/test-whatwg-transformstream-parked-write-async-context-legacy.js‎

Copy file name to clipboard
+60Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Flags: --no-async-context-frame
2+
'use strict';
3+
4+
// A write parked on backpressure must run transform() and the writes queued
5+
// behind it in the async context of the writer, not in the context of the
6+
// read that lifts the backpressure.
7+
// This variant covers the async_hooks-based AsyncLocalStorage.
8+
// Refs: https://github.com/nodejs/node/issues/66906
9+
10+
const common = require('../common');
11+
const assert = require('assert');
12+
const { AsyncLocalStorage } = require('async_hooks');
13+
const { setImmediate } = require('timers/promises');
14+
15+
const als = new AsyncLocalStorage();
16+
17+
function createPipeline(seen) {
18+
return new ReadableStream({
19+
start(controller) {
20+
for (const chunk of 'abc') controller.enqueue(chunk);
21+
controller.close();
22+
},
23+
}).pipeThrough(new TransformStream({
24+
transform(chunk, controller) {
25+
seen.push(`${chunk}:${als.getStore() ?? 'none'}`);
26+
controller.enqueue(chunk);
27+
},
28+
flush() {
29+
seen.push(`flush:${als.getStore() ?? 'none'}`);
30+
},
31+
}));
32+
}
33+
34+
async function consume(readable) {
35+
// eslint-disable-next-line no-unused-vars
36+
for await (const _ of readable);
37+
}
38+
39+
(async () => {
40+
{
41+
const seen = [];
42+
const readable = als.run('writer', () => createPipeline(seen));
43+
// Let the pipe park its first write on backpressure.
44+
await setImmediate();
45+
await consume(readable);
46+
assert.deepStrictEqual(seen, [
47+
'a:writer', 'b:writer', 'c:writer', 'flush:writer',
48+
]);
49+
}
50+
51+
{
52+
const seen = [];
53+
const readable = createPipeline(seen);
54+
await setImmediate();
55+
await als.run('reader', () => consume(readable));
56+
assert.deepStrictEqual(seen, [
57+
'a:none', 'b:none', 'c:none', 'flush:none',
58+
]);
59+
}
60+
})().then(common.mustCall());
Collapse file

‎test/parallel/test-whatwg-transformstream-parked-write-async-context.js‎

Copy file name to clipboard
+58Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'use strict';
2+
3+
// A write parked on backpressure must run transform() and the writes queued
4+
// behind it in the async context of the writer, not in the context of the
5+
// read that lifts the backpressure.
6+
// Refs: https://github.com/nodejs/node/issues/66906
7+
8+
const common = require('../common');
9+
const assert = require('assert');
10+
const { AsyncLocalStorage } = require('async_hooks');
11+
const { setImmediate } = require('timers/promises');
12+
13+
const als = new AsyncLocalStorage();
14+
15+
function createPipeline(seen) {
16+
return new ReadableStream({
17+
start(controller) {
18+
for (const chunk of 'abc') controller.enqueue(chunk);
19+
controller.close();
20+
},
21+
}).pipeThrough(new TransformStream({
22+
transform(chunk, controller) {
23+
seen.push(`${chunk}:${als.getStore() ?? 'none'}`);
24+
controller.enqueue(chunk);
25+
},
26+
flush() {
27+
seen.push(`flush:${als.getStore() ?? 'none'}`);
28+
},
29+
}));
30+
}
31+
32+
async function consume(readable) {
33+
// eslint-disable-next-line no-unused-vars
34+
for await (const _ of readable);
35+
}
36+
37+
(async () => {
38+
{
39+
const seen = [];
40+
const readable = als.run('writer', () => createPipeline(seen));
41+
// Let the pipe park its first write on backpressure.
42+
await setImmediate();
43+
await consume(readable);
44+
assert.deepStrictEqual(seen, [
45+
'a:writer', 'b:writer', 'c:writer', 'flush:writer',
46+
]);
47+
}
48+
49+
{
50+
const seen = [];
51+
const readable = createPipeline(seen);
52+
await setImmediate();
53+
await als.run('reader', () => consume(readable));
54+
assert.deepStrictEqual(seen, [
55+
'a:none', 'b:none', 'c:none', 'flush:none',
56+
]);
57+
}
58+
})().then(common.mustCall());

0 commit comments

Comments
0 (0)
You are visitor 01402775 — 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.