Skip to content

fix(crawler): prevent concurrent crawl resumes - #1119

Closed
WilliamK112 wants to merge 1 commit into
squirrelscan:mainfrom
WilliamK112:codex/prevent-concurrent-resume-1026
Closed

WilliamK112 wants to merge 1 commit into
squirrelscan:mainfrom
WilliamK112:codex/prevent-concurrent-resume-1026

Conversation

@WilliamK112

Copy link
Copy Markdown
Contributor

Summary

  • claim a crawl lease synchronously before audit --resume or crawl --resume continues an interrupted crawl
  • start a new crawl with a PID/host warning when a live owner already holds the lease, while preserving stale and dead-owner takeover
  • cover the race with two real Bun child processes competing for one SQLite crawl
  • document the resume behavior and add the changelog entry

Closes #1026

Verification

  • bun test packages/crawler/tests/crawl-lease.test.ts apps/cli/tests/audit/crawl-lease-claim.test.ts (18 passed)
  • bun run --cwd packages/crawler typecheck
  • bun run --cwd apps/cli typecheck
  • bun run --cwd apps/cli lint
  • bun test apps/cli/tests/controllers/crawl.test.ts apps/cli/tests/audit/stop-interrupted-crawl.test.ts (11 passed, 1 skipped network integration test)
  • bun run docs:check (61 passed)
  • app source formatting check with the repository oxfmt config
  • regression test was also verified to fail when the competing-owner rejection was temporarily removed

Checklist

  • I added or updated tests for behavioral changes.
  • I did not include secrets, customer data, or incompatible third-party material.
  • Every commit includes a DCO Signed-off-by line.

Docs: docs/cli/audit.mdx and docs/cli/crawl.mdx updated. Skill: none.

@nc9

nc9 commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Thanks @WilliamK112, nice fix: claiming before the resume decision is the right spot, and reusing the #946 compare-and-set keeps it tight. crawl-lease.test.ts passes locally and leaves no processes or temp files on a clean run. A few things before merge:

  1. Harden the child-process test. It has no explicit timeout, so Bun's default 5 s per-test timeout fires before your own 5 s deadline can. Under load it fails as a timeout, afterEach removes the temp paths, the finally then recreates the release file, and if the runner exits first both children poll forever. Please add an explicit timeout above the internal deadline (for example }, 20_000)), give the child its own deadline (exit non-zero after about 15 s), kill() any child still running in the finally, spawn both children inside the try, and use stderr: "inherit".
  2. Docs: define stale and the way out. In cli/audit.mdx and cli/crawl.mdx, say a lease is stale after 5 minutes without a heartbeat, and that you can stop the other process (Ctrl-C releases it at once), wait 5 minutes, or use --refresh for a fresh crawl. Link the lease section in cli/self.mdx.
  3. Live but silent owner on the same host. claim() takes over any lease older than 5 minutes, but isCrawlAbandoned (and cli/self.mdx) treat a same-host owner whose pid is alive as live for up to 24 h, for exactly the suspended-audit case. Ctrl-Z an audit for 6 minutes, --resume elsewhere, then fg: two writers again. Could you refuse the claim when the owner is on this host, its pid is alive and the heartbeat is under CRAWL_LEASE_PID_TRUST_MS (compare-and-set on the owner you read, like takeOverDeadOwner), with a test? If you would rather keep this PR to the issue's scope, say so and we will file a follow-up.
  4. Rebase on main (it is behind fix(crawler): identify 403 challenge pages #914, which also adds a CHANGELOG ### Fixed line).

Optional: a controller-level test that crawl --resume with a live foreign owner starts a new crawl and never calls resumeFromStorage would lock the wiring in.

@WilliamK112
WilliamK112 force-pushed the codex/prevent-concurrent-resume-1026 branch from a913d33 to 62b18e2 Compare October 26, 2026 00:37
@WilliamK112

Copy link
Copy Markdown
Contributor Author

Addressed at current head 62b18e2: the child-process race test now has a 20-second test timeout, 15-second child deadlines, inherited stderr, in-try spawning, and guaranteed termination cleanup. Audit/crawl docs define the five-minute stale window, the same-host live-PID exception, Ctrl-C release, waiting, and --refresh, with a link to a dedicated crawl-leases section in self. Claiming now refuses a same-host owner whose PID is alive for the 24-hour trust window and uses a compare-and-set over the owner fields read before taking stale/dead leases; a six-minute-silent live-owner regression covers it. The branch is rebased onto current main. All 19 focused tests, crawler/CLI typechecks, repository-wide format check, and full docs validation (61 docs tests) pass.

@WilliamK112
WilliamK112 force-pushed the codex/prevent-concurrent-resume-1026 branch from 62b18e2 to b406ab0 Compare October 26, 2026 06:38
@nc9 nc9 mentioned this pull request Oct 26, 2026
@nc9

nc9 commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Merged: landed in the v0.0.134 batch (#1149), main commits 1966e24. Thank you @WilliamK112, claiming before the resume decision with a compare-and-set, then closing the suspended-owner gap and hardening the child-process test, made this safe to ship. (Nik)

@nc9 nc9 closed this Oct 26, 2026
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.

A second --resume of a crawl held by a live owner keeps crawling it

2 participants