Skip to content

fix: give Crow a floor of 8 threads so a stalled instance still answers (#120) - #121

Merged
jrosskopf merged 1 commit into
mainfrom
fix/gh-120-io-thread-floor
Sep 19, 2026
Merged

jrosskopf merged 1 commit into
mainfrom
fix/gh-120-io-thread-floor

Conversation

@jrosskopf

@jrosskopf jrosskopf commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Why

Crow runs a request handler on the server thread that owns its connection, and dedicates one thread to accepting — so concurrency threads give concurrency - 1 usable ones. flAPI used hardware_concurrency(), which on a 1- or 2-vCPU container — Cloud Run's default — leaves a single thread. One slow synchronous query then blocks every other connection on the instance, /health included.

That means the stall detection shipped in v26.09.20 does nothing on a small instance: the probe never gets to answer.

Measured

One ~5s query in flight, 40 concurrent GET /health probes:

server threads blocked >0.5s answered 503 stalled
1 40/40 0/40
7 5/40 35/40

Consistent with roughly 1-in-threads. The first row is the point: readiness does not merely miss the stall occasionally, it never reports it — every probe waits out the query and then answers 200 ready.

What

max(hardware_concurrency, 8), capped at 64. Not configurable. These threads wait on sockets, so over-providing them costs thread stacks, not CPU.

This makes the failure unlikely, not impossible — 5 of 40 probes still blocked at 7 threads. The honest fix is running handlers off the server threads, and #120 stays open for it.

Two measurement traps, both hit here

  • Sequential probes cannot measure this. The first one to block waits out the query and every later one runs after it finished, so at most one can ever block regardless of thread count. An earlier sequential run reported a flat 1/40 for every configuration and carried no information.
  • taskset does not change std::thread::hardware_concurrency() on glibc — it kept reporting 32, so pinning CPUs does not simulate a small instance. The numbers above come from binaries with the count forced.

Both are recorded in the test file so the next person does not repeat them.

Testing

serverThreadCount() unit-tested at the floor, above it, at 0 (which hardware_concurrency() may legally return), and at the uint16_t cap. 765 unit tests, 334 standalone integration tests.

Part of #120, which stays open for the real fix.

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.

…rs (#120)

Crow runs a request handler on the server thread that owns its connection,
and dedicates one thread to accepting - so `concurrency` threads give
`concurrency - 1` usable ones. flAPI used `hardware_concurrency()`, which
on a 1- or 2-vCPU container (Cloud Run's default) leaves a SINGLE thread.
One slow synchronous query then blocks every other connection on the
instance, `/health` included.

Measured, one ~5s query in flight, 40 CONCURRENT health probes:

  1 server thread:   40/40 blocked,  0/40 reported the stall
  7 server threads:   5/40 blocked, 35/40 reported the stall

The first row is the one that matters. On a small instance readiness does
not merely miss the stall sometimes - it NEVER reports it. Every probe
waits out the query and then answers 200 ready, so the stall detection
shipped in v26.09.20 does nothing there.

The floor makes that unlikely rather than impossible; the honest fix is
running handlers off the server threads, which #120 stays open for. These
threads wait on sockets, so over-providing them costs stacks, not CPU.

Two measurement notes, both of which cost time here:

- This must be measured with CONCURRENT probes. Sequential ones cannot see
  it: the first probe to block waits out the query and every later one runs
  after it finished, so at most one can ever block, whatever the thread
  count. An earlier sequential run reported a flat "1/40" for every
  configuration and meant nothing.
- `taskset` does not change `std::thread::hardware_concurrency()` on glibc
  - it kept reporting 32 - so pinning CPUs does not simulate a small
  instance. The numbers above come from binaries with the count forced.

- Add APIServer::serverThreadCount(), unit-tested at the floor, above it,
  at 0 (which hardware_concurrency() may legally return) and at the
  uint16_t cap
- Document the behaviour in CONFIG_REFERENCE next to stall-timeout-s, with
  the measurements and the pointer to #120
@jrosskopf
jrosskopf merged commit 7b1ee85 into main Sep 19, 2026
22 checks passed
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