Skip to content

Keep the site and jobs working when every chronik server is down - #1180

Open
Klakurka wants to merge 1 commit into
masterfrom
ccr-b62297cf-awgd35-chronik-down
Open

Klakurka wants to merge 1 commit into
masterfrom
ccr-b62297cf-awgd35-chronik-down

Conversation

@Klakurka

@Klakurka Klakurka commented Dec 3, 2026

Copy link
Copy Markdown
Member

Related to #851

Description

Fallback chronik URLs (#1010) cover one server going down. This PR covers every chronik server of a network being unreachable, refusing or hanging. In that case the web app and the jobs keep running, answer promptly, and pick back up when chronik returns.

What could go wrong before, and what changed:

  • Hangs. chronik-client's axios calls have no timeout, so a server that accepts connections but never answers hung callers indefinitely (requests piled up behind the reverse proxy, which then returned 502).
    • Each attempt in chronikCallWithRetry is now bounded at CHRONIK_HTTP_ATTEMPT_TIMEOUT_MS (30s).
    • Chronik calls made while answering a web request (getBalance, getTransactionDetails, getLastBlockTimestamp for the networks list) share a CHRONIK_REQUEST_TIMEOUT_MS (10s) budget.
  • Error responses. Those web-request calls now fail with a ChronikUnavailableError.
    • /api/address/balance/[address] and /api/transaction/[transactionId] answer 503 CHRONIK_UNAVAILABLE_503 instead of a 500 carrying the raw error. The transaction route also answers 400 for an unknown network slug, instead of crashing on an undefined client.
    • /api/networks reports the network as connected: false, as before, but now promptly.
    • After a failure, requests for that network fail fast for CHRONIK_UNAVAILABLE_BACKOFF_MS (30s) instead of each waiting out the timeout. Any success clears this. A real chronik error, such as an unknown txid, does not trip it.
  • Process crashes. onMessage did void this.processWsMessage(msg), so a handler failing (e.g. syncBlockTransactions on BLK_FINALIZED with chronik gone) was an unhandled rejection. Next.js only logs those, but the jobs process (plain Node) exits on them. They're now caught and logged.
  • Fire-and-forget syncs. syncAndSubscribeAddresses, which API routes call with void, never rejects now. MultiBlockchainClient.syncAddresses / subscribeAddresses contain one network's failure, so the other network's results aren't lost.
  • Startup.
    • If ChronikClient.useStrategy ever throws, the client is left unset (its calls fail as unavailable) instead of leaving waitForStart() and the jobs waiting forever.
    • getUrls() (admin page SSR) and getSubscribedAddresses() no longer throw TypeError before the client is ready.
  • Jobs. The startup blockchain sync checks chronik first and skips an unreachable network. Before, every address failed after its own retries, and the current-prices job, which only starts after this sync completes, was held up.
  • Recovery. The WS reconnect loop already retries forever, and backfills subscribed addresses on a reconnect. It now also backfills when the first connection only succeeds after failing (chronik down at startup), so txs from the outage, along with their triggers and client payment statuses, are picked up once chronik is back. This also covers the startup sync skipped above.

Test plan

  • make github-test-unit: 20 suites / 425 tests pass.
  • New tests/unittests/chronikDown.test.ts mocks chronik-client so that every HTTP call fails as "Error connecting to known Chronik instances" (or never answers) and the WS never opens. It checks:
    • startup completes
    • balance and tx details fail with ChronikUnavailableError, then fail fast without calling chronik, then retry after the backoff, and a success clears the state
    • real chronik errors are passed through
    • an unknown network slug gets a 400
    • the networks list shows both networks as disconnected
    • the balance and tx routes answer 503
    • syncAndSubscribeAddresses resolves
    • the startup sync skips both networks
    • a failing WS message handler doesn't raise an unhandled rejection
    • hanging servers time out within the budgets
    • before init, getUrls / getSubscribedAddresses / calls behave
    • a failed client setup doesn't block startup
    • backfill runs after a first connection that needed retries, and not after one that worked straight away
    • backfill failures are contained
  • Two existing syncMissedTransactions tests stub the new reachability check.
  • ESLint is clean on the changed files. tsc --noEmit reports no new errors in the changed files.
  • Manual check suggested: point networkBlockchainURLs.ecash at an unreachable host (and one that accepts TCP but never answers, e.g. nc -l), start the app and jobs, and confirm:
    • pages load
    • /api/networks returns promptly with connected: false
    • balance/tx endpoints return 503
    • the jobs process stays up and moves on to the prices jobs
    • restoring the URL reconnects the WS and backfills

Remarks

  • The timeouts don't cancel the underlying axios request (chronik-client gives no way to). The caller just stops waiting, and the request finishes or fails in the background.
  • The new backfill-after-first-connect can overlap with the jobs process's startup sync if chronik flapped at startup. Both paths already tolerate each other (upserts / skipDuplicates), as they do for a reconnect during startup today.
  • utils/timeout.ts is the same file as in the health-checks PR, so the two merge cleanly in either order.
  • Not changed: a missing WS_AUTH_KEY still fails client setup (a config error, not an outage).

Generated by Claude Code

- Bound every chronik HTTP attempt with a 30s timeout: chronik-client sets
  none, so an unresponsive (rather than refusing) server hung callers forever.
- Chronik calls made for web requests (balance, tx details, network status)
  share a 10s budget and fail with ChronikUnavailableError; the balance and
  tx routes answer 503. After one such failure, requests for that network
  fail fast for 30s instead of each waiting out the timeout.
- Catch WS message handler failures, which were unhandled rejections (fatal
  in the jobs process) when chronik went down mid-block.
- On-demand syncs fired from API routes never reject, and one network failing
  no longer loses the other's results.
- The startup sync job skips a network whose chronik is unreachable instead
  of failing every address after retries, so the jobs waiting on it start.
- When the WS only connects after failing at startup, backfill the
  subscribed addresses as on a reconnect, so outage txs aren't missed.
- A failing chronik client setup no longer leaves startup waiting forever,
  and getUrls / getSubscribedAddresses work before the client is ready.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019G564i2sNiHJYQR95m8zGq
@coderabbitai

coderabbitai Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 50 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 0f047412-12fa-4066-851f-dec2b9d8fca6
Commits

Reviewing files that changed from the base of the PR and between f9d06f2 and 252d7f2.

Files selected for processing: 7

  • constants/index.ts
  • pages/api/address/balance/[address].ts
  • pages/api/transaction/[transactionId].ts
  • services/chronikService.ts
  • tests/unittests/chronikDown.test.ts
  • tests/unittests/chronikService.test.ts
  • utils/timeout.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

This branch has not been deployed

No deployments
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.

2 participants