Skip to content

fix(desktop-gpui): Cap opens on clean Windows installs and never exits silently - #4256

Open
richiemcilroy wants to merge 18 commits into
mainfrom
fix/windows-startup-reliability
Open

richiemcilroy wants to merge 18 commits into
mainfrom
fix/windows-startup-reliability

Conversation

@richiemcilroy

@richiemcilroy richiemcilroy commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

Cap on Windows could fail to open at all, most often after the move to the GPUI app, and reinstalling never helped. Four separate causes, each reproduced on a clean Windows 11 24H2 VM with no GPU (WARP only) and fixed here. A user reported Cap "isn't opening" even after deleting everything and reinstalling; others hit "file in use" errors from background cap-cli processes during updates, or got stuck at onboarding.

flowchart TD
    A[Cap.exe launched] --> B{VC++ runtime present?}
    B -- "no (clean Win11)" --> X1["MSVCP140.dll was not found<br/>(reinstall cannot fix)"]
    B -- yes --> C{Instance mutex held?}
    C -- "yes, owner hung" --> X2[exit 0, nothing on screen]
    C -- no --> D{Early init error?}
    D -- yes --> X3[exit 1, nothing on screen]
    D -- no --> E{store parses?}
    E -- "no (NUL-filled)" --> X4["onboarding: Could not save<br/>onboarding progress, forever"]
Loading

Root causes

  • No VC++ runtime on clean Windows 11. Cap.exe imports MSVCP140.dll, VCRUNTIME140.dll and VCRUNTIME140_1.dll. cap-cli.exe, cap-muxer.exe, onnxruntime.dll, dxcompiler.dll and dxil.dll import them too (read from the PE import tables of the shipped 0.6.3). A clean Windows 11 24H2 has none of them in System32, and the installer doesn't ship them. 0.5.x was built by tauri build, which links the runtime statically (STATIC_VCRUNTIME). The GPUI app is built with plain cargo build and links it dynamically, so this started with the GPUI release.
  • A hung running copy makes every new launch exit silently. The second launch gets ERROR_ALREADY_EXISTS on the Local\CapGpui-* mutex and sends a reopen request that times out (os error 10060 in the log). It then calls SetForegroundWindow on a frozen window and exit(0). Linux has the same pattern (forward_linux_reopen error, then exit(0)). Stale cap-gpui.pid/cap-gpui.ipc files alone are harmless on Windows: the mutex decides, and a launch with a pidfile pointing at a reused pid (explorer.exe) starts normally.
  • Early failures had no visible error and lost their log lines. Instance-lock errors, endpoint errors and a main window that would not open all called exit(1)/cx.quit() with only a log line. That line was often lost too, because process::exit skips the non-blocking log writer's guard. A panic while gpui starts (for example failed to initialize Windows platform) only reached the crash file.
  • An unparseable settings store blocks onboarding forever. A zero-filled store (what NTFS can leave after a power loss) fails to parse. fix(desktop-gpui): recover onboarding from an empty settings store #3778 only covers empty and whitespace files. Every write then refuses, so Get Started shows "Could not save onboarding progress" on every press.
  • Updates replaced Cap.exe and then stopped on DLLs held by cap-cli. Any running cap-cli.exe (for example cap record start --detach) statically loads the FFmpeg DLLs. NSIS writes Cap.exe first, then hits Error opening file for writing: avcodec-61.dll with Abort/Retry/Ignore. Abort leaves Cap.exe from the new version, cap-cli.exe from the old one, and the registry still on the old version. A silent install (cap update) skips the locked files without telling anyone.

The #3594 reporter's missing logs folder is a location mix-up, not an early crash: Windows logs go to %LOCALAPPDATA%\so.cap.desktop\logs, not Roaming.

