Repository navigation
Description
Type: Bug
Prompt/agent/skill/hook discovery reads every visible customization file with an unbounded Promise.all. On profiles with a large number of agent-plugin, skill and extension-contributed customization files, a burst of discovery passes exhausts the process file handle limit and the window becomes unresponsive with EMFILE: too many open files.
This is reproducible with no folder open, because the files involved are global (~/.copilot, ~/.claude, .vscode/extensions, settings.json, mcp.json).
Symptoms
The main process reaches ~150k pending libuv filesystem requests and ~9.5k OS handles, after which unrelated reads fail:
[error] Unknown (FileSystemError): Error: EMFILE: too many open files, open 'c:\Users\<user>\AppData\Roaming\Code\User\settings.json'
[error] Unknown (FileSystemError): Error: EMFILE: too many open files, open 'c:\Users\<user>\AppData\Roaming\Code\User\mcp.json'
[error] [computeAgentDiscoveryInfo] Failed to parse agent file: .../azqr-cost-optimize.agent.md Error: EMFILE: too many open files
[error] [computeSkillDiscoveryInfo] Failed to validate Agent skill file: .../SKILL.md Error: EMFILE: too many open files
[error] ChatSessionStore: Error writing chat session Unable to write file '...chatSessions/<id>.jsonl' (EMFILE: too many open files)
[warning] No ptyHost heartbeat after 6 seconds
[info] Extension host (LocalProcess pid: NNNNN) is unresponsive.
Once handles are exhausted the failure spreads well beyond chat: the terminal pty host misses heartbeats, chat sessions fail to persist, and extension hosts are marked unresponsive.
Cause
Three discovery paths in src/vs/workbench/contrib/chat/common/promptSyntax/service/promptsServiceImpl.ts fan out over every discovered file without a concurrency bound, and each iteration performs a fileService.readFile via parseNew:
computeSlashCommandDiscoveryInfo—Promise.all(slashCommandFiles.map(...))(prompts + skills)computeAgentDiscoveryInfo—Promise.all(allAgentFiles.map(...))computeHooks—Promise.all(hookFiles.map(...))
Two properties turn this into handle exhaustion rather than a slow scan:
-
No concurrency limit. Every discovered file is opened at once. Note that
computeSkillDiscoveryInfoandgetInstructionsDiscoveryInfoin the same file already iterate sequentially, so the exposure is limited to the three sites above. -
Invalidation does not cancel in-flight work. In
CachedPromise, a change event clears the cached promise but leaves the running computation going:this._register(this.getEvent()(() => { this.cachedPromise = undefined; delayer.trigger(() => this.onDidUpdatePromiseEmitter.fire()); }));
The
Delayerdebounces the notification, not the recompute, so successive invalidations stack multiple full scans concurrently.
The fan-out is easy to trigger because ExtensionPromptFileService invalidates all prompt types from a single context key change:
this.contextKeyService.onDidChangeContext(e => {
if (e.affectsSome(this._contributedWhenKeys)) {
for (const type of ALL_PROMPT_TYPES) {
this._onDidChange.fire({ type });
}
}
});So one setting change on an extension that contributes many when-gated customizations invalidates every type at once, and each type re-reads its full file set.
In the profile that produced the logs above, the recursively scanned customization roots held 1,256 directories / 2,621 files (735 SKILL.md, 34 agent files, 30 instruction files). Discovery costs several filesystem operations per file — readSkills performs a pathExists per candidate, a resolve per directory, and isResolvedWithin issues two realpath calls per skill in addition to the readFile — so one full pass is on the order of 10k operations, and the observed ~150k pending operations corresponds to roughly fifteen passes in flight simultaneously.
Notes
Bounded concurrency is already the established pattern elsewhere in the codebase — Limiter is used 35+ times under src/vs/platform, including for directly comparable discovery work (agentService.ts uses new Limiter<boolean>(4) for session discovery). These three call sites simply don't use it.
I've opened a PR that bounds the three unbounded sites with Limiter, which addresses the handle exhaustion. The non-cancelling invalidation in CachedPromise is left as a separate follow-up since it changes cancellation semantics and is better reviewed on its own.
VS Code version: 1.134.0 (commit 110a328)
OS version: Windows 11
Activity