diff --git a/lib/ours/errors.js b/lib/ours/errors.js index 979957e3c..87538a529 100644 --- a/lib/ours/errors.js +++ b/lib/ours/errors.js @@ -94,7 +94,19 @@ function E(code, message, Base) { }) NodeError.prototype.code = code NodeError.prototype[kIsNodeError] = true - codes[code] = NodeError + // Node 18 calls `ERR_MULTIPLE_CALLBACK()` from `_final` with no `new`. + // A class throws TypeError before this body runs, and that throw escapes + // a callback scheduled with `nextTick`, so the process dies instead of + // emitting ERR_MULTIPLE_CALLBACK. The function builds the same instance + // either way. `new` still returns it because a constructor that returns + // an object uses that object. + function createNodeError(...args) { + return new NodeError(...args) + } + Object.setPrototypeOf(createNodeError, NodeError) + createNodeError.prototype = NodeError.prototype + + codes[code] = createNodeError } function hideStackFrames(fn) { // We rename the functions that will be hidden to cut off the stacktrace diff --git a/src/errors.js b/src/errors.js index 4c391da26..b701370a1 100644 --- a/src/errors.js +++ b/src/errors.js @@ -106,7 +106,19 @@ function E(code, message, Base) { NodeError.prototype.code = code NodeError.prototype[kIsNodeError] = true - codes[code] = NodeError + // Node 18 calls `ERR_MULTIPLE_CALLBACK()` from `_final` with no `new`. + // A class throws TypeError before this body runs, and that throw escapes + // a callback scheduled with `nextTick`, so the process dies instead of + // emitting ERR_MULTIPLE_CALLBACK. The function builds the same instance + // either way. `new` still returns it because a constructor that returns + // an object uses that object. + function createNodeError(...args) { + return new NodeError(...args) + } + Object.setPrototypeOf(createNodeError, NodeError) + createNodeError.prototype = NodeError.prototype + + codes[code] = createNodeError } function hideStackFrames(fn) { diff --git a/src/test/ours/test-errors.js b/src/test/ours/test-errors.js index a300f1707..16cd7286a 100644 --- a/src/test/ours/test-errors.js +++ b/src/test/ours/test-errors.js @@ -11,7 +11,7 @@ function checkError(err, Base, name, code, message) { } // Update this numbers based on the number of checkError below multiplied by the assertions within checkError -t.plan(17 * 4) +t.plan(19 * 4 + 1) checkError( new errors.ERR_INVALID_ARG_VALUE('name', 0), @@ -102,6 +102,16 @@ checkError( 'ERR_MULTIPLE_CALLBACK', 'Callback called multiple times' ) +const multipleCallback = errors.ERR_MULTIPLE_CALLBACK() +checkError(multipleCallback, Error, 'Error', 'ERR_MULTIPLE_CALLBACK', 'Callback called multiple times') +t.ok(multipleCallback instanceof errors.ERR_MULTIPLE_CALLBACK) +checkError( + errors.ERR_INVALID_ARG_VALUE('name', 0), + TypeError, + 'TypeError', + 'ERR_INVALID_ARG_VALUE', + "The argument 'name' is invalid. Received 0" +) checkError(new errors.ERR_STREAM_CANNOT_PIPE(), Error, 'Error', 'ERR_STREAM_CANNOT_PIPE', 'Cannot pipe, not readable') diff --git a/src/test/ours/test-stream-writable-final-cb-twice.js b/src/test/ours/test-stream-writable-final-cb-twice.js new file mode 100644 index 000000000..9b547fb6b --- /dev/null +++ b/src/test/ours/test-stream-writable-final-cb-twice.js @@ -0,0 +1,43 @@ +'use strict' + +const t = require('tap') +const { Writable } = require('../../lib/ours/index') + +t.test('a second synchronous _final callback emits ERR_MULTIPLE_CALLBACK', (t) => { + t.plan(2) + const writable = new Writable({ + write(buf, enc, cb) { + cb() + }, + final(cb) { + cb() + cb() + } + }) + writable.on('error', (err) => { + t.equal(err.code, 'ERR_MULTIPLE_CALLBACK') + t.equal(err.name, 'Error') + }) + writable.end('hi') +}) + +t.test('a second _final callback on nextTick does not crash', (t) => { + t.plan(1) + const writable = new Writable({ + write(buf, enc, cb) { + cb() + }, + final(cb) { + cb() + process.nextTick(() => { + // finish already destroyed the stream, as in Node core. + // The bug was the TypeError thrown before that check. + t.doesNotThrow(() => cb()) + }) + } + }) + writable.on('error', (err) => { + t.fail(err.message) + }) + writable.end('hi') +}) diff --git a/test/ours/test-errors.js b/test/ours/test-errors.js index 4e0f898fc..51d538999 100644 --- a/test/ours/test-errors.js +++ b/test/ours/test-errors.js @@ -10,7 +10,7 @@ function checkError(err, Base, name, code, message) { } // Update this numbers based on the number of checkError below multiplied by the assertions within checkError -t.plan(17 * 4) +t.plan(19 * 4 + 1) checkError( new errors.ERR_INVALID_ARG_VALUE('name', 0), TypeError, @@ -89,6 +89,16 @@ checkError( 'ERR_MULTIPLE_CALLBACK', 'Callback called multiple times' ) +const multipleCallback = errors.ERR_MULTIPLE_CALLBACK() +checkError(multipleCallback, Error, 'Error', 'ERR_MULTIPLE_CALLBACK', 'Callback called multiple times') +t.ok(multipleCallback instanceof errors.ERR_MULTIPLE_CALLBACK) +checkError( + errors.ERR_INVALID_ARG_VALUE('name', 0), + TypeError, + 'TypeError', + 'ERR_INVALID_ARG_VALUE', + "The argument 'name' is invalid. Received 0" +) checkError(new errors.ERR_STREAM_CANNOT_PIPE(), Error, 'Error', 'ERR_STREAM_CANNOT_PIPE', 'Cannot pipe, not readable') checkError(new errors.ERR_STREAM_WRITE_AFTER_END(), Error, 'Error', 'ERR_STREAM_WRITE_AFTER_END', 'write after end') checkError( diff --git a/test/ours/test-stream-writable-final-cb-twice.js b/test/ours/test-stream-writable-final-cb-twice.js new file mode 100644 index 000000000..9b547fb6b --- /dev/null +++ b/test/ours/test-stream-writable-final-cb-twice.js @@ -0,0 +1,43 @@ +'use strict' + +const t = require('tap') +const { Writable } = require('../../lib/ours/index') + +t.test('a second synchronous _final callback emits ERR_MULTIPLE_CALLBACK', (t) => { + t.plan(2) + const writable = new Writable({ + write(buf, enc, cb) { + cb() + }, + final(cb) { + cb() + cb() + } + }) + writable.on('error', (err) => { + t.equal(err.code, 'ERR_MULTIPLE_CALLBACK') + t.equal(err.name, 'Error') + }) + writable.end('hi') +}) + +t.test('a second _final callback on nextTick does not crash', (t) => { + t.plan(1) + const writable = new Writable({ + write(buf, enc, cb) { + cb() + }, + final(cb) { + cb() + process.nextTick(() => { + // finish already destroyed the stream, as in Node core. + // The bug was the TypeError thrown before that check. + t.doesNotThrow(() => cb()) + }) + } + }) + writable.on('error', (err) => { + t.fail(err.message) + }) + writable.end('hi') +})