Skip to content

fix(crawler): recycle the PKC client after every crawl pass - #3

Merged
tomcasaburi merged 1 commit into
masterfrom
fix/crawler-recycle-pkc-client
Sep 4, 2026
Merged

fix(crawler): recycle the PKC client after every crawl pass#3
tomcasaburi merged 1 commit into
masterfrom
fix/crawler-recycle-pkc-client

Conversation

@tomcasaburi

@tomcasaburi tomcasaburi commented Sep 3, 2026

Copy link
Copy Markdown
Member

5archive-server-1 restarted 143 times in the 23 hours since it moved to v0.4.0: every ~8 minutes Node aborted with Reached heap limit … JavaScript heap out of memory at the 1 GiB cap (exit 134), and seeditarchive hit the same wall at its 512 MiB cap. No requests, no new comments, no errors: the crawl loop alone leaked about 2.4 MB/s.

pkc-js keeps what a crawl loads (community instances, their pages, the verification caches behind them) for the life of the PKC instance, and none of it is reachable from anything the crawler holds. Reproduced inside the container with the crawler's exact page walk over a 721-comment board: the heap grew ~2 MB per crawl with no growth in any enumerable pkc-js map, and destroying and recreating the PKC instance between crawls kept it flat. 68 boards refreshed every few seconds is ~20 MB per pass, hence the cap.

So a pass that crawled anything now retires the client on the way out and the next pass reconnects (a local WebSocket, a few hundred ms). The recycle is bounded to 10 s so a daemon that will not close the socket cannot stall the loop, and it is quiet: the [pkc] connected line is kept for connects that follow a timeout or a daemon restart instead of printing once per pass. An idle pass leaves the cache alone. Cut as 0.4.1.

Verified: typecheck clean, 119/119 server tests (new regression test covers the recycle and the idle-pass no-op).


Note

Medium Risk
Changes end-of-pass PKC client lifecycle and reconnect behavior on the crawler’s critical path; mitigated by timeout bounds, generation guards, and a regression test.

Overview
Fixes a heap leak where pkc-js retained crawl-loaded data (~2 MB per board pass) until process OOM. After any crawl pass that actually indexed communities, the crawler now destroys and recycles the PKC client via resetPkcClient({ quiet: true }), bounded by a 10s timeout so a stuck destroy() cannot block the loop.

Idle passes (nothing due) skip recycle to avoid pointless WebSocket reconnect churn. Routine recycles are quiet: they suppress the [pkc] connected log so only timeouts or daemon restarts stay visible.

tick() is exported for tests; a new regression test asserts destroy runs once after a crawling pass and not again on an idle second pass. Server version 0.4.1.

Reviewed by Cursor Bugbot for commit 35c494a. Bugbot is set up for automated code reviews on this repo. Configure here.

Summary by CodeRabbit

  • Bug Fixes

    • Improved crawler reliability by recycling the PKC connection after active crawling passes.
    • Avoided unnecessary connection resets during idle passes.
    • Improved connection logging to reduce noise during routine recycling while preserving failure visibility.
  • Tests

    • Added coverage verifying connection cleanup, comment persistence, and idle-pass behavior.
  • Chores

    • Updated the server package version to 0.4.1.

5archive-server-1 restarted 143 times in the 23 hours since it moved to
v0.4.0: every ~8 minutes Node aborted with "Reached heap limit ... JavaScript
heap out of memory" at the 1 GiB cap (exit 134), and seeditarchive hit the
same wall at its 512 MiB cap. No requests, no new comments, no errors: the
crawl loop alone leaked about 2.4 MB/s.

pkc-js keeps what a crawl loads (community instances, their pages, the
verification caches behind them) for the life of the PKC instance, and none
of it is reachable from anything the crawler holds. Reproduced inside the
container with the crawler's exact page walk over a 721-comment board: the
heap grew ~2 MB per crawl with no growth in any enumerable pkc-js map, and
destroying and recreating the PKC instance between crawls kept it flat.
68 boards refreshed every few seconds is ~20 MB per pass, hence the cap.

So a pass that crawled anything now retires the client on the way out and
the next pass reconnects (a local WebSocket, a few hundred ms). The recycle
is bounded to 10 s so a daemon that will not close the socket cannot stall
the loop, and it is quiet: the "[pkc] connected" line is kept for connects
that follow a timeout or a daemon restart instead of printing once per pass.
An idle pass leaves the cache alone. Cut as 0.4.1.
@coderabbitai

coderabbitai Bot commented Sep 3, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Team

Run ID: 3f15948e-c2d4-4ae2-ae58-ee5e41ab94af

📥 Commits

Reviewing files that changed from the base of the PR and between b63e17f and 35c494a.

📒 Files selected for processing (4)
  • server/package.json
  • server/src/crawler/crawler.test.ts
  • server/src/crawler/crawler.ts
  • server/src/pkc/client.ts

Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

The crawler now recycles the PKC client after passes that process communities. Quiet reconnect handling suppresses routine connection logs. Tests verify client destruction after active work and no destruction during idle passes. The package version increases to 0.4.1.

Changes

PKC client recycling

Layer / File(s) Summary
Quiet reconnects
server/src/pkc/client.ts
resetPkcClient accepts a quiet option. connect consumes the suppression flag and omits the successful connection log for quiet reconnects.
Crawler lifecycle integration
server/src/crawler/crawler.ts, server/src/crawler/crawler.test.ts, server/package.json
tick counts processed rows and recycles the PKC client after active passes with a 10-second timeout. Tests verify active-pass destruction and idle-pass reuse. The package version changes to 0.4.1.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Merge Risk: ⚪ Minimal · up to 35c49

Active crawler passes now recycle the PKC client to limit retained memory, while idle passes retain the existing client and shutdown is bounded. No merge-blocking production or user-impact risk is currently identified.

Sequence Diagram(s)

sequenceDiagram
  participant CrawlerTick
  participant Database
  participant PKCClient
  participant PKCConnection
  CrawlerTick->>Database: process queued community
  Database-->>CrawlerTick: crawled row
  CrawlerTick->>PKCClient: resetPkcClient({ quiet: true })
  PKCClient->>PKCConnection: reconnect with quiet flag
  PKCConnection-->>PKCClient: connection result
  PKCClient-->>CrawlerTick: complete recycling
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 3 files. (1 skipped: 1… Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: recycling the PKC client after crawler passes.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 3 files. (1 skipped: 1 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/crawler-recycle-pkc-client

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@tomcasaburi
tomcasaburi merged commit e862e99 into master Sep 4, 2026
2 checks passed
@tomcasaburi
tomcasaburi deleted the fix/crawler-recycle-pkc-client branch September 4, 2026 13:11
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