What changed

  • scripts/setup.js: copies msvcp140.dll, msvcp140_1.dll, vcruntime140.dll and vcruntime140_1.dll from the Visual Studio that links the build (VC/Redist/MSVC/<Microsoft.VCRedistVersion.default.txt>/<arch>/Microsoft.VC14*.CRT) into target/native-deps/vcruntime. scripts/bundle-gpui.mjs installs them next to Cap.exe (app-local deployment, so they are never older than the toolset that linked the binaries). desktop-release.yml now requires them in the installer.
  • packaging/windows/installer-hooks.nsh: new WaitForCapCommandLineTools in the pre-install and pre-uninstall hooks, after the MCP servers are ended. A fast FindProcessCurrentUser probe means PowerShell only starts when a cap-cli.exe/cap-muxer.exe exists. The check waits up to 10 s for any running from $INSTDIR, then asks Retry/Cancel. Cancel, or a silent install, aborts before any file is written.
  • src/startup.rs (new): startup::begin logs Cap starting version= pid= executable= as the first line and owns the log guard. startup::exit flushes the log before process::exit. startup::fail logs, writes the crash file synchronously, shows a native error ("Cap couldn't start", the reason, and the logs folder), then exits. A main-thread panic before the main window opens shows the same dialog from the panic hook.
  • src/single_instance.rs:
    • Windows: the running copy counts as hung when it does not acknowledge the reopen request, or when it does but its window does not answer WM_NULL within 5 s (the threshold Windows uses for "Not Responding"; the listener acknowledges from its own thread, so a frozen UI thread would otherwise still swallow the launch). The launch then asks "Cap is already running but isn't responding. Quit that copy and open Cap again?". After OK it re-takes the mutex and repeats the hand-off, and only ends the same pid the user confirmed if that copy is still stuck, so a copy that recovered or exited during the prompt is never touched. The pid comes only from this profile's cap-gpui.pid, or the pid in its cap-gpui.ipc when the pidfile is missing, never from other processes in the session. If neither identifies the owner, the launch shows an error instead of exiting silently. A Cap whose window still answers is activated as before. Launches take turns through an owned Local\CapGpuiLaunch-<profile> mutex, held until the pidfile and endpoint are published, and only the holder opens the instance mutex. Otherwise a launch still checking on the frozen copy kept that mutex alive after another launch ended the copy, and both stopped with "could not be reached". A launch that dies holding the lock leaves it abandoned, which counts as free.
    • Linux: the same prompt when the pidfile's Cap is alive but doesn't answer. Launches take turns through an exclusive flock on cap-gpui.lock, held until the pidfile and activation socket are published, so two launches can never both end the same hung copy and both carry on: the second waits, then hands off to the copy the first one started. After OK the launch repeats the hand-off and only ends the confirmed copy if it still doesn't answer, the same takeover_step rule as Windows. A pidfd is opened before the prompt and SIGTERM, then SIGKILL after 3 s, go through it, so a pid reused while the prompt is open can never be signalled.
    • Every former silent exit(1) now goes through startup::fail.
  • src/store.rs: only the two onboarding flags (set_has_completed_startup/set_has_completed_onboarding) set an unparseable store aside as store.unreadable-<ms> (bytes kept) and save to a fresh store. Every other writer still refuses an unparseable store, as before. While the store doesn't parse, its auth and presets are already invisible, so onboarding loses nothing more.
  • src/main.rs: a main window that fails to open shows the error instead of quitting silently.
  • New dialog.startup.* strings in all seven locales.

Callers checked

  • single_instance::acquire: only main. The macOS acquire path is unchanged apart from its error exits. A failed forward there still activates the app, which macOS shows (beachball, Force Quit), and live_legacy_instance_survives_failed_forwarding_without_metadata_changes keeps pinning that.
  • set_has_completed_*: only onboarding_window.rs:489 and :512.
  • platform::confirm_dialog is unchanged. On Windows it renders OK/Cancel, the same as every other Cap confirmation there.
  • crash_capture's panic hook still records first. The dialog is shown only on the main thread and only before startup::finished().

What could regress, and why it doesn't

  • A busy but healthy instance: the reopen ack comes from the listener thread, independent of the UI thread, and a window that answers WM_NULL is activated. Proven on the VM: a normal second launch hands off with no prompt, and with the pidfile deleted the healthy copy is brought forward, not quit.
  • Install time: the PowerShell step only runs when a CLI process exists. Fresh install 23.3 s vs 23.4 s on 0.6.3, over-install 22.3 s vs 22.3 s (same VM, silent mode).
  • App-local CRT shadowing a newer system copy: the DLLs come from the toolset that linked the binaries, so they always match the build.

Tests

  • single_instance::windows_takeover_tests (Windows CI): a pidfile naming a non-Cap, dead or garbage pid identifies no instance; the endpoint file identifies an instance whose pidfile is missing; a held lock with no identifiable instance is reported within 5 s; an instance that never acks reopen is reported, not activated; an instance with no endpoint or window is reported; an acking instance is handed off; ending a Cap.exe waits for its exit; a process not named Cap is never ended; a second launch waits for the launch lock until the first releases it; a launch that died holding it does not block the next; another profile never waits.
  • single_instance::linux_takeover_tests: SIGTERM ends a hung instance; one ignoring SIGTERM is killed after 3 s; a process that exits during the prompt is never signalled again (ESRCH through its pidfd); an already-exited instance starts a new one without asking; a responsive one is handed off without asking; Cancel leaves the hung copy running; OK ends it and this launch starts; a copy that answers again by the time OK is pressed is kept and handed off; a second launch waits on the lock, then hands off to the copy the first one started instead of starting another. Run 3 times in a row on Linux, 28/28 each.
  • single_instance::takeover_step_tests: only the confirmed pid is ended, and only once.
  • store::tests::onboarding_progress_sets_aside_an_unparseable_store (NUL-filled, truncated, null, []; other writers still refuse; bytes kept) and onboarding_progress_leaves_an_unreadable_store_alone.
  • scripts/bundle-gpui.test.mjs: VC runtime resources, and the hook order, path match, silent default and stack balance.
  • i18n::dialogs_surface_tests covers the new strings.

Proof (Windows 11 24H2, Azure D4s_v4, no GPU, no VC++ runtime installed)

Fixed build: release installer built from this branch's head. Before: the shipped 0.6.3 (and 0.6.2 for the update).

