Repository navigation
event loop: count ticks that re-enter a mid-batch poll dispatch #33267
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
Open
alii
wants to merge
6
commits into
main
Choose a base branch
from
claude/nested-dispatch-tick-counter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a3422bf
event loop: count ticks that re-enter a mid-batch poll dispatch
alii 16d8b35
[autofix.ci] apply automated fixes
autofix-ci[bot] a557b71
make the nested-dispatch counter debug-build-only
alii 1481153
[autofix.ci] apply automated fixes
autofix-ci[bot] 2855d5b
keep the counter field unconditional; gate only the increment
alii ef3022f
note that the counter field occupies existing tail padding
alii File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { getEventLoopStats } from "bun:internal-for-testing"; | ||
| import { expect, test } from "bun:test"; | ||
| import { isDebug, isWindows } from "harness"; | ||
|
|
||
| // `nestedDispatchTicks` counts event-loop ticks that began while an outer | ||
| // ready-poll dispatch was still mid-batch, i.e. a poll callback synchronously | ||
| // waited on the event loop. https://github.com/oven-sh/bun/issues/33261 | ||
| // The counter only exists in debug builds (BUN_DEBUG), and only the POSIX | ||
| // event loop increments it: release builds and Windows always report 0. | ||
| test.skipIf(isWindows || !isDebug)( | ||
| "nestedDispatchTicks counts re-entrant ticks started inside a dispatch callback", | ||
| async () => { | ||
| const before = getEventLoopStats().nestedDispatchTicks; | ||
| expect(before).toBeNumber(); | ||
|
|
||
| // The fetch handler runs inside the server socket's poll dispatch, and | ||
| // HTMLRewriter.transform with an async handler synchronously waits on that | ||
| // handler's promise (waitForPromise), so a nested tick starts while the | ||
| // dispatch is still mid-batch. Sockets are level-triggered, so the nested | ||
| // tick cannot lose any one-shot event. When #33261 de-blocks HTMLRewriter, | ||
| // this trigger must become another synchronous waiter (or an internal hook). | ||
| const rewriter = new HTMLRewriter().on("p", { | ||
| async element() { | ||
| await Bun.sleep(1); | ||
| }, | ||
| }); | ||
| using server = Bun.serve({ | ||
| port: 0, | ||
| fetch() { | ||
| rewriter.transform("<p>x</p>"); | ||
| return new Response("ok"); | ||
| }, | ||
| }); | ||
| const res = await fetch(`http://localhost:${server.port}/`); | ||
| expect(await res.text()).toBe("ok"); | ||
|
|
||
| expect(getEventLoopStats().nestedDispatchTicks).toBeGreaterThan(before); | ||
| }, | ||
| ); | ||
|
|
||
| // Runs in every build flavor: the field always exists, and outside debug | ||
| // builds it must stay 0 (the counter is compiled out). | ||
| test("getEventLoopStats() always reports nestedDispatchTicks", async () => { | ||
| const stats = getEventLoopStats(); | ||
| expect(stats.nestedDispatchTicks).toBeNumber(); | ||
| expect(stats.nestedDispatchTicks).toBeGreaterThanOrEqual(0); | ||
| if (!isDebug) { | ||
| await Promise.all(Array.from({ length: 4 }, (_, i) => Bun.$`printf %s r${i}`.quiet())); | ||
| expect(getEventLoopStats().nestedDispatchTicks).toBe(0); | ||
| } | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.