-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Description
What version of Bun is running?
1.3.0+b0a6feca5
What platform is your computer?
Darwin 24.5.0 arm64 arm
What steps can reproduce the bug?
Pre-requisites:
- Have Docker installed
- Pull test image:
docker pull testcontainers/ryuk:0.14.0
Create empty project with dockerode dependency:
mkdir incomingmessage-socket-unref-repro
cd $_
npm init -y
npm install dockerodeCreate index.mjs file which starts a Docker container,
import Dockerode from "dockerode";
const dockerode = new Dockerode();
console.log("Creating container...");
const container = await dockerode.createContainer({
Image: "testcontainers/ryuk:0.14.0",
Env: ["RYUK_CONNECTION_TIMEOUT=10s"],
ExposedPorts: { "8080/tcp": {} },
HostConfig: {
PortBindings: { "8080/tcp": [{ HostPort: "0" }] },
Binds: [`/var/run/docker.sock:/var/run/docker.sock:rw`],
},
});
console.log("Starting container...");
await container.start();
console.log("Fetching log stream...");
const logStream = await container.logs({
follow: true,
stdout: true,
stderr: true,
});
logStream.socket.unref();
console.log("COMPLETE, EXIT NOW");What is the expected behavior?
When run with node, we see the following output, followed by the process terminating immediately, even though the log stream has not ended:
❯ node index.mjs
Creating container...
Starting container...
Fetching log stream...
COMPLETE, EXIT NOW
❯
What do you see instead?
When run with bun, we see the same output, but the process does not terminate immediately. It will only terminate after 10 seconds, which is when the container exits and the log stream ends:
❯ bun index.mjs
Creating container...
Starting container...
Fetching log stream...
COMPLETE, EXIT NOW
Additional information
The logStream is of type IncomingMessage. The key code to let the process exit immediately is:
logStream.socket.unref();Without it, both node and bun will wait for the stream to end before exiting. With it, node exits regardless of whether or not the stream is open, bun does not.
I am trying to add official bun support to testcontainers-node here testcontainers/testcontainers-node#1162.
Our use case is, we start a background container which manages containers created within the same session. When the session ends, the background container will clean up its managed containers after a timeout. We do not want the user's session to wait for this container's timeout, it should happen in the background.