Repository navigation
fix: async simctl boot query so detached streams do not stall (#186) #191
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d87475f
fix: keep simulator shutdown queries off the streaming event loop
kiendle 7c008c9
fix: share pending boot queries and simplify watchdog regression
kiendle 2db967b
ci: repeat shutdown-watch responsiveness regression on Linux
kiendle 4bae63c
fix: revalidate helper identity after boot-state await
kiendle 9768d57
fix: reuse validated helper state after boot query
kiendle 12a6410
fix: confirm stale boot snapshots before recycling helpers
kiendle 1566097
docs: explain reuse confirmation retry errors
kiendle b87552e
test: cover helper removal during boot confirmation
kiendle a54e761
fix: restore maintainer-requested scope for PR #187
kiendle 01860f7
fix: async simctl boot query so detached streams do not stall (#186)
EvanBacon 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
19 changes: 19 additions & 0 deletions
19
packages/serve-sim/src/__tests__/fixtures/shutdown-watch-xcrun.sh
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,19 @@ | ||
| #!/bin/sh | ||
| set -eu | ||
|
|
||
| case "$*" in | ||
| "simctl list devices booted -j") | ||
| printf started > "$SERVE_SIM_TEST_QUERY_DIR/query-started" | ||
| while [ ! -e "$SERVE_SIM_TEST_QUERY_DIR/release-query" ]; do | ||
| sleep 0.02 | ||
| done | ||
| printf finished > "$SERVE_SIM_TEST_QUERY_DIR/query-finished" | ||
| ;; | ||
| "simctl list devices -j") ;; | ||
| *) | ||
| printf 'Refusing unexpected simulator command: %s\n' "$*" >&2 | ||
| exit 88 | ||
| ;; | ||
| esac | ||
|
|
||
| printf '{"devices":{"test":[{"udid":"%s","state":"Booted"}]}}\n' "$SERVE_SIM_TEST_UDID" |
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,24 @@ | ||
| import { createServer } from "net"; | ||
| import { setTimeout as delay } from "timers/promises"; | ||
|
|
||
| export async function freePort(): Promise<number> { | ||
| const server = createServer(); | ||
| const ready = Promise.withResolvers<void>(); | ||
| server.once("error", ready.reject); | ||
| server.listen(0, "127.0.0.1", ready.resolve); | ||
| await ready.promise; | ||
| const address = server.address(); | ||
| const closed = Promise.withResolvers<void>(); | ||
| server.close((error) => error ? closed.reject(error) : closed.resolve()); | ||
| await closed.promise; | ||
| if (!address || typeof address === "string") throw new Error("failed to allocate port"); | ||
| return address.port; | ||
| } | ||
|
|
||
| export async function waitFor(predicate: () => boolean, timeoutMs = 1_000, intervalMs = 5): Promise<void> { | ||
| const deadline = Date.now() + timeoutMs; | ||
| while (!predicate()) { | ||
| if (Date.now() >= deadline) throw new Error("condition not reached"); | ||
| await delay(intervalMs); | ||
| } | ||
| } |
88 changes: 88 additions & 0 deletions
88
packages/serve-sim/src/__tests__/shutdown-watch-responsive.test.ts
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,88 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { spawn, type ChildProcess } from "child_process"; | ||
| import { chmodSync, copyFileSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "fs"; | ||
| import { tmpdir } from "os"; | ||
| import { delimiter, join } from "path"; | ||
| import { freePort, waitFor } from "./server-test-support"; | ||
|
|
||
| const udid = "00000000-0000-4000-8000-000000000102"; | ||
|
|
||
| // The real CLI subprocess's watchdog cannot use the test process's fake clock. | ||
| test.skipIf(process.platform === "win32")("serves HTTP while the shutdown boot-state query is pending", async () => { | ||
| const root = mkdtempSync(join(tmpdir(), "serve-sim-shutdown-watch-")); | ||
| const bin = join(root, "bin"); | ||
| const started = join(root, "query-started"); | ||
| const finished = join(root, "query-finished"); | ||
| const release = join(root, "release-query"); | ||
| let child: ChildProcess | undefined; | ||
| let stderr = ""; | ||
| let exited = false; | ||
| const { promise: closed, resolve: resolveClosed } = Promise.withResolvers<void>(); | ||
| const waitClosed = async (ms: number) => { | ||
| const timeout = Promise.withResolvers<void>(); | ||
| const timer = setTimeout(timeout.resolve, ms); | ||
| try { | ||
| await Promise.race([closed, timeout.promise]); | ||
| } finally { | ||
| clearTimeout(timer); | ||
| } | ||
| }; | ||
| try { | ||
| mkdirSync(bin); | ||
| const xcrun = join(bin, "xcrun"); | ||
| copyFileSync(join(import.meta.dir, "fixtures/shutdown-watch-xcrun.sh"), xcrun); | ||
| chmodSync(xcrun, 0o700); | ||
| writeFileSync(join(bin, "open"), "#!/bin/sh\nexit 88\n", { mode: 0o700 }); | ||
| const env: NodeJS.ProcessEnv = { ...process.env, PATH: `${bin}${delimiter}${process.env.PATH ?? ""}`, TMPDIR: root, TMP: root, TEMP: root }; | ||
| for (const key of Object.keys(env)) { | ||
| if (key.startsWith("SERVE_SIM_") || ["NODE_OPTIONS", "NODE_PATH", "DEBUG"].includes(key)) delete env[key]; | ||
| } | ||
| env.SERVE_SIM_TEST_QUERY_DIR = root; | ||
| env.SERVE_SIM_TEST_UDID = udid; | ||
| const port = await freePort(); | ||
| child = spawn(process.execPath, [join(import.meta.dir, "../index.ts"), udid, | ||
| "--port", String(port), "--host", "127.0.0.1", "--exit-on-simulator-shutdown"], { | ||
| env, stdio: ["ignore", "ignore", "pipe"], | ||
| }); | ||
| child.stderr?.on("data", (data) => { stderr += data; }); | ||
| child.once("close", () => { exited = true; resolveClosed(); }); | ||
| child.once("error", () => { exited = true; resolveClosed(); }); | ||
| const stateFile = join(root, "serve-sim", `server-${udid}.json`); | ||
| await waitFor(() => { | ||
| if (exited) throw new Error("CLI exited before readiness: " + stderr); | ||
| return existsSync(stateFile); | ||
| }, 10_000, 20); | ||
| const state = JSON.parse(readFileSync(stateFile, "utf8")); | ||
| expect(state.pid).toBe(child.pid); | ||
| expect(state.port).toBe(port); | ||
| await waitFor(() => { | ||
| if (exited) throw new Error("CLI exited before its boot query: " + stderr); | ||
| return existsSync(started); | ||
| }, 10_000, 20); | ||
| expect(existsSync(finished)).toBe(false); | ||
| const response = await fetch(`http://127.0.0.1:${port}/__shutdown_watch_probe__`, { signal: AbortSignal.timeout(1_500) }); | ||
| await response.arrayBuffer(); | ||
| expect(response.status).toBe(404); | ||
| expect(existsSync(finished)).toBe(false); | ||
| } catch (error) { | ||
| throw new Error("CLI shutdown-watch probe failed: " + stderr, { cause: error }); | ||
| } finally { | ||
| try { | ||
| if (child) { | ||
| writeFileSync(release, "release"); | ||
| if (existsSync(started) && !exited) await waitFor(() => existsSync(finished), 3_500, 20); | ||
| } | ||
| } finally { | ||
| try { | ||
| if (child) { | ||
| if (!exited) child.kill("SIGTERM"); | ||
| await waitClosed(2_000); | ||
| if (!exited) child.kill("SIGKILL"); | ||
| await closed; | ||
| } | ||
| } finally { | ||
| rmSync(root, { recursive: true, force: true }); | ||
| } | ||
| } | ||
| } | ||
| }, 20_000); |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: EvanBacon/serve-sim
Length of output: 22841
Script executed:
Repository: EvanBacon/serve-sim
Length of output: 34809
Revalidate helper ownership before recycling.
If another invocation replaces the helper while the boot query is pending, recycling can kill the new port holder or delete its state file. The live helper can then disappear from
--listand--kill. Coordinate recycling with state replacement and revalidate ownership after the query.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Accepting for v1. This cross-process registration race already existed on the sync path; #187 deliberately kept the narrow async-query fix without a shared ownership contract (see the reverted confirmation/retry work). Tracking ownership revalidation as follow-up rather than expanding this stall fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@EvanBacon Understood. You’re accepting the ownership race for v1 and addressing it in a follow-up.
Would you like me to open a GitHub issue for the follow-up, if one does not already exist?
You are interacting with an AI system.