Skip to content

fix(desktop): survive slow first launch of downloaded postgres - #293

Merged
nixuuu merged 1 commit into
devfrom
fix/desktop-postgres-first-run
Nov 6, 2026
Merged

nixuuu merged 1 commit into
devfrom
fix/desktop-postgres-first-run

Conversation

@nixuuu

@nixuuu nixuuu commented Nov 6, 2026

Copy link
Copy Markdown
Member

Evidence

A colleague ran nightly 0.2.0-nightly.92 on a fresh MacBook Pro (M5 Pro, macOS 27.0.1). The first local setup failed twice with Downloaded PostgreSQL could not run on this computer. The desktop log had only that generic line. "Try again" sometimes looked stuck on "Setting up PostgreSQL…". The 7th attempt worked, and initdb, postgres, migrations, gateway and worker then started normally.

Measured on a Mac Studio, macOS 27.0.1, idle, using the installer's own steps in a temp dir (real npm download, SHA-512, extract, preparePostgres, --version):

Run postgres pg_ctl initdb
cold, fresh files (3 runs) 3010–3608 ms 146–162 ms 309–431 ms
second launch, same files 20–24 ms 3–5 ms 12–14 ms
fixed installer end to end, same pin already installed by Canopy elsewhere 3376 ms 146 ms 374 ms
fresh files with com.apple.quarantine hung until killed at 180 s (SIGTERM, exit 143); syspolicyd logged Unable to initialize qtn_proc every 2 s
quarantine set, then removed with xattr -dr 3010 ms 151 ms 309 ms

Files extracted by Canopy carry only com.apple.provenance, not quarantine. preparePostgres (thin plus ad-hoc signing of 113 Mach-O files) takes 4.4–5 s.

Root cause

macOS checks each newly written executable on its first launch, and the slow part is postgres, which loads ICU, OpenSSL and libxml2. The cost depends on the files, not their content: every retry extracts a new runtime and pays it again. The smoke check gave each binary a fixed 10 s AbortSignal.timeout. On a freshly installed, busy Mac that first launch went past 10 s. Any timeout or kill turned into the same generic error, with no detail in the log. Later attempts passed once the Mac was less busy, not because of a cache.

Fix

  • postgres-check.ts: the three --version checks share one hard budget of 3 min. After 5 s the setup screen shows "Checking PostgreSQL with macOS security… This can take a minute the first time."
  • On timeout the child gets SIGKILL. Waiting for its exit and for stdout/stderr is bounded by a 2 s grace, so a retry cannot hang. Cancelling kills the running check at once.
  • The error names the cause: timeout, blocked by the OS (signal), failed run, or wrong version. logs/desktop.log gets the binary, the exit code or signal, whether the timeout fired, the elapsed time, and trimmed stdout/stderr with the runtime path replaced by <runtime>. A successful install logs the first-launch times.
  • On darwin, preparePostgres removes com.apple.quarantine from the extracted runtime. Integrity still comes from the pinned SHA-512, checked before extraction.
  • Retries already reused the verified package.tgz.partial; a test now covers it, and that no extracted/ or install/ directory is left after a failure.
  • The setup screen drops Electron's Error invoking remote method 'canopy:configure': Error: prefix.
  • Confirmed that another PostgreSQL cannot interfere: binaries run by absolute path, the service env allowlist has no PG* or DYLD*, Postgres uses a random port on 127.0.0.1 with no Unix socket (-k ""), and it has its own data dir. Documented in docs/desktop.md.

Fixes: #23: #23 moved PostgreSQL from the signed bundle to a runtime download, which runs freshly written binaries through the 10 s check.

Tests

apps/desktop/test/postgres-check.test.ts uses an injectable spawn and short budgets:

  • a launch stuck until SIGKILL;
  • a process that never exits or closes its output, which stays bounded;
  • a slow launch that shows progress;
  • signal, exit and version classification, with no path in the message;
  • cancellation;
  • a real installer retry against the fixture registry: the first attempt fails, the download is reused (1 request), and the stage directories are cleaned up.

bun install, gen:check, typecheck, lint, format:check, check:tmp (1530 pass, 0 fail) and the web build are all clean.

Not verified

  • A real fresh macOS 27 MacBook running the packaged app, where the controller is a child of Canopy.app. The Mac Studio tests ran over SSH. The 3 min budget is a judgement, not a measured worst case.
  • What a GUI-session app sees when it launches a quarantined binary (a Gatekeeper dialog or a block). Canopy's extraction does not add quarantine, so the removal is a precaution.
  • The hang on "Try again" could not be reproduced directly. The old code could appear stuck for up to about 35 s (5 s of synchronous preparation plus 3 × 10 s with no new status) and had no bound on reading stdout. Both are now bounded and visible.

Copy audit (docs/copy.md §8)

Before After Why
Downloaded PostgreSQL could not run on this computer. Check the desktop logs and OS security policy, then try again. (every failure) timeout: PostgreSQL did not respond within 3 min. macOS may still be checking it for security. Try again. names the likely cause; one idea per sentence
same signal: macOS stopped PostgreSQL from running. Check System Settings → Privacy & Security, then try again. (other OS: Your computer stopped PostgreSQL from running. Check your security software, then try again.) concrete action and settings path
same wrong version: Downloaded PostgreSQL reports an unexpected version. Try again. separate cause
same other failures: Downloaded PostgreSQL could not run on this computer. Check the desktop logs, then try again. drops the vague "OS security policy"
— progress after 5 s: Checking PostgreSQL with macOS security… This can take a minute the first time. (other OS: Checking PostgreSQL… This can take a minute the first time.) §4.6: -ing verb with ellipsis, says it happens only the first time
Error invoking remote method 'canopy:configure': Error: … … no internal jargon on the surface

Checks: sentence case; each error ends with an action; no paths or jargon; no humour in errors; sentences ≤ 20 words. Glossary: no new terms. The texts sit in the existing startup alert and status line, so the layout is unchanged.

macOS checks each newly written executable on its first launch. A cold
`postgres --version` took 3-3.6 s on an idle Mac; on a freshly installed,
busy Mac it went past the old 10 s limit per binary and failed with a
generic error. Each retry extracts new files, so it pays the cost again.

- Run the three `--version` checks in one hard 3 min budget, with a
  "Checking PostgreSQL with macOS security…" status after 5 s.
- SIGKILL on timeout. Waiting for the exit and the output is bounded too,
  so a retry cannot hang.
- Name the cause (timeout, blocked by the OS, failed run, wrong version)
  and log the binary, exit code or signal, the timeout, the timing and
  trimmed stdout/stderr.
- Remove com.apple.quarantine from the verified, extracted runtime. A
  quarantined binary waited in syspolicyd until killed.
- Drop Electron's IPC prefix from setup errors.

Fixes: #23
@nixuuu
nixuuu merged commit d88ae8f into dev Nov 6, 2026
9 checks passed
@nixuuu
nixuuu deleted the fix/desktop-postgres-first-run branch November 6, 2026 19:28
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