-
Notifications
You must be signed in to change notification settings - Fork 5k
event loop: count queued concurrent_tasks in is_event_loop_alive() #36686
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| // https://github.com/oven-sh/bun/issues/11453 | ||
| // | ||
| // @edgedb/generate under Bun would sometimes exit 0 silently mid-connect. | ||
| // Root cause: edgedb's RawConnection does sock.ref()/await/sock.unref() around | ||
| // reads, and in between runs SCRAM via crypto.subtle (Bun exposes a global | ||
| // `crypto`, so the edgedb client picks its browserCrypto adapter). When the | ||
| // small-input digest fast path (or the completion leg of a work-queue crypto | ||
| // op) posts its result via ScriptExecutionContext::postTaskTo, the task lands | ||
| // in EventLoop.concurrent_tasks with no accompanying event-loop ref, and | ||
| // is_event_loop_alive() did not look at concurrent_tasks. With the socket | ||
| // unref'd, the liveness check saw zero and the process exited with the crypto | ||
| // result still queued. | ||
|
Check warning on line 12 in test/regression/issue/11453.test.ts
|
||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
claude[bot] marked this conversation as resolved.
Outdated
|
||
| import { describe, expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, normalizeBunSnapshot } from "harness"; | ||
| import { once } from "node:events"; | ||
| import net from "node:net"; | ||
|
|
||
| describe("issue #11453: crypto.subtle keeps the event loop alive after a yield", () => { | ||
| // Deterministic: the <64-byte SHA digest fast path computes synchronously | ||
| // and posts the callback via postTaskTo with no work-queue ref. | ||
| test.concurrent("crypto.subtle.digest (small input) awaited after setImmediate", async () => { | ||
| const script = ` | ||
| (async () => { | ||
| await new Promise(r => setImmediate(r)); | ||
| await crypto.subtle.digest("SHA-256", new Uint8Array(32)); | ||
| console.log("resolved"); | ||
| })(); | ||
| `; | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "-e", script], | ||
| env: bunEnv, | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| expect(normalizeBunSnapshot(stderr)).toBe(""); | ||
| expect(normalizeBunSnapshot(stdout)).toMatchInlineSnapshot(`"resolved"`); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
|
|
||
| test.concurrent("crypto.subtle.digest (small input) awaited after setTimeout", async () => { | ||
| const script = ` | ||
| (async () => { | ||
| await new Promise(r => setTimeout(r, 0)); | ||
| await crypto.subtle.digest("SHA-256", new Uint8Array(32)); | ||
| console.log("resolved"); | ||
| })(); | ||
| `; | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "-e", script], | ||
| env: bunEnv, | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| expect(normalizeBunSnapshot(stderr)).toBe(""); | ||
| expect(normalizeBunSnapshot(stdout)).toMatchInlineSnapshot(`"resolved"`); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
|
|
||
| // The exact shape from edgedb's rawConn._waitForMessage: with the socket | ||
| // unref'd between reads, an awaited crypto.subtle op must keep the process | ||
| // alive until it resolves and the next sock.ref() runs. | ||
| test.concurrent("crypto.subtle between sock.unref() and sock.ref() on a net.Socket", async () => { | ||
| const server = net.createServer(socket => { | ||
| socket.setNoDelay(); | ||
| socket.on("data", () => { | ||
| setImmediate(() => { | ||
| try { | ||
| socket.write("R"); | ||
| } catch {} | ||
| }); | ||
| }); | ||
| socket.on("error", () => {}); | ||
| }); | ||
| server.listen(0, "127.0.0.1"); | ||
| await once(server, "listening"); | ||
| const port = (server.address() as net.AddressInfo).port; | ||
|
|
||
| const script = ` | ||
| const net = require("net"); | ||
| const sock = net.createConnection(${port}, "127.0.0.1"); | ||
| let connR, dataR; | ||
| sock.on("connect", () => connR()); | ||
| sock.on("error", e => { console.error("err", e.message); process.exit(2); }); | ||
| sock.on("data", () => { if (dataR) { dataR(); dataR = null; } }); | ||
| (async () => { | ||
| await new Promise(r => (connR = r)); | ||
|
|
||
| // round-trip: edgedb's ref() / await data / unref() pattern | ||
| sock.write("x"); | ||
| sock.ref(); | ||
| await new Promise(r => (dataR = r)); | ||
| sock.unref(); | ||
|
|
||
| // Socket is now unref'd. Nothing else is ref'd. The awaited digest | ||
| // must keep the loop alive on its own. | ||
| await crypto.subtle.digest("SHA-256", new Uint8Array(32)); | ||
|
|
||
| sock.write("y"); | ||
| sock.ref(); | ||
| await new Promise(r => (dataR = r)); | ||
| sock.unref(); | ||
|
|
||
| console.log("resolved"); | ||
| sock.destroy(); | ||
| })(); | ||
| `; | ||
|
|
||
| try { | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "-e", script], | ||
| env: bunEnv, | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| expect(normalizeBunSnapshot(stderr)).toBe(""); | ||
| expect(normalizeBunSnapshot(stdout)).toMatchInlineSnapshot(`"resolved"`); | ||
| expect(exitCode).toBe(0); | ||
| } finally { | ||
| await new Promise<void>(r => server.close(() => r())); | ||
| } | ||
| }); | ||
| }); | ||
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.