Skip to content

Shell environment refresh can permanently fail concurrent thread starts #2380

Closed

Description

@ymichael

Summary

Two concurrent provider-managed thread starts can become permanent error threads when a shell-environment refresh invalidates their shared provider-installation probe. The workspaces provision successfully, but both thread.start commands fail with Runtime shutting down instead of transparently checking the provider again and continuing.

Versions and environment

Steps to reproduce

The timing is intermittent in the product and deterministic at the host-daemon command layer:

  1. Start two installation-managed provider threads concurrently so they share one in-flight providerInstallationStatus promise.
  2. While that promise is pending, let replaceBaseShellEnv observe a changed PATH. It clears the installation gate and shuts down the maintenance runtime.
  3. The runtime rejects the shared status request with Runtime shutting down.
  4. Observe both accepted thread.start commands reject instead of re-probing in the new gate generation.

Live occurrences:

They were created 1.5 seconds apart and failed at the same millisecond after sharing the status probe.

Expected vs actual

Actual:

Command thread.start failed
Runtime shutting down

Both thread timelines stop after successful workspace provisioning. There is no provider output, requested command execution, or final response.

Expected:

Invalidating a provider-installation probe should cause waiting thread starts to join a fresh probe against the new shell environment. If that probe succeeds, both real threads should continue starting.

Evidence

The thread-start preflight awaits the shared installation gate:

async function requireSupportedProviderCliForThreadStart({
command,
options,
}: RequireSupportedProviderCliArgs): Promise<void> {
if (!command.bridgeLaunch.capabilities.providerInstallation) {
return;
}
const requirement =
command.type === "thread.rewind.prepare"
? ("thread_rewind" as const)
: undefined;
// A changed PATH can resolve a different provider binary, and the runtime
// manager only learns about one through this refresh: it clears the gate
// and evicts idle runtimes so the thread launches against the new env. It
// has to run before the memo is consulted, since a hit would otherwise
// skip the probe that used to carry the refresh, and before the gate
// samples its generation, so the clear does not discard the fresh probe.
// The refresh's own short TTL keeps back-to-back starts free.
await options.refreshShellEnv();
// The probe spawns the provider CLI several times, so a remembered
// supported answer is served without touching the maintenance bridge or
// re-verifying the bridge artifact.
const status = await options.runtimeManager.providerInstallationGate.run(
providerInstallationGateKey({
providerId: command.providerId,
bridgeLaunch: command.bridgeLaunch,
requirement,
}),
async () => {
const bridgeLaunch = await resolveRuntimeBridgeLaunch(
command.bridgeLaunch,
options,
);
return options.providerInstallationStatus({
providerId: command.providerId,
bridgeLaunch,
...(requirement !== undefined ? { requirement } : {}),
});
},
);

A changed shell environment clears the gate and immediately shuts down the maintenance runtime:

async replaceBaseShellEnv(
shellEnv: NonNullable<AgentRuntimeOptions["shellEnv"]>,
): Promise<void> {
if (shellEnvEquals(this.baseShellEnv, shellEnv)) {
return;
}
this.baseShellEnv = { ...shellEnv };
// A new PATH can resolve a different provider binary, so the remembered
// version check no longer describes what a thread would run.
this.providerInstallationGate.clear();
await this.shutdownProviderMaintenanceRuntime();
await this.evictIdleRuntimeEntries();

The gate avoids caching a pre-invalidation answer, but still returns that stale promise or rejection to callers that were already waiting:

return {
clear() {
generation += 1;
settledByKey.clear();
pendingByKey.clear();
},
run(key, probe) {
const currentTime = now();
const settled = settledByKey.get(key);
if (settled !== undefined) {
if (settled.expiresAt > currentTime) {
return Promise.resolve(settled.status);
}
settledByKey.delete(key);
}
const pending = pendingByKey.get(key);
if (pending !== undefined) {
return pending;
}
const startedGeneration = generation;
const started = probe()
.then((status) => {
const settledAt = now();
// Expired neighbours are swept here rather than on a timer so the
// map stays bounded without keeping the process alive.
pruneExpired(settledAt);
// Bridges report `versionUnsupported: false` for a CLI that is not
// installed at all. Only a bridge that enforces a minimum version
// (codex, pi) can turn a too-old CLI installed in the meantime,
// without a PATH change, into an unsupported answer, so only its
// not-installed answer is worth forgetting. A bridge that reports
// `minimumSupportedVersion: null` (Claude Code, ACP) can never
// reject, and its launcher finds the executable on its own, so its
// not-installed answer is remembered like a supported one.
if (
(status.installed || status.minimumSupportedVersion === null) &&
!status.versionUnsupported &&
startedGeneration === generation
) {
settledByKey.set(key, { status, expiresAt: settledAt + ttlMs });
}
return status;
})
.finally(() => {
if (pendingByKey.get(key) === started) {
pendingByKey.delete(key);
}
});
pendingByKey.set(key, started);
return started;
},

Relevant host-daemon log for both starts:

type="thread.start" message="Runtime shutting down"
at shutdownProviderMaintenanceRuntime
at replaceBaseShellEnv

What you ruled out

  • The personal workspaces provisioned successfully, so this was not workspace provisioning failure.
  • Neither provider session began; the prompts and requested shell command were never processed.
  • Both independent environments failed simultaneously, ruling out workspace-local state.
  • Stable provider-probe failures still surface normally; only a probe invalidated by a gate generation change should retry.
  • Searched open and closed issues for Runtime shutting down, thread start, provider maintenance, and shell-environment refresh; no duplicate found.

Suggested priority and effort

High. The race silently turns accepted user messages into permanent error threads. Retrying in a new thread is a workaround, but the original work does not recover. The fix is localized to the shared provider-installation gate.

  • Reproduced from live thread evidence and with a deterministic regression test on the base commit.
  • Searched open and closed issues for the same problem.
  • Agent-written report links the investigation thread.

AGENT GENERATED

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions