-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bun.serve does not work with unix socket #8044
Comments
Does this issue still reproduce if it's just a simple hello-world server on the unix socket? |
Yes // server.ts
import fs from "fs/promises";
const socketPath = "/tmp/lit-ssr.sock";
async function main(): Promise<void> {
// Delete the socket file if it already exists
if (await fs.exists(socketPath)) {
await fs.unlink(socketPath);
}
const [exec, ..._] = process.argv;
if (exec.indexOf("node") !== -1) {
node();
} else if (exec.indexOf("bun") !== -1) {
// node();
// FIXME: This doesn't work yet
bun();
} else {
throw Error("Runtime not supported yet");
}
}
async function node(): Promise<void> {
const net = await import("net");
const server = net.createServer((socket) => {
socket.on("data", async (data) => {
socket.write("Hello World");
});
socket.on("end", () => {});
socket.on("error", (err) => {
console.error("Socket error:", err);
});
});
server.listen(socketPath, () => {
console.log(`Server listening on unix://${socketPath}!`);
});
}
// FIXME: This doesn't work yet
async function bun(): Promise<void> {
const Bun = await import("bun");
const server = Bun.serve({
unix: socketPath,
async fetch(req) {
try {
const response = new Response("Hello World");
response.headers.set("Content-Type", "text/txt");
return response;
} catch (err) {
throw err; // Rethrow the error to prevent silent failures
}
},
});
console.log(`Server listening on unix://${socketPath}!`);
await server;
}
main(); // client.ts
import net from "net";
type Dependency = { code: string; ext: string };
async function render(timeout: number = 5000): Promise<any> {
const { promise, resolve, reject } = Promise.withResolvers<any>();
const socket = net.connect("/tmp/lit-ssr.sock");
const cancelId = setTimeout(() => {
socket.end();
reject(new Error("Timeout"));
}, timeout);
const body = "Hello";
socket.write(body);
socket.on("data", (data) => {
resolve(data);
clearTimeout(cancelId);
socket.end();
});
socket.on("error", (err) => {
reject(err);
clearTimeout(cancelId);
socket.end();
});
socket.on("close", () => {
reject(new Error("Socket closed"));
clearTimeout(cancelId);
socket.end();
});
return promise;
}
async function main(): Promise<void> {
console.log(await render(1000));
console.log("done");
}
main(); |
Also should be fine can fetch from an unix socket without using the node api |
const body = "Hello";
socket.write(body); This CLI command does not send requests to a unix domain socket, this sends to an http request localhost:4000
If we instead use oha to send http requests to the example server above, on an M1 mac we get about 220,000 requests per second: ❯ oha http://localhost:3001 -n 400000 -c 10 --unix-socket=/tmp/lit-ssr.sock -d '{"hello": 123}' -m=POST
Summary:
Success rate: 100.00%
Total: 1.7615 secs
Slowest: 0.0013 secs
Fastest: 0.0000 secs
Average: 0.0000 secs
Requests/sec: 227079.3121 That being said, the crash you ran into is clearly a bug regardless - but I am not able to reproduce it. Are you still able to reproduce it in Bun v1.0.30? Is there anything else that needs to be done to reproduce the crash? I'm going to close this as "not planned" for now since the example code doesn't reproduce the issue, but I will be happy to re-open this issue if you continue to run into it and give us an example that reproduces |
What version of Bun is running?
1.0.21+837cbd60d
What platform is your computer?
Linux 6.6.10-zen1-1-zen x86_64 unknown
What steps can reproduce the bug?
What is the expected behavior?
That my client could connect to my socket
What do you see instead?
A socket timeout
Additional information
I'm using
protobufjs
to serialize the response, but as can you see it's wrapped in a try catchWhen I ran the node implementation I got
To reproduce this error you must use wrk, if you only do a few fetch requests this error does not happen
The text was updated successfully, but these errors were encountered: