Skip to content

perf(sdk): make CopilotClient.start() single-flight - #2561

Draft
DonJayamanne wants to merge 1 commit into
github:mainfrom
DonJayamanne:perf/boot-04-single-flight-2
Draft

perf(sdk): make CopilotClient.start() single-flight#2561
DonJayamanne wants to merge 1 commit into
github:mainfrom
DonJayamanne:perf/boot-04-single-flight-2

Conversation

@DonJayamanne

Copy link
Copy Markdown
Contributor

Summary

CopilotClient.start() was not single-flight: concurrent first calls (including
auto-start from createSession() / resumeSession()) could each spawn a
separate CLI process. The client only tracked the last this.cliProcess, so
stop() could not terminate the earlier ones — a duplicate cold-start cost and a
real process leak.

This guards start() with a shared in-flight startPromise: concurrent callers
await one startup, and the guard is cleared on completion so a failed start can
be retried. The original start sequence moved into a private doStart(); startup
order is unchanged and no public API changed.

Closes #2560

Changes

  • nodejs/src/client.ts: add startPromise; start() returns the in-flight
    start when one is running; doStart() holds the original startup sequence.
  • nodejs/test/client.test.ts: regression tests for single-flight and
    retry-after-failure.

Verification

  • npm test -- test/client.test.ts -t 'single-flight' → 2 passed.
  • tsc --noEmit → clean.
  • Manual reproduction (3 concurrent createSession() on a fresh client):
    3 → 1 CLI process spawned; 2 → 0 orphaned after stop().

Notes

  • This branch is based on an earlier main; a rebase may be needed before merge.

Concurrent createSession()/resumeSession() on a fresh client each auto-start
the CLI; without an in-flight guard the second spawn overwrote this.cliProcess
and orphaned the first, which stop() never terminates. Guard start() with a
shared startPromise so concurrent callers join one spawn; clear it on completion
so a failed start can retry.

Adds regression tests (single-flight + retry-after-failure).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@DonJayamanne
DonJayamanne force-pushed the perf/boot-04-single-flight-2 branch from b7f6793 to 937b35f Compare September 8, 2026 01:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CopilotClient.start() is not single-flight — concurrent auto-start spawns duplicate CLI processes

1 participant