Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
stream: fix TransformStream race on cancel with pending write
Signed-off-by: marcopiraccini <marco.piraccini@gmail.com>
  • Loading branch information
marcopiraccini committed Feb 28, 2026
commit 2449435053b8eb4ab9d68ce76cddb1994cee74af
8 changes: 6 additions & 2 deletions lib/internal/webstreams/transformstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ const {
const assert = require('internal/assert');

const kSkipThrow = Symbol('kSkipThrow');

const getNonWritablePropertyDescriptor = (value) => {
return {
__proto__: null,
Expand Down Expand Up @@ -524,7 +523,12 @@ function transformStreamDefaultControllerError(controller, error) {

async function transformStreamDefaultControllerPerformTransform(controller, chunk) {
try {
return await controller[kState].transformAlgorithm(chunk, controller);
const transformAlgorithm = controller[kState].transformAlgorithm;
if (typeof transformAlgorithm !== 'function') {
Comment thread
MattiasBuelens marked this conversation as resolved.
Resolved
// Algorithms were cleared by a concurrent cancel/abort/close.
return;
}
return await transformAlgorithm(chunk, controller);
} catch (error) {
transformStreamError(controller[kState].stream, error);
throw error;
Expand Down
54 changes: 54 additions & 0 deletions test/parallel/test-whatwg-transformstream-cancel-write-race.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';

const common = require('../common');
const { TransformStream } = require('stream/web');
const { setTimeout } = require('timers/promises');

// Test for https://github.com/nodejs/node/issues/62036
// A late write racing with reader.cancel() should not throw an internal "transformAlgorithm is not a function" TypeError.

Check failure on line 8 in test/parallel/test-whatwg-transformstream-cancel-write-race.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

This line has a length of 122. Maximum allowed is 120

async function test() {
Comment thread
MattiasBuelens marked this conversation as resolved.
Resolved
const stream = new TransformStream({
transform(chunk, controller) {
controller.enqueue(chunk);
},
});

await setTimeout(0);

const reader = stream.readable.getReader();
const writer = stream.writable.getWriter();

// Release backpressure.
const pendingRead = reader.read();

// Simulate client disconnect / shutdown.
const pendingCancel = reader.cancel(new Error('client disconnected'));

// Late write racing with cancel.
const pendingLateWrite = writer.write('late-write');

const results = await Promise.allSettled([
pendingRead,
pendingCancel,
pendingLateWrite,
]);

// pendingRead should fulfill (with done:true or a value).
// pendingCancel should fulfill.
Comment thread
MattiasBuelens marked this conversation as resolved.
Resolved
// pendingLateWrite may reject, but must NOT reject with an internal
// TypeError about transformAlgorithm not being a function.
for (const result of results) {
if (result.status === 'rejected') {
const err = result.reason;
if (err instanceof TypeError &&
/transformAlgorithm is not a function/.test(err.message)) {
throw new Error(
'Internal implementation error leaked: ' + err.message
);
}
}
}
}

test().then(common.mustCall());
Loading