Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
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') {
// 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
55 changes: 55 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,55 @@
'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.

async function test() {
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.
// 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