Skip to content

fix(gateway): stop scheduled worktree scans during shutdown - #1320

Merged
nixuuu merged 1 commit into
devfrom
perf/speed-up-updates
Nov 26, 2026
Merged

nixuuu merged 1 commit into
devfrom
perf/speed-up-updates

Conversation

@nixuuu

@nixuuu nixuuu commented Nov 26, 2026

Copy link
Copy Markdown
Member

Incident

Five gateway updates spent 217–453 seconds waiting for background jobs during shutdown. In the latest case, 148 worktree scans completed during five full minutes of the 390-second wait. The new gateway was listening about two seconds after the wait ended. Local Desktop runs the same cleanup scheduler.

Changes

  • Stop the scheduled inventory scan on shutdown. Cancel outstanding reads from workers and merge checks, and leave the remaining worktrees for the next pass.
  • Once a removal is reserved, finish the worker command and persist its result. Report the individual background job if shutdown takes more than five seconds.
  • Manual cleanup and preview keep their existing behavior.

Verification

The integration regression reproduced the hang on the base revision with a worker reply held open. The fixed gateway stops while that reply is held. A separate integration test holds a removal acknowledgement and verifies its database result after shutdown.

Locally, 36 focused tests, gateway typechecking, and lint on the changed files passed. A separate Linux Local Desktop check exercised worker, gateway and PostgreSQL shutdown. Native macOS and Windows installer timing was not measured. Full repository checks run in CI.

Fixes: #82

Stop waiting for read-only worker scans and merge checks when the gateway shuts down. Finish reserved deletions before releasing the database and name slow background jobs in the shutdown log.

Fixes: #82
@nixuuu
nixuuu enabled auto-merge (squash) November 26, 2026 18:43
@nixuuu
nixuuu merged commit 1ba8c60 into dev Nov 26, 2026
24 checks passed
@nixuuu
nixuuu deleted the perf/speed-up-updates branch November 26, 2026 18:48
nixuuu added a commit that referenced this pull request Nov 26, 2026
## Change

The hourly cleanup, startup scan and worker reconnect used to fetch and
inspect each managed checkout on an available worker. A read-only
production snapshot on November 26 had 287 live entries. After external
entries and offline workers, 276 could receive a scan. Of those, 1 was
busy, 40 were protected by recent archival and 208 could not match the
enabled policy. The remaining 27 shared one worker, repository path and
remote. Worker availability in this estimate uses the database lease.

The gateway now checks the owner-scoped inventory and effective policy
before sending a scheduled scan. A scope snapshot only filters
candidates. Each admitted candidate uses current settings and its own
checkout status. After one successful scan, later candidates for the
same repository path and remote reuse fetched refs on that worker
connection for the rest of the pass. The worker also skips a duplicate
merge check when both ref names are identical.

Reserved commands retain their replay path. The worker fetches again and
checks the expected HEAD, local changes and required merge evidence
before removing a checkout. Manual preview and execution still scan each
managed checkout in scope. A newly merged branch can be picked up at the
following refresh.

## Isolated measurement

Three alternating pairs of runs through the global scheduler tick, using
a fresh PostgreSQL database, a real gateway, a WorkerHost and local bare
remotes per run. The baseline has the same source tree as the merged
shutdown fix in #1320. Each of three projects had 12 worktrees on one
worker with 512 tracked files per repository. The policies were merged,
all rules disabled, and archived or 30 days inactive. The fixture
included busy threads, recent and older archives, dirty files and
unpushed commits.

| Per pass, median of three runs | Before | After |
| --- | ---: | ---: |
| Wall time | 6.403 s | 2.312 s |
| Scan RPCs / cumulative time | 36 / 5.164 s | 10 / 1.179 s |
| Removal RPCs / cumulative time | 5 / 1.056 s | 5 / 0.966 s |
| Top-level worker Git processes / cumulative Git time | 980 / 3.013 s |
351 / 0.950 s |
| Fetch | 41 / 855 ms | 7 / 153 ms |
| Status | 41 / 439 ms | 15 / 136 ms |
| Git commands during merge verification | 258 / 591 ms | 79 / 185 ms |
| SQL executions / cumulative client time | 168 / 243 ms | 107 / 159 ms
|

All runs removed the same five checkouts on disk and in PostgreSQL. The
seven remaining fetches comprise two candidate-repository refreshes and
five pre-removal rechecks. RPC durations include worker execution, so
the Git timings overlap them. The local remotes exercise Git merge
detection without network latency or a GitHub API account. The
measurement harness runs outside required CI.

## Verification

68 integration tests passed locally across worktree-lifecycle,
worktree-merged, worktree-closed-cleanup and worktree-github, with a
clean check:tmp result. New regressions exercise the skipped scan RPC
plus full manual preview, and the loss of the remote between the first
removal and the next candidate. In the latter case the worker rejects
the reserved removal and the gateway releases the reservation. The suite
also covers reconnect replay, cancellation during a read, settlement
after shutdown begins, and owner/project-scoped merge proof.

Lint and formatting passed on the six edited TypeScript files. The
repository-wide typecheck, builds and remaining tests run in CI.

The first CI run (733fe3e) exposed a Git-version difference in the new
regression fixture. Pushing by URL had already advanced the worker’s
tracking ref on the runner. The fixture now advances the bare repository
from the other side, leaving that tracking ref stale until the scheduled
fetch. All seven worktree-merged tests passed locally with the adjusted
fixture. The first run also rejected the skill preview in
config-sync-environment.test.ts with a stale-report error on shard 8.
That focused linked-computer test passed locally on the original
revision.

Fixes: #82

Related: #1320
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