fix(host-service): share one in-flight connect in getDaemonClient()

BR
1d ago
+130-15

What & why

Fixes #9452.

connecting was set only after await ptyDaemonSocketPath(), so two first callers each opened a connection and one was never disposed. The in-flight promise is now set before the first await.

Left out (already there before): disposeDaemonClient() during a connect can let the old finally clear a newer connecting. Only tests and shutdown call it.

How I tested it

Failing regression test with a fake socket server through the existing SUPERSET_PTY_DAEMON_SOCKET override, fix on top. The bug was first found by checking every order of events in a small model, then reproduced against the real code.

  • packages/host-service: bun run test gives 2755 pass, 0 fail.

Checklist

  • PR title follows conventional commits (type(scope): subject)
  • bun run lint and bun run typecheck pass (CI fails on lint warnings too)
  • "Allow edits from maintainers" is checked on fork PRs
  • I have signed the CLA (the bot asks on your first PR)

Summary by cubic

Fixes #9452. getDaemonClient() now shares a single in-flight connect, so concurrent first callers no longer open duplicate daemon connections where one is never cached or disposed.

  • The in-flight promise is set before the first await instead of after resolving the socket path.
  • A connect that outlives disposeDaemonClient() no longer replaces the newer cached client, and its failure can no longer duplicate the fresher connect.
  • A failed connect disposes its partially initialized client so later attempts still succeed.
  • Adds regression tests using a fake socket server covering all three cases.

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability when multiple requests connect to the terminal daemon at the same time, ensuring they share a single connection.
    • Improved recovery after connection failures so later attempts can connect successfully.
    • Prevented a connection attempt started before disposal from replacing a newer client after reconnection.