Skip to content
Merged
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
10 changes: 9 additions & 1 deletion src/js/node/dgram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -702,6 +705,7 @@ Socket.prototype.close = function (callback) {
return this;
}

healthCheck(this);
state.receiving = false;
state.handle.socket?.close();
state.handle = null;
Expand All @@ -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);
Expand All @@ -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;

Expand Down
71 changes: 71 additions & 0 deletions test/js/bun/udp/dgram.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>();
socket.bind(0, onListening);
await listening;
const port = socket.address().port;
const { promise: closed, resolve: onClose } = Promise.withResolvers<void>();
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<void>();
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();
});
});