Skip to content

Fail startup when a providers backend cannot be reached - #274

Open
byo wants to merge 8 commits into
mainfrom
providers-startup
Open

byo wants to merge 8 commits into
mainfrom
providers-startup

Conversation

@byo

@byo byo commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Context

Refs #271. When the providers backend is unreachable while indexstar starts, the provider cache preload fails quietly. GET /providers then returns an empty list (null) until indexstar is restarted, because listing an empty cache never starts a refresh. On 2026-11-03 this lasted over 40 minutes after an indexer host reboot.

Proposed Changes

  • Before creating the provider cache, indexstar fetches the provider list from every --providersBackends backend. The backends are fetched concurrently.
  • A backend that fails (connection error, non-200 status or invalid JSON) is retried every SERVER_PCACHE_STARTUP_RETRY_INTERVAL (default 5s, at least 1s), up to SERVER_PCACHE_STARTUP_ATTEMPTS attempts in total (default 5, at least 1). Only failed backends are retried. A backend that answers with an empty list passes, so a new, empty indexer can start.
  • The provider cache starts with the lists fetched by the check, so each backend is fetched once at startup, and a backend that fails right after the check cannot leave the cache empty.
  • If any backend still fails, startup fails with an error naming each failing backend, and the process exits with status 1. Kubernetes restarts the container, and the crash loop shows up in rollouts and alerts, so an operator can fix or remove the backend.
  • The finder HTTP listener is bound after the check passes, so while the check retries, connections to it are refused instead of hanging. The metrics listener is bound at startup as before.
  • The check runs only at startup. Later refresh failures do not stop a running server.

Behavior change for operators: while a providers backend is unreachable, a starting indexstar serves nothing, finds included, instead of serving with an empty provider list. In the worst case a pod takes about 2.5 minutes to exit (5 attempts of up to the 30s HTTP client timeout each, plus 4 waits of 5s), however many backends fail.

Not in this change:

  • /providers returning null for an empty list.
  • The cache emptying at runtime after a backend outage longer than the cache TTL.
  • Serving metrics while the check runs.

Tests

  • Unit tests (synctest) for the startup check:
    • A flaky backend is retried until it answers, and a healthy one is fetched once.
    • A backend that keeps failing produces an error after exactly the configured attempts, naming only that backend.
    • Backends are fetched concurrently.
    • At least one attempt is made.
    • The retry interval has a 1s minimum.
    • Cancellation stops the wait.
  • HTTP responses with a real pcache HTTP source: 200 with [], null or providers passes. 404, 500 and invalid JSON fail and name the backend.
  • The provider cache starts with the lists from the check: a backend that answers the check and fails every later request still leaves its providers in the cache, with one request made.
  • End-to-end tests with runApp:
    • A providers backend answering 503 makes startup fail after the configured attempts. Without the check, this test hangs until it times out.
    • With the finder listen port held by the test, startup fails with the providers error rather than a bind error, so the check runs before the finder listener is bound.
  • go vet, go test -race ./... and golangci-lint pass, with no new lint findings.

Revert Strategy

Revert the merge commit. No data or config migration is involved; the two new environment variables become unused.

🤖 Assisted by Claude Code

The provider cache is preloaded once when indexstar starts. When a
providers backend is unreachable at that moment, the preload fails
quietly and /providers keeps returning an empty list until indexstar is
restarted, because listing an empty cache never starts a refresh.

Before creating the provider cache, fetch the provider list from every
providers backend. A backend that fails is retried every
SERVER_PCACHE_STARTUP_RETRY_INTERVAL (default 5s), up to
SERVER_PCACHE_STARTUP_ATTEMPTS attempts in total (default 5). If any
backend still fails, startup fails with an error naming it, so the
container exits and is restarted instead of serving incomplete data.

The check runs only at startup. Later refresh failures do not stop the
server.
@byo
byo requested review from TakGN, gammazero and nymd November 18, 2026 14:30
@byo byo self-assigned this Nov 18, 2026
Comment thread providers.go Resolved
@claude

claude Bot commented Nov 18, 2026

Copy link
Copy Markdown

Claude review

