Skip to content

Cache and back off Web Inspector socket discovery #704

@shaun0927

Description

@shaun0927

Tracked by #713.

Background

Web Inspector socket discovery can run lsof, ps, and filesystem scans repeatedly during proxy startup. This is valid work, but repeated discovery increases startup latency.

Objective

Reduce repeated socket discovery cost while preserving correctness for booted simulator selection.

Scope

  • Add short TTL cache for UDID-to-socket path results.
  • Invalidate cache after failed socket probe.
  • Use exponential or staged backoff during wait loops.
  • Keep existing lsof and mtime fallback behavior.

Target files and paths

  • src/simulator/socket-finder.ts
  • src/simulator/proxy.ts
  • tests/unit/socket-finder.test.ts
  • tests/unit/proxy-timing.test.ts if proxy wait behavior changes

Implementation steps

  1. Add a cache keyed by target UDID and no-target lookup separately.
  2. Store only socket paths that pass probeSocket.
  3. Invalidate cached path on failed probe.
  4. Replace fixed 500ms retry with staged backoff.
  5. Add tests for cache hit, stale cache invalidation, and no-target behavior.

Acceptance criteria

  • Repeated discovery for the same UDID reuses a recently probed live socket.
  • Stale cached sockets are invalidated and rediscovered.
  • No-target discovery does not incorrectly return a socket for a requested UDID.
  • Existing fallback paths remain available.

Verification

  • npm run lint -- --quiet
  • npm test -- --runInBand tests/unit/socket-finder.test.ts
  • Manual macOS smoke: boot simulator, start proxy twice, confirm second discovery is faster or avoids repeated expensive commands in debug logs.

Dependencies / recommended order

Run after proxy readiness split if both touch startup timing. This issue is independent of HTTP/security work.

Risks and cautions

  • Do not return unprobed socket paths from cache.
  • Do not cache failures for so long that newly booted simulators are missed.
  • Do not delete stale sockets unless they were explicitly probed stale.

Non-goals

  • Do not change proxy port allocation.
  • Do not replace lsof fallback entirely.
  • Do not introduce platform support beyond macOS/Xcode simulator needs.

Ambiguity reducers

  • “Cache hit” requires a live probe success within the TTL.
  • “Backoff” must still respect the configured overall timeout.
  • Target-specific and target-agnostic cache entries are separate.

Validity review

Proceed as a focused speed issue. It supports startup responsiveness without changing automation semantics.

Implementation detail refinements

  • Make TTL configurable for tests.
  • Keep cache in module or finder instance with a reset hook for tests.
  • Expose debug counters only in tests or verbose logs.

Verification checklist reinforcement

  • Live socket cache hit avoids lsof in unit test mock.
  • Stale cache triggers rediscovery.
  • Backoff loop respects total timeout.
  • Existing mtime fallback test still passes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    automation-roadmapOpenSafari automation roadmap work itemsperformancePerformance optimization

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions