Skip to content

Commit

Permalink
fix(net) make sure to always end the connection when destroy is called (
Browse files Browse the repository at this point in the history
  • Loading branch information
cirospaciari authored Aug 19, 2024
1 parent 1367e5e commit f9af7be
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/js/node/net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ const bunTLSConnectOptions = Symbol.for("::buntlsconnectoptions::");

const kRealListen = Symbol("kRealListen");

function closeNT(self) {
self.emit("close");
}
function endNT(socket, callback, err) {
socket.end();
callback(err);
Expand Down Expand Up @@ -642,6 +639,14 @@ const Socket = (function (InternalSocket) {
}

_destroy(err, callback) {
const socket = this[bunSocketInternal];
if (socket) {
this[bunSocketInternal] = null;
// we still have a socket, call end before destroy
process.nextTick(endNT, socket, callback, err);
return;
}
// no socket, just destroy
process.nextTick(closeNT, callback, err);
}

Expand All @@ -655,6 +660,7 @@ const Socket = (function (InternalSocket) {
this.#final_callback = callback;
} else {
// emit FIN not allowing half open
this[bunSocketInternal] = null;
process.nextTick(endNT, socket, callback);
}
}
Expand Down
14 changes: 14 additions & 0 deletions test/js/node/net/node-destroy-fixture.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions test/js/node/net/node-net.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,3 +523,35 @@ it("should not hang after FIN", async () => {
server.close();
}
});

it("should not hang after destroy", async () => {
const net = require("node:net");
const { promise: listening, resolve: resolveListening, reject } = Promise.withResolvers();
const server = net.createServer(c => {
c.write("Hello client");
});
try {
server.on("error", reject);
server.listen(0, () => {
resolveListening(server.address().port);
});
const process = Bun.spawn({
cmd: [bunExe(), join(import.meta.dir, "node-destroy-fixture.js")],
stderr: "inherit",
stdin: "ignore",
stdout: "inherit",
env: {
...bunEnv,
PORT: ((await listening) as number).toString(),
},
});
const timeout = setTimeout(() => {
process.kill();
reject(new Error("Timeout"));
}, 1000);
expect(await process.exited).toBe(0);
clearTimeout(timeout);
} finally {
server.close();
}
});

0 comments on commit f9af7be

Please sign in to comment.