Skip to content

test(hot): accept two reloads for one save in the entry-point promise tests - #44666

Closed
robobun wants to merge 5 commits into
mainfrom
robobun/638c6aac/hot-entry-promise-test-saves
Closed

robobun wants to merge 5 commits into
mainfrom
robobun/638c6aac/hot-entry-promise-test-saves

Conversation

@robobun

@robobun robobun commented Oct 6, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

  • does not take a promise of the program's, %s, for that of the entry point fails an attempt in 52 of 455 CI builds (Nov 11 to 13), on the Linux release lanes: stdout is ["first load", "collected", "collected", "third load", ""].
  • The test saves main.js in place. fs.writeFileSync writes over the file, then calls ftruncate (src/runtime/node/node_fs.rs:8278): two watcher events. A reload between them runs the second load twice.

Fix

  • The second text prints collected once. Each load still does the work, as on main.
  • The test asserts that the third load exits with code 0.
  • Verified: bun bd test test/cli/hot/hot.test.ts passes. With 5 ms between the two calls, the tests of main fail 8 of 8 and these pass 8 of 8.

Background

Notes

This PR changes tests only. No file under src/ changes. The diff against main is 4 added lines and 1 removed line in one test.

Source of the CI numbers. Annotations of the 455 finished builds 140029 to 141204 (2026-11-11 to 2026-11-13). The two tests fail an attempt in 59 of them. Every hit passed on the retry.

signature builds job hits by lane
"collected" twice 52 debian 13 aarch64 23, alpine 3.23 aarch64 12, debian 13 x64 8, ubuntu 25.04 aarch64 6, alpine 3.23 x64 3, ubuntu 25.04 x64 3
Expected ";" but found ")" 8 alpine 3.23 aarch64 6, debian 13 x64 1, alpine 3.23 x64 1
timeout on Windows 0

All 8 builds with the parse error ran a test text without the padding of #48677. With the padding it does not occur. "collected" twice occurs with the padding too, in 11 builds: 140753, 140778, 140788, 140919, 140935, 140977, 141013, 141046, 141083, 141109, 141176.

The doubled load from a delay between the two calls. A probe does the syscalls of fs.writeFileSync by hand (openSync without O_TRUNC, writeSync, a busy wait, ftruncateSync) for the two saves. Debug build of this branch, 8 tests per cell (4 runs of the 2 cases):

delay text of main fails text of this PR fails
0 1 0
1 ms 6 0
5 ms 8 0
50 ms 8 0

Every failure is "collected" twice, the same diff as in CI. The ftruncate is an event of its own even when it does not change the size. The text of this PR also passes with delays from 20 ms to 1.5 s, which put the second load at each step of the first (16 tests), and with three events for each save (8 tests).

Without the probe. The two tests next to 24 busy loops on 12 cores, debug build: the text of main fails 1 of 104 test runs ("collected" twice), the text of this PR fails 0 of 72. CI has about the same rate for main: 55 job hits in 455 builds, with 6 Linux release lanes and 2 cases for each build.

Why the line is printed once. --hot can reload twice for one save, and this test is not about that. The older tests in this file accept it too (expect(reloadCounter).toBeGreaterThanOrEqual(3), and driveErrorReloadCycle saves again when it sees the previous error). #30617 (open) makes the watcher wait longer before it posts a reload. This PR does not depend on it. Every load still does the collections and makes the promises, so the program differs from the one on main only in the line it prints.

Why a rename does not remove the second reload. A rename over the entry point gives a MOVED_TO event on the directory and a DELETE_SELF event on the replaced file. src/jsc/hot_reloader.rs:1039 handles the case that they land in separate reads, and each posts a reload.

Not in this PR: the rejected and handled case does not catch its bug. Promise.reject(e).catch(() => {}) makes two promises: the rejected one and the one that catch() returns, which is fulfilled a moment later. The loop makes them in turn, so the collected cell goes to either kind. A fulfilled promise in that cell looks like an entry point that loaded, and the unfixed build passes. Release build from before #44350 (367d939), the real test files:

loop of the second text pending fails rejected and handled fails
as on main and in this PR (promise, catch(), promise, catch()) 11 of 11 0 of 31
all 20,000 promises, then the catch() calls 6 of 6 20 of 20

