fix(mobile): release leased device session before stopping daemon

GA
1d ago
+734-62

What and why

  • On Windows, stopping an agent-device@0.21.38 daemon runs SIGTERM (TerminateProcess), so the daemon exits without running finalizeDaemonLeases, never sends DELETE /session to BrowserStack/TestMu AI/Revyl, and omits daemon-shutdown.json (providerReleases.status: 'unknown').
  • Call client.leases.release(scope) with daemonLifecycle: 'attach' and cleared remote URL/token fields before stopDriver (packages/mobile/src/lease/index.ts:177-227, packages/mobile/src/lease/daemon.ts:62-76), so the live daemon ends the provider session via RPC before shutdown and never spawns a replacement if it already died.
  • Bound the RPC wait via DeviceReleaseContext.timeoutMs (packages/mobile/src/provider.ts:116,300) and releaseRpcBudgetMs (packages/mobile/src/lease/index.ts:63-71):
    • Reserve STOP_RESERVE_MS = 15_000 for daemon stop, giving the RPC 12_000 ms at the default 30_000 ms cleanupTimeout and scaling above 30_000 ms while keeping an 18_000 ms stop reserve (timeoutMs - 18_000).
    • Skip the RPC (return 0) when timeoutMs === undefined or when less than MIN_RELEASE_RPC_MS = 5_000 remains (< 20_000 ms), so short budgets do not cancel DELETE mid-flight and let daemon stop run directly.
    • Wrap rpcTimeout.signal.aborted errors as Timed out after <budget> ms waiting for lease RPC release (packages/mobile/src/lease/index.ts:201-203).
  • Throw ProviderReleaseWarningError (packages/mobile/src/lease/daemon.ts:50-55,74) on non-empty provider.warnings so provider rejections such as WEBDRIVER_SESSION_DELETE_FAILED always fail the release (or aggregate with stopError) even when stopDriver resolves (packages/mobile/src/lease/index.ts:226), while transport errors, timeouts, and released: false still fall back to daemon stop.
  • Allow assertSessionsEnded (packages/mobile/src/lease/daemon.ts:135-154) to accept status: 'unknown', an empty released array, or not-running only when sessionEnded was already confirmed by leases.release, while still rejecting missing providerReleases, pending releases, non-JSON output, or a dead daemon unless leases.release already confirmed the session ended.
  • Store scope right after startDriver returns and pass an explicit 30_000 ms budget and signal inside acquire's cleanup path (packages/mobile/src/lease/index.ts:133-149).
  • Document context.timeoutMs in docs/mobile.mdx, qualify the > 20 s cleanupTimeout requirement in docs/integrations/{browserstack,testmu,revyl}.mdx (plus ja/zh BrowserStack and TestMu AI translations), update factory JSDocs in packages/{browserstack,testmu,revyl}/src/provider.ts, and add .changeset/mobile-release-lease-before-stop.md.
  • Related agent-device issues/PRs: callstack/agent-device#4659 (merged, reports Windows SIGTERM exits as forced) and callstack/agent-device#4822 (follow-up for owner-only graceful shutdown on Windows).
flowchart LR
  R["provider.release()"] --> H["stopHeartbeat()"]
  H --> B{"rpcBudgetMs >= 5s?"}
  B -- "no (<20s or no timeoutMs)" --> S1["stopDriver(sessionEnded = false)"]
  B -- "yes" --> L["leases.release (attach-only RPC)"]
  L -- "ProviderReleaseWarningError" --> S1
  L -- "other error or false" --> S1
  L -- "released = true, no warnings" --> S2["stopDriver(sessionEnded = true)"]
  S1 -- "fails or ProviderReleaseWarningError" --> E["AggregateError / ProviderReleaseWarningError"]
  S2 -- "graceful, not-running, or win32 unknown" --> C["rm(dir)"]
const { released, provider } = await client.leases.release({ ...scope, signal });
const warnings = releaseWarnings(provider);
if (warnings.length > 0) throw new ProviderReleaseWarningError(warnings.join('; '));
return released;

Issue

Fixes #1723

Verified

Ran it locally on Windows 11 against bf19d33f (ed45764b only edited 1 line in .changeset/mobile-release-lease-before-stop.md to remove em dashes, verified with pnpm run check:changeset-bumps):

  • pnpm --filter @e2e-dev/mobile exec vitest run tests/unit/lease (with BROWSERSTACK_SESSION_DETAILS_ENDPOINT=http://10.255.255.1/x in the outer env and withRealMockLease overriding both BROWSERSTACK_WEBDRIVER_ENDPOINT and BROWSERSTACK_SESSION_DETAILS_ENDPOINT to the local 127.0.0.1 stub): 5/5 files, 44/44 passed, including all 5 real agent-device@0.21.38 daemon tests:
    • 5 s budget + 1.5 s DELETE 200 (RPC skipped, 3.1 s, daemon exited; rejects unconfirmed stop on Windows 0.21.38)
    • 16 s budget + 1.5 s DELETE 200 (RPC skipped, 3.0 s, daemon exited; rejects unconfirmed stop on Windows 0.21.38)
    • 30 s budget + 4 s DELETE 200 (RPC released, 7.3 s, passed on Windows, daemon exited)
    • 30 s budget + hung DELETE (RPC capped at 12 s with Timed out after 12000 ms waiting for lease RPC release, 14.8 s, daemon exited)
    • 30 s budget + DELETE 500 (rejected with WEBDRIVER_SESSION_DELETE_FAILED, 3.3 s, daemon exited)
  • Manual end-to-end check through built browserstack() (@e2e-dev/browserstack) at bf19d33f with 60 s budget and 13 s DELETE 200: passed in 14858 ms (aliveAfter: false, dirExistsAfter: false).
  • pnpm --filter @e2e-dev/revyl test:unit (19/19) and full pnpm run check (oxlint, fallow, monorepo build + tsc -r, check-error-codes, check-peer-ranges, check-changeset-bumps, docs:check across 244 MDX pages and 162 translations) passed at bf19d33f.
  • Did not run against live BrowserStack/TestMu AI/Revyl cloud devices locally (no provider credentials in this environment).