Repository navigation
fix: prevent periodic stalls in detached simulator streams #187
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
kiendle
wants to merge
9
commits into
EvanBacon:main
from
kiendle:fix/nonblocking-simulator-shutdown-watch
Closed
Changes from all commits
Commits
Show all changes
9 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 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
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.