Two earlier commits of this PR had the second loop. With it, the pending case timed out after 90 s on windows 11 aarch64 in both CI runs (builds 141268 and 141322), on the first attempt, and passed on the retry. The rejected and handled case passed there, and so did windows 2019 x64. No other build from 2026-11-10 to 2026-11-14 has that timeout for these tests (about 600 builds). The second text of those commits also had comments in it: 820 and 826 bytes for the pending case, against 494 on main and 562 now. I cannot run Windows and I did not find the cause, so the loop is back to the one on main and the comments are out of the text. Someone with a windows 11 aarch64 machine can tell which of the two it was. With the loop of main and the short text (this head), both Windows lanes passed the file on the first attempt in build 141369.

History of this PR. October: the three saves differed in one digit, so that no read could see a mix of two of them. main then padded the third save (#48677), and the merge on 2026-11-13 dropped the digit. The merge commit skipped the work of a second load (if (!globalThis.kept)). That became the printed-once line on 2026-11-14, so that every load does what it does on main.

Other runs. The whole file on the debug build: 26 pass, 2 skip (Windows only), 1 todo, 0 fail. A review note asked for the exit code check. It has the form that the other --hot tests of this file use.

Earlier numbers. From 2026-10-02 to 2026-10-06 the two tests failed on Linux in 75 of 400 builds (47 with "collected" twice, 30 with the parse error). From 2026-10-07 to 2026-10-16 a sample had 3 of 147.

Not changed. holds the promise of the entry point itself, which it looks at on every tick also saves in place. It waits only for the line of the second load, and it has no failure in the 455 builds. --hot does not emit beforeExit to the listeners of a failed run timed out on windows 11 aarch64 in 2 of the builds (140982, 141150). That is another test and another failure.


no test proof · iteration 2 · platform-specific test(s) that do not run on this machine, deferring to CI, which covers all platforms: test/cli/hot/hot.test.ts

The two tests save main.js in place. fs.writeFileSync writes over the
file and then resizes it, which is two watcher events. When --hot
reloads between them, the second load runs twice and prints "collected"
twice, or the third load parses the new file followed by the tail of
the old one.

The three saves now have the same length and differ in one digit, so a
read always gets one whole version. The second load does its work once.
The 20,000 promises are made before the first catch(), so the collected
cell goes to one of them and not to a promise that catch() returns.
@coderabbitai

coderabbitai Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The hot-reload test documents that one save can trigger two loads. Its second-load script logs "collected" only once, and the test asserts that the runner exits with code 0.

Changes

Hot reload promise reuse

Layer / File(s) Summary
Duplicate-load handling and test result
test/cli/hot/hot.test.ts
The test documents that writeFileSync() can produce two watcher events. The second-load script logs "collected" only if globalThis.collected is unset, then sets the flag. The test asserts that the runner exits with code 0.

Suggested reviewers: jarred-sumner, dylan-conway

Merge Risk: Minimal · up to a69da

This is a test-only change that makes the hot-reload promise test tolerate a duplicate watcher event and assert a clean exit. It has no production impact and no concrete merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "test(hot): accept two reloads for one save in the entry-point promise tests" matches the main change. The PR changes only test/cli/hot/hot.test.ts so that the entry-point promise tests acce…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description follows the repository template with both required sections, 'What does this PR do?' and 'How did you verify your code works?', and both are substantively filled in. The 'Problem' and …
Full details: Title check

Explanation

The title "test(hot): accept two reloads for one save in the entry-point promise tests" matches the main change. The PR changes only test/cli/hot/hot.test.ts so that the entry-point promise tests accept a second reload from one save, and it prints "collected" once. The title is specific and follows a conventional commit style. It is a little long at 75 characters, but it is clear.

Full details: Description check

Explanation

The description follows the repository template with both required sections, 'What does this PR do?' and 'How did you verify your code works?', and both are substantively filled in. The 'Problem' and 'Fix' sections explain the flaky CI failure, the cause (two watcher events from writeFileSync and ftruncate), and the change (the second load prints 'collected' once and the test asserts exit code 0). The verification section gives a concrete command and results (8 of 8 passing with a 5 ms delay, against 8 of 8 failing on main). The description is long and includes a large details block of CI statistics, which is more than the template asks for, but the required information is present. The robobun evidence note says no platform-specific proof was run locally and defers to CI, which the author states explicitly. Remaining gaps are minor: the Windows timeout on 'pending' is unresolved, and the author acknowledges this openly.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 1bd6435 and a69dadb.

Files selected for processing: 1

  • test/cli/hot/hot.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

@github-actions github-actions Bot added the claude label Oct 6, 2026
@robobun

robobun commented Oct 6, 2026 •

Copy link
Copy Markdown
Collaborator Author

Status: ready for review. This PR changes tests only: 4 added lines and 1 removed line.

CI (build 141369, this head): test/cli/hot/hot.test.ts passed on the first attempt on every lane that ran, with both Windows lanes among them. No test is red. The 4 darwin any x64 test jobs did not run: they expired in the queue twice (05:05 and 06:05 UTC on 2026-11-14) with no free agent, and are scheduled again. The darwin x64 agents are busy with older builds. The 6 tests that needed a retry are in other files.

How the failure was reproduced: a probe did the syscalls of fs.writeFileSync by hand for the saves of the two tests (openSync without O_TRUNC, writeSync, a delay, ftruncateSync).

  • Debug build, 5 ms delay: the test text of main fails 8 of 8 tests with "collected" twice on stdout. The text of this PR passes 8 of 8.
  • Without the probe, next to 24 busy loops: the text of main fails 1 of 104 test runs in the same way. The text of this PR fails 0 of 72.

The flake on main: "collected" twice in 52 of the 455 builds 140029 to 141204 (2026-11-11 to 2026-11-13), 11 of them with the current test text of main.

Not in this PR: the rejected and handled case does not catch the bug of #44350 (0 of 31 runs on a build from before it). Two earlier commits here made the 20,000 promises before the first catch(), which catches it 20 of 20 runs. With that order the pending case timed out on windows 11 aarch64 in both CI runs (builds 141268 and 141322) and passed on the retry. I cannot run Windows and did not find the cause, so the loop is the one of main again.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review found no issues

No high-confidence issues detected in this change.

main pads the third save of the entry-point promise tests to the length
of the second, so a load between the write and the resize no longer
reads the tail of the second text. This branch keeps that and drops its
own way to the same end (three saves that differ in one digit).

What stays from this branch: the second load does its work once, since
one save can reload twice, and the 20,000 promises are made before the
first catch().
@robobun

robobun commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 12:29 AM PT - Nov 14th, 2026

✅ @robobun, your commit a69dadb9d6ce31243944ccd3c421d2c903efe085 passed in Build #141369! 🎉


🧪   To try this PR locally:

bunx bun-pr 44666

That installs a local version of the PR into your bun-44666 executable, so you can run:

bun-44666 --bun

@robobun robobun changed the title test(hot): remove two races from the entry-point promise tests test(hot): let one save reload twice in the entry-point promise tests Nov 13, 2026
@robobun robobun changed the title test(hot): let one save reload twice in the entry-point promise tests test(hot): accept two reloads for one save in the entry-point promise tests Nov 13, 2026

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — after the merge of main the change is down to two test-only edits in test/cli/hot/hot.test.ts (the if (!globalThis.kept) guard and making the 20,000 promises before the first catch()), both of which read as correct.

What was reviewed:

  • Checked whether the guard lets an unfixed build pass when one save reloads twice: the gc/readFile/setImmediate/randomBytes chain still runs once and the current entry-point promise is collected the same way, so the property from #44350 is still asserted. I did not run the unfixed-build check the PR describes.
  • Verified both new comments against source: writeFileSync opens without O_TRUNC and calls ftruncate after the write (src/runtime/node/node_fs.rs:8278-8387), and catch() on a rejected promise yields a fulfilled one. The second for loop attaches every handler synchronously, so no unhandled rejection fires before it.
  • Note: the PR description still describes the "three saves differ in one digit" source(1|2|3) shape from the first commit; after the merge, the test keeps main's padEnd third write instead, so the description is stale.
Extended reasoning...

The diff touches one test file only (test/cli/hot/hot.test.ts, lines 912-931) and no production code, so it has no security-sensitive surface. The original commit rewrote the three saves as one source(load) template, but the merge of main dropped that in favor of main's existing padEnd approach, leaving just the reload guard and the push-then-catch split. The guard removes the double "collected" from a double watcher event without changing the GC stack depth or count, and the catch split strengthens the test by keeping catch-derived fulfilled promises from taking the collected cell. The bug hunt ran dry, the single candidate (guard weakening the test) was ruled out by reading, and no CODEOWNER covers the path, which decided approve.

@robobun

robobun commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator Author

On the stale description: it was rewritten after the merge with main (last edit 23:23 UTC, about when this review started). The body now names only the two edits that remain: the guard of the second load, and the 20,000 promises made before the first catch(). The one-digit saves appear only under Notes, as the history of the first version.

The merge commit skipped the work of a second load of the second text.
Every load now does the work again, as on main. Only the line is printed
once, so a save that reloads twice no longer fails the test.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Beyond the inline nit, I also checked the two new comment claims against source: a path-based writeFileSync does write and then ftruncate (src/runtime/node/node_fs.rs:8352-8388), and the 20000 .catch() handlers are all attached synchronously before any microtask checkpoint, so the "rejected and handled" case still raises no unhandled rejection. Not run here: that the reordered test still fails on a build without the #44350 fix; the PR's own-build numbers are the only evidence of that.

Extended reasoning...

The change is one hunk in test/cli/hot/hot.test.ts: the second-load source now creates all promises before attaching any catch handler and prints "collected" only once per --hot session via a globalThis guard; the exact stdout/stderr assertion is unchanged. No production code and no security-sensitive surface is touched. Reading confirmed both new comments are true, but whether the test still catches the original use-after-free is an empirical property of heap cell reuse that reading cannot settle, so this is a note of what was ruled out rather than an approval.

Additional findings (outside the current diff — GitHub can't attach inline comments there):

  • 🟣 test/cli/hot/hot.test.ts — nit, pre-existing: a maintainer gets a passing run even if the third load exits non-zero, since the test never asserts the exit code. The final toEqual at test/cli/hot/hot.test.ts:937 checks only { stdout, stderr }; runner.exited is never awaited, although the third text calls process.exit(0). Fix: await runner.exited together with runner.stderr.text() and assert a combined { stdout, stderr, exitCode: 0 } object, so a reload that prints "third load" but exits with another code (or is killed on dispose) fails the test.

    Why this was flagged

    The third text written at test/cli/hot/hot.test.ts:934 is console.log("third load"); process.exit(0);. After await line("third load") at line 935 the test reads runner.stderr.text() at line 936 and asserts only { stdout, stderr } at lines 937-940. runner.exited is never awaited and no exit code is asserted, so a run in which bun prints "third load" and then exits non-zero without writing to stderr (an exit-path regression under --hot) still passes. The base branch has the same gap; this PR edits the same test body (lines 920-926) without adding the exit-code assertion that REVIEW.md asks of subprocess tests.

    Verification: pre-existing (nit). Triggering condition: a --hot run that prints "third load" and then exits with a non-zero code without writing anything to stderr. Nothing in test/cli/hot/hot.test.ts:934-940 references runner.exited or exitCode. So the exit status of the third load is never observed. The same lines are byte-identical in the base commit 4bf0ef9, so the gap predates this PR.

@robobun

robobun commented Nov 14, 2026

Copy link
Copy Markdown
Collaborator Author

The exit code is now asserted (94395e8): expect(await runner.exited).toBe(0) after the output check, the same form as the other --hot tests in this file. With that line the two tests still pass on a debug build of this branch and still fail on a release build from before #44350, 3 of 3 runs each.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — a small, test-only change that keeps the original property under test while tolerating the two watcher events one writeFileSync produces.

What was reviewed:

  • The globalThis.collected guard: the first load of the second text always prints (the flag is unset), a repeat load still does the GC and 20000-promise work and only skips the print, and globalThis.kept is reset per load, so no path leaves collected unprinted or printed twice.
  • The two-pass loop: all .catch() calls still run synchronously in the same callback before any microtask checkpoint, so no unhandled rejection reaches stderr; the reordering changes allocation order, not GC depth or count.
  • The new expect(await runner.exited).toBe(0) sits after the stdout/stderr assertion and the third text calls process.exit(0), so it cannot hang; ordering matches the harness convention.
  • Not done here: I did not run the test, nor verify the PR's claim that the reordered loop fails the rejected and handled case on a build from before #44350.
Extended reasoning...

The diff touches one test file, test/cli/hot/hot.test.ts, in a single it.each case for --hot entry-point promise reuse: it splits promise creation from .catch() attachment, guards the collected print with a global flag so a double reload prints once, and adds a trailing exit-code assertion. It touches no production code and no security-sensitive surface. No CODEOWNERS entry covers this path, the timeline shows no CHANGES_REQUESTED state or unaddressed objection, and the hunt ran dry; the change is small enough to verify fully by reading, which decided approve over defer.

The second text makes its promises in the order that main has again, and
the note on the two watcher events moves out of the text. What stays:
"collected" is printed once, and the exit code is checked.

With the promises made before the first catch(), the "pending" case
timed out on windows 11 aarch64 in both CI runs of this branch (builds
141268 and 141322) and passed on the retry. The cause is not known.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Comment thread test/cli/hot/hot.test.ts Resolved
@robobun

robobun commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator Author

Folded into #49196, which combines the open test-only PRs into one. The commit f1014021a in that PR is this change, unchanged.

Closing this one in favor of #49196.

@robobun robobun closed this Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant