日本語

Bun –watch vs –hot: What Happens to Your Process on Save

​​Bun​TypeScript

When you want a development server to run again automatically on every save, Bun offers two options: bun --watch and bun --hot. --watch runs the program again from the beginning, while --hot re-reads the code in the same process. This difference changes what happens to in-memory values, HTTP server responses, and setInterval timers.

To help decide which one to use instead of nodemon, I ran the same files in both modes on my test environment and compared the output and numbers.

Note

The test environment was Ubuntu 24.04 (x86_64) with Bun 1.4.4. I made saves by rewriting files from a script using sed and similar tools. I did not test saving from an editor, Windows or macOS, bun --watch test or bun build --watch, changes to a separate file imported from the entry file, or combinations with frameworks. For the official documentation, I checked the source files at the tag used for testing (bun-v1.4.4) on GitHub, and the links point there.

The differences at a glance

The differences I confirmed in testing are summarized below.

On save bun --watch bun --hot
Process Starts over from the top (PID stays the same) Stays as it is
Values put on globalThis Lost Kept
let values inside a module Return to their initial value Return to their initial value
HTTP server Requests fail for 20–32 ms after the save, until the new version responds Requests do not fail
setInterval timers The previous ones stop The previous ones keep running
SIGTERM handler Runs before the restart Does not run, and registrations keep accumulating
Saved with a syntax error The server stops Keeps responding with the previous code

The official Watch Mode page also explains that --watch performs a hard restart of the process, while --hot performs a soft reload that re-reads the code without restarting the process.

–watch runs the program from the top with the same PID

I ran the following state.ts in both modes. On each save, I rewrote the version value to "v1", "v2", and "v3".

ts
declare global {
  var reloads: number;
}

globalThis.reloads ??= 0;
let moduleCount = 0;
moduleCount++;

const version = "v0";
console.log(`[${version}] pid=${process.pid} uptime=${process.uptime().toFixed(2)}s globalThis.reloads=${globalThis.reloads} moduleCount=${moduleCount}`);
globalThis.reloads++;

setInterval(() => {}, 1_000_000);

The final setInterval keeps the program from exiting right away. The output of bun --watch app.ts was as follows (I ran state.ts under the name app.ts).

text
[v0] pid=2145 uptime=0.00s globalThis.reloads=0 moduleCount=1
[v1] pid=2145 uptime=0.00s globalThis.reloads=0 moduleCount=1
[v2] pid=2145 uptime=0.00s globalThis.reloads=0 moduleCount=1
[v3] pid=2145 uptime=0.00s globalThis.reloads=0 moduleCount=1

On each save, the uptime went back to 0 seconds and globalThis.reloads stayed at 0. The program started from a clean state every time.

The PID, however, stayed at 2145. So I started it again under strace (this time the PID was 2195) and recorded the execve system calls, which run a program. execve replaces the currently running program with a new one (Linux execve man page). I replaced the bun path with /path/to/bun.

text
2195  execve("/path/to/bun", ["bun", "--watch", "app.ts"], 0x7ffe03994888 /* 6 vars */) = 0
2196  execve("/path/to/bun", ["bun", "--watch", "app.ts"], 0x2ce87530000 /* 6 vars */ <unfinished ...>
...
2195  +++ superseded by execve in pid 2196 +++

The first line is the initial start. The second line is the record for the save, where bun --watch app.ts was run again with the same arguments. The last line shows that process 2195 was replaced through this execve. The PID the program printed was 2195 both before and after the save.

Since the PID does not change, be careful not to conclude that "it was not restarted" from the PID alone.

–hot re-evaluates the code while keeping the process

Running the same state.ts with bun --hot app.ts gave the following output.

text
[v0] pid=2175 uptime=0.00s globalThis.reloads=0 moduleCount=1
[v1] pid=2175 uptime=1.00s globalThis.reloads=1 moduleCount=1
[v2] pid=2175 uptime=2.01s globalThis.reloads=2 moduleCount=1
[v3] pid=2175 uptime=3.01s globalThis.reloads=3 moduleCount=1

The uptime increased by about one second on each save, and globalThis.reloads went up to 1, 2, 3. The process did not stop, and the values stored on globalThis were kept.

However, moduleCount, declared with let inside the module, returned to 1 each time. --hot re-evaluates the file from the beginning, so only what is stored on globalThis survives. The official documentation also states that all files are re-evaluated and that global state such as globalThis is preserved.

With an HTTP server, responses drop out only under –watch

I also compared a small server built with Bun.serve. It counts incoming requests in hits and returns that count.

ts
let hits = 0;
const version = "v0";

const server = Bun.serve({
  port: 3000,
  fetch() {
    hits++;
    return new Response(`${version} hits=${hits}\n`);
  },
});

console.log(`${version} listening on ${server.port} pid=${process.pid} at ${Date.now()}`);

I accessed it three times with curl, saved, and then accessed it three more times. The results were the same for --watch and --hot.

text
v0 hits=1
v0 hits=2
v0 hits=3
--- save (v0 -> v1)
v1 hits=1
v1 hits=2
v1 hits=3

Because hits is a let inside the module, it goes back to 0 even under --hot. If you want the request count to survive saves, you need to put it on globalThis.

The difference appeared right after the save. While sending requests to the server every 2 ms and saving the file, I measured 10 runs each of the time until the new version (such as v1) responded and the number of requests that failed during that time.

bun --watch: median 24 ms from save until the new version responded, with 7–12 requests failing in that window. bun --hot: median 3 ms, with 0 failures in all 10 runs
Time from save until the new version responded, and the number of failed requests during that time (10 runs each)

Under --watch, the new version took 20–32 ms to respond, and 7–12 requests failed during that window. The first failure in each run was either a connection error (Python's URLError) or a connection reset (ConnectionResetError). Under --hot, the new version responded in 3–5 ms, and all 10 runs had 0 failed requests.

–hot keeps the previous timers and process.on handlers

--hot has a point to watch out for. I checked it with the following timer.ts.

ts
const version = "v0";

setInterval(() => {
  console.log(`tick from ${version}`);
}, 400);

process.on("SIGTERM", () => {
  console.log(`SIGTERM handler ran in ${version}`);
  process.exit(0);
});

console.log(`${version} started, SIGTERM listeners=${process.listenerCount("SIGTERM")}`);

This is the output of bun --hot app.ts when saving twice, about one second apart.

text
v0 started, SIGTERM listeners=1
tick from v0
tick from v0
v1 started, SIGTERM listeners=2
tick from v0
tick from v1
tick from v0
tick from v1
tick from v0
v2 started, SIGTERM listeners=3
tick from v1
tick from v0
tick from v2
...

Even after saving, the timer from the previous version (v0) kept running without stopping. After two saves, the timers for v0, v1, and v2 were all running at the same time. The handlers registered with process.on also increased, with SIGTERM listeners going to 1, 2, 3.

Running the same file with bun --watch app.ts gave the following.

text
v0 started, SIGTERM listeners=1
tick from v0
tick from v0
SIGTERM handler ran in v0
v1 started, SIGTERM listeners=1
tick from v1
tick from v1
SIGTERM handler ran in v1
v2 started, SIGTERM listeners=1
tick from v2
tick from v2

Under --watch, the timer of the previous version stopped, and there was always just one handler. The SIGTERM handler ran before the restart. The official documentation also says that bun run --watch runs the handler for the termination signal the script registered (SIGTERM by default) before restarting.

Warning

If you use setInterval in code run with --hot, every save adds another copy of the same loop, and they all run at once. For example, if the code periodically calls an external API, the number of calls increases accordingly.

To stop the previous timer under --hot, keep the timer ID on globalThis and call clearInterval on it when the module is loaded again.

ts
declare global {
  var tickTimer: ReturnType<typeof setInterval> | undefined;
}

const version = "v0";

// If a timer from before the reload is still running, stop it
if (globalThis.tickTimer) clearInterval(globalThis.tickTimer);

globalThis.tickTimer = setInterval(() => {
  console.log(`tick from ${version}`);
}, 400);

console.log(`${version} started`);

When I ran this file with bun --hot app.ts and saved twice in the same way, there was always only one timer running.

text
v0 started
tick from v0
tick from v0
v1 started
tick from v1
tick from v1
v2 started
tick from v2
tick from v2

Saving with a syntax error stops the server under –watch

I also compared what happens when you save a half-finished file. I changed the last }); in server.ts to ), saved, and then restored the correct file.

In both modes, the following error appeared as soon as I saved (I replaced the path with /path/to/app; two more errors of the same form followed).

text
10 | )
     ^
error: Expected identifier but found ")"
    at /path/to/app/app.ts:10:1

The server behaved differently while the error was showing. Under --watch, curl could not connect to the server.

text
curl: (7) Failed to connect to localhost port 3000 after 0 ms: Couldn't connect to server

Under --hot, the response came from the v0 code as it was before the error.

text
v0 hits=2

When I restored the correct file and saved, both modes returned v2 hits=1. Under --watch, the server also started again automatically, so there was no need to retype the command.

Which one to use

Based on these results, --hot suits cases where you are rewriting an HTTP server and testing as you go. Requests do not fail right after a save, and if you save half-written code, the previous code keeps running. However, code that uses setInterval or process.on needs to be written so that it stops the previous instance, as in the example above.

--watch always starts from a clean state, so there is no unexpected behavior caused by leftover state. For code with many timers or event handlers, or when you want to test the startup logic too, it makes the results easier to read.

Also, according to the official documentation, --hot is different from the hot reloading that updates the browser page, and it is meant for server-side use. For browser-side hot reloading, the documentation recommends using a framework such as Vite.

​Posted by 86