-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
EADDRINUSE: Failed to start server. Is port 3000 in use? #9560
Comments
Can you check the port is already taken by another process |
Only The problem is, without the use of spawn, everything works as expected. As soon as I execute the aforementioned example, it throws the message |
You can't run multi server on same port you should use different port for each server like 3000/3001/3002 This is not a bug, this is how TCP works |
I have already tried this; still the same message about port Where should I go to change the default port for |
Here is an example // file: index.ts
const port = parseInt(Bun.argv.slice(2)[0].split("=")[1]);
const server = Bun.serve({
port: port,
fetch(request) {
return new Response("Welcome to Bun!");
},
});
console.log(`Listening on localhost:${server.port}`);
process.once("SIGINT", () => {
server.stop();
});
process.once("SIGTERM", () => {
server.stop();
}); // file: spawn.ts
import os from "node:os";
// @ts-ignore
const numCPUs = os.availableParallelism();
for (let i = 0; i < numCPUs; i++) {
Bun.spawn(["bun", "./index.ts", `--port=300${i + 1}`], {
stdio: ["inherit", "inherit", "inherit"],
env: { ...process.env },
});
} output Do not use
|
These are your sub-processes; which one is your main / parent process that gets spawned? It should be the one that starts with port 3000; there's no reason to benchmark each sub-process separately, it cannot be done this way. See how Elysia accomplishes the working output and compare it with your custom implementation, unless I'm missing something here which confuses me even further... Here's import cluster from 'node:cluster';
import http from 'node:http';
import { availableParallelism } from 'node:os';
import process from 'node:process';
const numCPUs = availableParallelism();
if (cluster.isPrimary) {
console.log(`Primary ${process.pid} is running`);
// Fork workers.
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
} else {
// Workers can share any TCP connection
// In this case it is an HTTP server
http.createServer((req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(3000); // Original example is `.listen(8000);`, I replaced it with port 3000
console.log(`Worker ${process.pid} started`);
} The execution: The actual |
Thanks to FrameworkBenchmarks I have found the cause of my problem: I should have added |
What version of Bun is running?
1.0.33+9e91e137f
What platform is your computer?
Linux debian 6.6.15-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.6.15-2 (2024-02-04) x86_64 GNU/Linux
What steps can reproduce the bug?
Code directly taken from official website works as expected
spawn.tsx
though withproduces
EADDRINUSE: Failed to start server. Is port 3000 in use?
.Is it really a bug or am I doing something wrong here?
What is the expected behavior?
To see the same output as Elysia.js that works with
spawn
:What do you see instead?
I see this
Additional information
No response
The text was updated successfully, but these errors were encountered: