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 incorrectly reports "port in use" inside docker container #5315
Comments
less repro index.js const net = require('net');
const server = new net.Server();
server.on('error', err => {
console.log(err);
server.close();
});
server.listen(3000, 'localhost', () => {
port = server.address();
server.close();
console.log(port);
}); docker run -it -v ./index.js:/tmp/index.js -w /tmp debian:sid-slim
apt update && apt install -y curl unzip
curl -fsSL https://bun.sh/install | bash
~/.bun/bin/bun index.js
|
I suspect the issue might be related to the implementation of usocket, but I cannot be completely sure at this moment. It could be a general issue, but there haven't been any issues reported in the usocket repository before. The problem seems to be related to ipv6 configuration in the container's
Below are the results of my system call tracing bun/packages/bun-usockets/src/bsd.c Lines 443 to 461 in 92e95c8
We can also reproduce this issue using a C program. It lists the results of #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
int main() {
struct addrinfo hints, *result, *rp;
int status, sockfd;
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_PASSIVE;
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if ((status = getaddrinfo("localhost", "3000", &hints, &result)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
exit(EXIT_FAILURE);
}
for (rp = result; rp != NULL; rp = rp->ai_next) {
void *addr;
char ipstr[INET6_ADDRSTRLEN];
if (rp->ai_family == AF_INET) {
struct sockaddr_in *ipv4 = (struct sockaddr_in *)rp->ai_addr;
addr = &(ipv4->sin_addr);
} else {
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)rp->ai_addr;
addr = &(ipv6->sin6_addr);
}
if (inet_ntop(rp->ai_family, addr, ipstr, sizeof(ipstr)) != NULL) {
printf("Find IP Address: %s\n", ipstr);
if (rp->ai_family == AF_INET6) {
printf("Try to bind %s\n", ipstr);
sockfd = socket(rp->ai_family, SOCK_STREAM, rp->ai_protocol);
if (sockfd == -1) {
perror("socket");
continue;
}
if (bind(sockfd, rp->ai_addr, rp->ai_addrlen) == -1) {
perror("bind");
} else {
printf("Bind %s 3000 successfully.\n", ipstr);
}
close(sockfd);
}
} else {
perror("inet_ntop");
}
}
freeaddrinfo(result);
return 0;
}
Output (gcc):
|
Removing |
But |
Yes, we will fix this. |
Any updates on this one? I run Bun in a lot of containers using Bun.serve, but currently I can't use them because of this (the /etc/hosts file is more or less virtual in containers). I guess back to Node.js for that part for now. I also wanted to add that Bun doesn't report the failing ports if run with --hot, it either fails silently or reports listening, but if you try the ports you get an immediate "RangeError: Maximum call stack size exceeded." |
You can directly specify a valid IP address (e.g. Bun.serve({
hostname: "0.0.0.0",
port: 8080,
// ...
}); If here it's expected that when IPv6 binding fails, it should automatically attempt to bind to the IPv4 address under the same hostname ( |
Not sure if this is related, but I am experiencing this isssue: const server = fastify();
server.listen({ host: "0.0.0.0", port: 3000 }, (err, address) => {
if (err) {
console.error(err);
process.exit(1);
}
console.log(`Server listening at ${address}`);
}); Fastify version: /etc/hosts file:
Trace: $ bun run --hot ./src/index.ts
337 | family: isIPv6(address) ? "IPv6" : "IPv4",
338 | port: this.#server.port
339 | };
340 | }
341 |
342 | listen(port, host, backlog, onListen) {
^
EADDRINUSE: Failed to start server. Is port 3000 in use?
syscall: "listen"
at listen (node:http:342:16)
at /node_modules/fastify/lib/server.js:250:6
at manageErr (/node_modules/fastify/fastify.js:602:10)
at exit (/node_modules/fastify/lib/hooks.js:113:4)
at next (/node_modules/fastify/lib/hooks.js:124:8)
at hookRunnerApplication (/node_modules/fastify/lib/hooks.js:98:2)
at /node_modules/fastify/fastify.js:584:10
at _encapsulateThreeParam (/node_modules/avvio/boot.js:562:6)
at timeoutCall (/node_modules/avvio/boot.js:458:4)
error: script "dev" exited with code 1 (SIGHUP) I tried commenting out ::1 line from /etc/hosts file, but still didn't work. I want to use "0.0.0.0" instead of localhost when running my server locally. |
…IPv4 address under the same hostname. Close: oven-sh#5315
@odicho I think what you're experiencing might be a separate issue. If you could open a new issue and provide some additional information, that would be greatly appreciated. Please include details such as whether you're on Linux or macOS, whether it fails every time, and if it succeeds when |
… under the same hostname. (#6533) * fix(serve): When IPv6 configuration is incorrect, attempt to bind to IPv4 address under the same hostname. Close: #5315 * fix review * fix review again --------- Co-authored-by: Ashcon Partovi <ashcon@partovi.net> Co-authored-by: Dylan Conway <35280289+dylan-conway@users.noreply.github.com>
What version of Bun is running?
1.0.1+31aec4ebe325982fc0ef27498984b0ad9969162b
What platform is your computer?
inside container:
Linux 5.4.0-144-generic x86_64 unknown
, on host:Linux 5.4.0-144-generic x86_64 x86_64
What steps can reproduce the bug?
sudo docker run -it debian:sid-slim
apt update && apt install -y curl unzip
curl -fsSL https://bun.sh/install | bash
source /root/.bashrc
bunx detect-port-alt 3000
apt install -y npm
npx -y detect-port-alt 3000
What is the expected behavior?
bunx detect-port-alt 3000
, likenpx detect-port-alt 3000
, correctly reports that port 3000 is available.What do you see instead?
Additional information
This is blocking me from running a vanilla
create-react-app
project using bun.It's important to try step 5 above (
bunx detect-port-alt 3000
) before step 6 (installing node) because if node is available, it will be used instead of bun (giving misleading results):The text was updated successfully, but these errors were encountered: