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
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 assert = require('assert');
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 [
readResult,
cancelResult,
lateWriteResult,
] = await Promise.allSettled([
pendingRead,
pendingCancel,
pendingLateWrite,
]);

assert.strictEqual(readResult.status, 'fulfilled');
assert.strictEqual(cancelResult.status, 'fulfilled');
if (lateWriteResult.status === 'rejected') {
const err = lateWriteResult.reason;
const isNotAFunction = err instanceof TypeError &&
/transformAlgorithm is not a function/.test(err.message);
assert.ok(!isNotAFunction,
`Internal implementation error leaked: ${err.message}`);
}
}

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