The change does what the description says. Each providers backend is fetched before pcache.New, failed ones are retried, and startup returns an error naming them. c.Context is the signal-aware context from main, so SIGTERM during the wait is honoured, and Reload does not recreate the cache, so the check runs only at startup. The tests cover retry, exhaustion, the minimum of one attempt, cancellation and the end-to-end failure. I found no correctness, concurrency or resource problems. I could not read the go-libipni pcache source in this environment, so I did not check how pcache.New preloads, or whether the extra FetchAll per backend at startup is redundant with it. I also did not run the tests.

Findings

  1. Nit - providers.go:27: backends are fetched sequentially, so the worst-case startup time grows with the number of failing backends. The ~2.5 min bound in the description holds for one failing backend. Fetching the pending sources concurrently would keep it bounded, or the description could say the figure is per failing backend.

claude-sonnet-5-5 at high effort · requested by byo · re-run with @claude review [sonnet|opus|fable] [low|medium|high|xhigh|max]

@TakGN TakGN left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review findings (7 inline comments). Main concerns: failing the whole process on one bad providers backend, the double fetch that leaves a TOCTOU gap, and sequential probing.

Comment thread server.go Resolved
Comment thread providers.go Resolved
Comment thread providers.go Resolved
Comment thread providers.go Resolved
Comment thread providers.go Resolved
Comment thread providers_test.go Resolved
Comment thread providers_test.go Resolved
@byo
byo requested a review from TakGN November 19, 2026 09:05
@byo

byo commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

@claude review

@claude

claude Bot commented Nov 19, 2026

Copy link
Copy Markdown

Claude review

The startup check is correct as written. I read checkProviderBackends and its call in NewServer, and I found nothing that breaks. Attempt counting, retrying only the failed backends, the max(...) clamps and the ctx.Done() select all behave as the tests describe. The goroutines are always joined by wg.Wait(), and fetchErrs[i] is written by a single goroutine each. The listeners that NewServer binds earlier are closed by their deferred functions when it returns an error. Reload never touches the provider cache, so config reloads are unaffected. I could not run the tests in this review sandbox, so that part is by reading only.

No blockers or should-fix items. Two things the author may want to know, neither worth blocking on:

  • Nit, server.go:151: the check only narrows the problem from /providers returns null after startup until restart when the providers backend was down at start #271. pcache.New does its own preload straight afterwards. If a backend fails between the check and that preload, the preload still fails quietly and /providers can stay empty. That window is small, so it seems acceptable, but the PR text says the check guarantees this can't happen, which is slightly too strong.
  • Nit, server.go:151: the --listen and --metrics sockets are already bound while the check waits, which can be about 2.5 minutes. The kernel accepts connections into the backlog, but nothing answers them. A liveness probe can fail and kill the pod before indexstar exits on its own. This ends in a restart either way, but the logs will show probe failures rather than the startup error. Running the check before the Listen calls would avoid that.

claude-sonnet-5-5 at high effort · requested by byo · re-run with @claude review [sonnet|opus|fable] [low|medium|high|xhigh|max]

@byo

byo commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

Claude review

The startup check is correct as written. I read checkProviderBackends and its call in NewServer, and I found nothing that breaks. Attempt counting, retrying only the failed backends, the max(...) clamps and the ctx.Done() select all behave as the tests describe. The goroutines are always joined by wg.Wait(), and fetchErrs[i] is written by a single goroutine each. The listeners that NewServer binds earlier are closed by their deferred functions when it returns an error. Reload never touches the provider cache, so config reloads are unaffected. I could not run the tests in this review sandbox, so that part is by reading only.

No blockers or should-fix items. Two things the author may want to know, neither worth blocking on:

  • Nit, server.go:151: the check only narrows the problem from /providers returns null after startup until restart when the providers backend was down at start #271. pcache.New does its own preload straight afterwards. If a backend fails between the check and that preload, the preload still fails quietly and /providers can stay empty. That window is small, so it seems acceptable, but the PR text says the check guarantees this can't happen, which is slightly too strong.
  • Nit, server.go:151: the --listen and --metrics sockets are already bound while the check waits, which can be about 2.5 minutes. The kernel accepts connections into the backlog, but nothing answers them. A liveness probe can fail and kill the pod before indexstar exits on its own. This ends in a restart either way, but the logs will show probe failures rather than the startup error. Running the check before the Listen calls would avoid that.

claude-sonnet-5-5 at high effort · requested by byo · re-run with @claude review [sonnet|opus|fable] [low|medium|high|xhigh|max]

Thanks, both noted.

Window before the preload: fixed. The check now hands the lists it fetched to the provider cache. Each source is wrapped so that its first FetchAll, which is the pcache.New preload, returns the list from the check, and later fetches go to the backend. The cache can no longer start empty after the check passed, and each backend is fetched once at startup. Fixed in a248131

Listeners bound during the check: the indexstar deployment has only a readiness probe (/health) and no liveness or startup probe. While the check runs, the pod is just not ready, which is what we want. If a liveness probe were added, it would fail before Listen too (connection refused instead of a timeout), so moving the check would change only the probe's error message. Binding first also makes a port conflict fail right away. Leaving the order as is.

@claude

claude Bot commented Nov 19, 2026

Copy link
Copy Markdown

Claude review

The startup check looks correct. Failed backends are retried and succeeded ones are fetched once. Each fetched list is handed to the first FetchAll of the provider cache through prefetchedSource, so the window before the preload is closed. Context cancellation is honoured during the wait. Reload does not go through NewServer, so SIGHUP reloads never run the check and cannot block the main loop. The tests cover retry counts, concurrency, clamping of attempts and retry interval, cancellation, HTTP response handling, cache pre-fill, and an end-to-end startup failure. I found no confirmed defects.

Limits of this review: the sandbox did not let me run go test or read the go-libipni pcache source, so I did not run the new tests or check pcache.New internals. I relied on TestCheckProviderBackendsPrefillsCache, which asserts that the preload consumes the pre-fetched list with exactly one backend request.

No issues found.

claude-sonnet-5-5 at high effort · requested by byo · re-run with @claude review [sonnet|opus|fable] [low|medium|high|xhigh|max]

@byo

byo commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

@claude review

Comment thread server.go Resolved
@claude

claude Bot commented Nov 19, 2026

Copy link
Copy Markdown

Claude review

The change is small and well tested. The retry loop, the clamping of attempts and interval, the per-backend failure reporting and the one-shot prefetchedSource handoff all look correct. Empty and null lists are stored through a non-nil pointer, so they are handled correctly. I found one problem worth fixing before merge: the check runs after the listeners are bound. I could not read the pcache source in this environment, because go-libipni is not in the module cache. I did not confirm that the pcache.New preload calls FetchAll once per source. The new prefill test is the only evidence for that.

Findings

  1. Should fix, server.go:151: checkProviderBackends runs after --listen and --metrics are already bound (server.go:80-93). During the retry window, which can last about 2.5 minutes, clients, probes and metrics scrapes connect successfully to the kernel backlog and then hang instead of getting connection refused. A probe with a shorter timeout can kill the pod before the startup error is logged. Run the check before binding the listeners, or bind them only after it succeeds.

claude-sonnet-5-5 at high effort · requested by byo · re-run with @claude review [sonnet|opus|fable] [low|medium|high|xhigh|max]

@byo

byo commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

@claude review

@claude

claude Bot commented Nov 19, 2026

Copy link
Copy Markdown

Claude review

The change does what the description says. I found no correctness, concurrency, resource or security problems. checkProviderBackends fetches the backends concurrently, with each goroutine writing only its own fetchErrs[i]. It retries only the backends that failed, and it clamps attempts and interval. It also stops waiting when the context is cancelled. prefetchedSource uses atomic.Pointer.Swap, so the handoff to pcache is safe even if FetchAll is called concurrently. A null or empty list is handled correctly, because the pointer to the slice is non-nil. Moving the finder Listen after the check is safe: the deferred bound.Close() and mb.Close() cleanup still covers the early-return paths. The tests cover retry, exhaustion, concurrency, cancellation, HTTP status and body cases, cache prefill and listener ordering.

I could not read the go-libipni module source in this sandbox, so I did not check directly that pcache.New calls FetchAll once at preload. TestCheckProviderBackendsPrefillsCache exercises exactly that, so I am relying on it.

No issues found.

claude-sonnet-5-5 at high effort · requested by byo · re-run with @claude review [sonnet|opus|fable] [low|medium|high|xhigh|max]

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