Skip to content

fix(relay): report event-loop freezes and keep seat polls off the cell-list read - #27405

Merged
Jinwoo-H merged 2 commits into
mainfrom
relay-director-stall-signals
Oct 11, 2026
Merged

Jinwoo-H merged 2 commits into
mainfrom
relay-director-stall-signals

Conversation

@Jinwoo-H

@Jinwoo-H Jinwoo-H commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​198 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​198
Prod 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​9 $\color{#cf222e}{\Huge{\mathbf{−}}}$​3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​6

ELI5

When the relay's database was starting up on staging (2026-10-11), both directors stopped doing anything at all for about two and a half minutes. Our dashboards showed nothing unusual. This PR makes that kind of freeze visible, removes a small wait in the seat poller that could make it worse, and adds tests showing the database pool fails fast when the database accepts connections but never answers.

The fix for the freeze itself is a separate Terraform PR, which moves the director onto a Cloud SQL Auth Proxy sidecar. This PR doesn't change how the director connects.

What Changed

  • Event-loop max in runtime metrics. Each runtime metrics line now also reports eventLoopDelayMsMax, the longest stall in the interval. Before, the line reported only the p99, which read 67.9 ms across a 134 s freeze: one long freeze is a single sample, so it never reaches the 99th percentile. The max captures it.
  • Seat poller no longer waits on its cell-list read. Once the poller has a cell list, each round polls cells from that list straight away. Before, the round that started a cell-list refresh waited for the read first and skipped its polls: one round, about 1 s, per refresh (every 30 s, and on retry while the read fails). Later rounds already polled, because a refresh in flight returns at once, so this is a small gain. It mattered on 2026-10-11 only because of the freeze. These polls feed a reserve cell's dead-man, so no poll should wait on the database. Only the very first read, when there is no list yet, is still awaited.
  • New test database-connect-stall-postgres.test.ts.
    • The first case always runs. A local TCP server stands in for a Cloud SQL instance that is starting: it accepts connections and never speaks. Six callers on a three-connection pool all get a retryable error in under 4.5 s (about 2 s observed). The event loop never pauses for more than 250 ms.
    • The second case runs only when ORCA_RELAY_TEST_POSTGRES_URL is set. It puts a proxy in front of a real PostgreSQL and moves it through stop (refuse), start-up (accept but never answer) and back up. Each phase fails fast and retryably, and the pool serves again afterwards.

Why

Root cause. On Cloud Run's built-in Cloud SQL socket, connect() itself blocks until the connector's dial to the instance finishes. That dial timed out after 10 s on each try while the instance started. The awaits ran back to back without returning to the event loop, so no timer could fire for 134 s:

  • the pool's 2 s connect timeout
  • the 4 s assignment queue wait
  • the runtime metrics flush
  • the seat polls

Evidence is in the staging logs from 06:12:14 to 06:14:28 on 2026-10-11:

  • the only lines logged are at the exact millisecond of each connector failure
  • a request that never touches the database took 133.6 s
  • the reserve cell's dead-man tripped at 06:13:49

Production showed the same signature once in the last 14 days, on 2026-09-28 at about 14:09–14:11 UTC. Four directors each lost 40–50 s.

Why these changes. They don't fix the blocking connect; the sidecar PR does that. They make the next freeze show up in metrics, and they lock in, with tests, the fail-fast behaviour the sidecar relies on over TCP. No in-process test can reproduce a blocking connect(); the staging stop/start drill after the sidecar merge covers that.

Alternatives considered.

  • A database circuit breaker. Skipped for now: while connects block the loop, every probe still freezes the director for 10 s.
  • Raising the p99 alert threshold instead of adding max. A single long freeze never moves p99, whatever the threshold.

Linked Issue

N/A (internal relay incident follow-up, staging 2026-10-11).

Visual Proof

N/A: no UI. Only the metrics line and tests change.

Testing

  • vitest run for the whole relay app: 1108 passed, 188 skipped (Postgres-gated tests).
  • database-connect-stall-postgres.test.ts against a local PostgreSQL 16 (ORCA_RELAY_TEST_POSTGRES_URL set): both cases pass in about 2 s each.
  • The new seat-poller test fails on the old code (expected [ 'cell-a.example.test' ] to equal two polls).
  • The new observability test holds the loop for 300 ms and checks the max reports at least 250 ms.
  • tsc --noEmit passes for the relay app.

AI Disclosure

N/A (internal contributor).

Agent skill upstream boundary

  • Not applicable.

Notes

  • The new field is additive on the runtime metrics line. Existing log-based metrics and readers ignore unknown fields. The metric and alert that read it are gated in the sidecar Terraform PR, so production gets them only after staging proves them.
  • No cell image change is needed for the freeze fix. The eventLoopDelayMsMax field reaches cells on their next normal roll.

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • N/A: no UI
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote impact: N/A (relay server only)
  • Relay tsc and vitest pass locally

…oop freezes

The seat poller no longer waits on its database cell-list read before polling
cells once it has a list, so a stalled read cannot starve the polls that feed a
reserve cell's dead-man. Runtime metrics add eventLoopDelayMsMax: a single long
freeze is one sample, so p99 read 68 ms across a 134 s staging freeze.

Adds a pool test against a database that accepts and never answers, and a
real-Postgres stop/start recovery test.
@coderabbitai

coderabbitai Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: fccd1512-cce2-476c-adcf-ba68f5c4b6bc


📥 Commits

Reviewing files that changed from the base of the PR and between 6f39f62 and 917d442.



📒 Files selected for processing (2)
  • cloud/apps/relay/src/database-connect-stall-postgres.test.ts
  • cloud/apps/relay/src/shadow-seat-directory.ts


🚧 Files skipped from review as they are similar to previous changes (2)
  • cloud/apps/relay/src/shadow-seat-directory.ts
  • cloud/apps/relay/src/database-connect-stall-postgres.test.ts


Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 3 remain after this review.




📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes add PostgreSQL pool tests for refused and blackholed connections, expose the maximum event-loop delay in relay metrics, and update shadow-seat polling to use the last loaded cell list while later refreshes are pending.



Priority: ➖ Normal

Merge Risk: 🔵 Low · up to 917d4

The runtime changes add an event-loop delay metric and let seat polling continue during a cell-list refresh. The remaining concerns are in the new connection-stall tests: one assertion can fail spuriously, and one timer can linger after a failed test. Neither affects production behavior, but both are worth tightening before merge.

Pre-merge checks | Passed 3 | Failed 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 5 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check Warning The description explains the problem, changes, rationale, alternatives, testing, and compatibility considerations. It does not provide an actual linked issue, although the template requires one, and i… Link the relevant existing issue, or document the applicable maintainer exception with the issue reference. Also report the results of pnpm lint and pnpm build, or state that CI will cover them.
✅ Passed checks (3 passed)
Check name Status Explanation
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.
Title check Passed The title clearly and concisely describes both primary changes: reporting event-loop freezes and allowing seat polls to proceed without waiting for the cell-list read.

Full details: Description check

Explanation

The description explains the problem, changes, rationale, alternatives, testing, and compatibility considerations. It does not provide an actual linked issue, although the template requires one, and it does not report lint or build results.


  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR



  • 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

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

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 2


ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a253152b-5521-453c-9752-2b5f75a07f17
📥 Commits

Reviewing files that changed from the base of the PR and between b197bc6 and 6f39f62.

📒 Files selected for processing (5)
  • cloud/apps/relay/src/database-connect-stall-postgres.test.ts
  • cloud/apps/relay/src/relay-observability.test.ts
  • cloud/apps/relay/src/relay-observability.ts
  • cloud/apps/relay/src/shadow-seat-directory.test.ts
  • cloud/apps/relay/src/shadow-seat-directory.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 4 remain after this review.

// A queued caller waits out one connect timeout for a slot, then one for its own dial.
expect(elapsedMs).toBeLessThan(4_500)
}
expect(stalling.sockets.size).toBeGreaterThan(0)

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.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Count accepted connections, not connections still open.

When the stalled connections close before Promise.all settles, the server’s close handler removes them from stalling.sockets. The pool can then fail every query as intended while this assertion fails. Record an accepted-connection count when the server receives a connection, and assert on that count instead. pg-pool destroys a connecting client when its connection timeout expires. (raw.githubusercontent.com)

function watchEventLoop(): { stop: () => number } {
let last = performance.now()
let longestGapMs = 0
const timer = setInterval(() => {

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.

🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

Clear the event-loop interval during test cleanup.

If an assertion in the PostgreSQL-gated test fails before loop.stop(), afterEach closes the pool and server but leaves this interval active. An active interval keeps the Node.js event loop running and can delay test-worker shutdown. Register interval cleanup when watchEventLoop() creates it, or use try/finally around the test. (nodejs.org)

@Jinwoo-H
Jinwoo-H merged commit 12a8fc0 into main Oct 11, 2026
22 checks passed
Jinwoo-H added a commit that referenced this pull request Oct 11, 2026
…an deploy it (#27462)

The staging director deploy binds its digest to staging-gce-c4's checked-in image. E is main
2bdb553 (#27405 event-loop max, #27406 sidecar), already mirrored to staging. C4 itself
changes only through its own reviewed refresh workflow.
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