Case Before (0.6.3) After
Clean install, launch
Running copy hung, launch again second launch exits, nothing appears then OK:
NUL-filled store, Get Started
Unrecoverable start error (instance endpoint can't be written)
Update from 0.6.2 (/P /R /UPDATE) with cap record --detach running "Error opening file for writing avcodec-61.dll", Abort left Cap.exe new and cap-cli old no file touched; after cap record stop and Retry, every binary is the new build, Cap relaunches, and the recording is intact
Healthy copy, pidfile deleted, launch again existing copy kept, no prompt
  • Silent install (/S, the cap update path) with a recording running: exit code 2 in 15 s, every file unchanged, recording still running.
  • cap-cli version runs on the clean VM from the new install.
  • The log of a second launch now contains Cap starting ... pid=6052 and Cap GPUI is already running; bringing it forward pid=5012. Before this change, neither line reached disk.

macOS and Linux: CI builds and runs the full GPUI test suite on both. Neither shares the VC runtime or installer causes. Linux shared the hung-instance silent exit (fixed). macOS already activates the running app visibly.

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; no new actionable issue was established.

Summary

This PR improves desktop startup and Windows updates.

  • Clean Windows installs include the runtime Cap needs to open.
  • Windows updates wait for Cap command-line tools to release installed files.
  • A second launch can ask before replacing a Cap copy that is stuck.
  • Cap shows an error and keeps its startup logs when it cannot open.
  • Onboarding can continue when the settings store cannot be parsed.

The supplied previous threads have no comment numbers, so previousFindings is empty. Their original causes were checked against the current code: regular Windows reopen now checks the window, profile-wide process searches are gone, Linux holds a process reference and retries handoff after confirmation, and both platforms serialize recovery launches.

Reviews (14) · Last reviewed commit: "Merge remote-tracking branch 'origin/mai..." · Reviewed by Greptile

@vercel
vercel Bot temporarily deployed to Preview – cap-status November 14, 2026 05:50 Inactive
@vercel

vercel Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
cap-status Ignored Ignored Preview Nov 16, 2026 8:52pm UTC

@vercel
vercel Bot temporarily deployed to Preview – cap-status November 14, 2026 06:16 Inactive
@vercel
vercel Bot temporarily deployed to Preview – cap-status November 14, 2026 06:47 Inactive
@richiemcilroy
richiemcilroy marked this pull request as ready for review November 14, 2026 14:37
@sandchest-com

sandchest-com Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Sandchest

Sandchest reviewed 140d6dd: nothing exploitable found in this change.

Sandchest reviews every change for vulnerabilities an attacker could use · 14 credits · Review details

@capy-ai capy-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capy found 2 potential issues (2 medium).

View 1 other finding in Capy.

Open in Capy

@vercel
vercel Bot temporarily deployed to Preview – cap-status November 14, 2026 15:54 Inactive
@richiemcilroy

Copy link
Copy Markdown
Member Author

hey @greptileai, please re-review the PR

@capy-ai capy-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capy found no issues.

View 1 other finding in Capy.

Open in Capy

@vercel
vercel Bot temporarily deployed to Preview – cap-status November 14, 2026 23:12 Inactive
@richiemcilroy

Copy link
Copy Markdown
Member Author

hey @greptileai, please re-review the PR

@capy-ai capy-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capy found no issues.

View 1 other finding in Capy.

Open in Capy

@capy-ai capy-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capy found no issues.

Open in Capy

@richiemcilroy

Copy link
Copy Markdown
Member Author

hey @greptileai, please re-review the PR

@vercel
vercel Bot temporarily deployed to Preview – cap-status November 15, 2026 15:38 Inactive

@capy-ai capy-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capy found no issues.

View 2 other findings in Capy.

Open in Capy

@vercel
vercel Bot temporarily deployed to Preview – cap-status November 16, 2026 09:48 Inactive
@vercel
vercel Bot temporarily deployed to Preview – cap-status November 16, 2026 09:56 Inactive
@richiemcilroy

Copy link
Copy Markdown
Member Author

hey @greptileai, please re-review the PR

Two launches recovering the same frozen copy could both stop with "could
not be reached": each opened the instance mutex to check on the copy, so
whichever launch ended it found the mutex kept alive by the other launch
with no process behind it. Launches of a profile now take turns through
an owned Local\CapGpuiLaunch mutex, held until the pidfile and endpoint
are published, so only the holder ever opens the instance mutex and the
next launch hands off to the copy it started. A holder that dies leaves
the lock abandoned, which counts as free.

Also expect the Linux launch lock file in the first-launch profile test.
@vercel
vercel Bot temporarily deployed to Preview – cap-status November 16, 2026 10:24 Inactive
@richiemcilroy

Copy link
Copy Markdown
Member Author

hey @greptileai, please re-review the PR

@capy-ai capy-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capy found no issues.

View 2 other findings in Capy.

Open in Capy

This branch was successfully deployed

1 active and 1 inactive (outdated) deployments
Preview – cap-web — 140d6dd3 Deployed Nov 16, 2026 by vercel[bot]
Preview – cap-status — 8070639e Deployed Nov 16, 2026 by vercel[bot]
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.

1 participant