From c76d7cd4172ac3676361cb7fe9c6f0ef2abfc527 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sun, 28 Jun 2026 21:57:15 +0000 Subject: [PATCH 1/2] node:dgram: throw ERR_SOCKET_DGRAM_NOT_RUNNING from socket methods after close() close() nulls the internal handle, so address(), remoteAddress(), send(), a second close() and bind() on a closed socket dereferenced null and threw an internal TypeError with no code. Call the existing healthCheck() guard from each of those methods, at the same points Node's lib/dgram.js does, and make Symbol.asyncDispose check the handle rather than handle.socket so disposing an already-closed socket resolves instead of throwing. --- src/js/node/dgram.ts | 10 ++++- test/js/bun/udp/dgram.test.ts | 71 +++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/src/js/node/dgram.ts b/src/js/node/dgram.ts index 98aa19a0e984..a239f0cb78fc 100644 --- a/src/js/node/dgram.ts +++ b/src/js/node/dgram.ts @@ -219,6 +219,7 @@ function bufferSize(self, size, _buffer) { Socket.prototype.bind = function (port_, address_ /* , callback */) { let port = port_; + healthCheck(this); const state = this[kStateSymbol]; if (state.bindState !== BIND_STATE_UNBOUND) { @@ -578,6 +579,8 @@ Socket.prototype.send = function (buffer, offset, length, port, address, callbac validateString(address, "address"); } + healthCheck(this); + if (state.bindState === BIND_STATE_UNBOUND) this.bind({ port: 0, exclusive: true }, null); if (list.length === 0) list.push(Buffer.alloc(0)); @@ -702,6 +705,7 @@ Socket.prototype.close = function (callback) { return this; } + healthCheck(this); state.receiving = false; state.handle.socket?.close(); state.handle = null; @@ -711,7 +715,7 @@ Socket.prototype.close = function (callback) { }; Socket.prototype[SymbolAsyncDispose] = async function () { - if (!this[kStateSymbol].handle.socket) { + if (!this[kStateSymbol].handle) { return; } const { promise, resolve, reject } = $newPromiseCapability(Promise); @@ -731,12 +735,16 @@ function socketCloseNT(self) { } Socket.prototype.address = function () { + healthCheck(this); + const addr = this[kStateSymbol].handle.socket?.address; if (!addr) throw $ERR_SOCKET_DGRAM_NOT_RUNNING(); return addr; }; Socket.prototype.remoteAddress = function () { + healthCheck(this); + const state = this[kStateSymbol]; const socket = state.handle.socket; diff --git a/test/js/bun/udp/dgram.test.ts b/test/js/bun/udp/dgram.test.ts index ddc2298510b0..f033d8190972 100644 --- a/test/js/bun/udp/dgram.test.ts +++ b/test/js/bun/udp/dgram.test.ts @@ -206,3 +206,74 @@ describe("unref()", () => { expect([path.join(import.meta.dir, "dgram-unref-hang-fixture.ts")]).toRun(); }); }); + +describe("after close()", () => { + // Node throws ERR_SOCKET_DGRAM_NOT_RUNNING from these methods once the + // socket is closed. They must not surface an internal TypeError instead. + async function boundThenClosed() { + const socket = createSocket("udp4"); + const { promise: listening, resolve: onListening } = Promise.withResolvers(); + socket.bind(0, onListening); + await listening; + const port = socket.address().port; + const { promise: closed, resolve: onClose } = Promise.withResolvers(); + socket.close(onClose); + await closed; + return { socket, port }; + } + + test("address() throws ERR_SOCKET_DGRAM_NOT_RUNNING", async () => { + const { socket } = await boundThenClosed(); + let err: any; + try { + socket.address(); + } catch (e) { + err = e; + } + expect({ name: err?.name, code: err?.code }).toEqual({ name: "Error", code: "ERR_SOCKET_DGRAM_NOT_RUNNING" }); + }); + + test("remoteAddress() throws ERR_SOCKET_DGRAM_NOT_RUNNING", async () => { + const { socket } = await boundThenClosed(); + expect(() => socket.remoteAddress()).toThrowWithCode(Error, "ERR_SOCKET_DGRAM_NOT_RUNNING"); + }); + + test("send() throws ERR_SOCKET_DGRAM_NOT_RUNNING", async () => { + const { socket, port } = await boundThenClosed(); + expect(() => socket.send(Buffer.from("hello"), port, "127.0.0.1")).toThrowWithCode( + Error, + "ERR_SOCKET_DGRAM_NOT_RUNNING", + ); + }); + + test("send() with a callback throws ERR_SOCKET_DGRAM_NOT_RUNNING synchronously", async () => { + const { socket, port } = await boundThenClosed(); + expect(() => socket.send(Buffer.from("hello"), port, "127.0.0.1", () => {})).toThrowWithCode( + Error, + "ERR_SOCKET_DGRAM_NOT_RUNNING", + ); + }); + + test("close() throws ERR_SOCKET_DGRAM_NOT_RUNNING", async () => { + const { socket } = await boundThenClosed(); + expect(() => socket.close()).toThrowWithCode(Error, "ERR_SOCKET_DGRAM_NOT_RUNNING"); + }); + + test("bind() throws ERR_SOCKET_DGRAM_NOT_RUNNING", async () => { + const { socket } = await boundThenClosed(); + expect(() => socket.bind(0)).toThrowWithCode(Error, "ERR_SOCKET_DGRAM_NOT_RUNNING"); + }); + + test("close() of a never-bound socket can only be called once", async () => { + const socket = createSocket("udp4"); + const { promise: closed, resolve: onClose } = Promise.withResolvers(); + socket.close(onClose); + await closed; + expect(() => socket.close()).toThrowWithCode(Error, "ERR_SOCKET_DGRAM_NOT_RUNNING"); + }); + + test("Symbol.asyncDispose resolves when the socket is already closed", async () => { + const { socket } = await boundThenClosed(); + expect(await socket[Symbol.asyncDispose]()).toBeUndefined(); + }); +}); From ca736040dbf5956eaabebf34d9e79795f2466f06 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sun, 28 Jun 2026 23:06:20 +0000 Subject: [PATCH 2/2] ci: retrigger