Skip to content

fix(graphql): recover schema caches after listener interruptions - #1999

Merged
Anmol1696 merged 1 commit into
mainfrom
fix/schema-listener-recovery
Nov 14, 2026
Merged

Anmol1696 merged 1 commit into
mainfrom
fix/schema-listener-recovery

Conversation

@Anmol1696

Copy link
Copy Markdown
Contributor

GraphQL servers recover schema freshness after their PostgreSQL notification connection is interrupted. Previously, a connection that ended without an error or silently stalled could leave HTTP serving cached schemas indefinitely. Even a successful reconnect could retain schemas changed while disconnected, because PostgreSQL does not replay missed notifications.

This completes the listener recovery path alongside the cache eviction and in-flight build fixes in #1962 and #1977.

  • Handle connection errors and end, destroy failed sessions, and coalesce reconnect attempts with a 5-second retry delay.
  • Probe the held session every 30 seconds; bound LISTEN and heartbeat queries to 5 seconds. Shutdown cancels timers and discards late connections without waiting for UNLISTEN.
  • After LISTEN succeeds, invalidate routing, GraphQL handler, and storage discovery caches. Advance a global generation so pending builds cannot restore stale handlers.
  • Fence routing lookups and requests that resolved their API before invalidation. Re-resolve rather than caching or building from the old configuration.
  • Catch notification flush failures and invalidate all services conservatively. Log subscription success only after LISTEN completes.

Recovery clears process-wide caches and can cause a temporary burst of schema builds. This PR changes the server package only; no database migration, Cloud configuration, or deployment is included.

Validation

  • Built the server-test dependency closure and the final server package.
  • Server unit suite: 292 tests passed across 27 suites.
  • PostgreSQL HTTP integration: 8 tests passed across 2 suites, including existing scoped routing coverage.
  • The new integration test warms a real GraphQL schema, terminates the listener backend, commits DDL and a missed notification, verifies the old schema during the gap, then verifies the rebuilt schema after reconnect and a subsequent notification-driven update.
  • Regression tests cover end-only disconnects, stalled LISTEN/heartbeat, late completion, shutdown, overlapping connection attempts, cache invalidation, and lookups/builds crossing recovery. The end-only, missed-cache, and pending-route tests were observed failing before their fixes.
  • ESLint on changed TypeScript files and git diff --check passed.

Based on freshly fetched main at 92e15c0778465b2c51f32dbf6de8a44e2160ef92. No devnet recovery or deployment was performed.

@luxor-code-review

luxor-code-review Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Review complete. No blocking issues — approved ✅; 1 nitpick below.

🧹 Nitpicks (1) — 🟢 1 low
  • 🟢 Latched listenerConnecting flag blocks listener recovery (server.ts:277) — The rewritten addEventListener (graphql/server/src/server.ts:277-287) sets this.listenerConnecting = true before calling this.getPool().connect(...), and the flag is only reset inside the connect callback.

This PR reworks the server middleware pipeline: cache-generation invalidation and single-flight admission in graphql/server/src/middleware/cache-generation.ts and api.ts, a rewritten flush path in flush.ts, and single-flight-aware graphile middleware in graphile.ts. The notify LISTEN listener in graphql/server/src/server.ts gains a latched listenerConnecting flag and stricter close/cleanup semantics, with its contract documented in graphql/server/README.md. New and updated tests in graphql/server/src/middleware/__tests__/ and graphql/server-test/__tests__/ cover flush ordering, routing, single-flight behavior, and listener lifecycle including reconnect and shutdown races.

Files Change
graphql/server/src/middleware/cache-generation.ts, api.ts, types.ts Reworks generation-counter invalidation, single-flight admission, and shared middleware types
graphql/server/src/middleware/flush.ts, graphile.ts Rewrites flush path and adds single-flight-aware graphile middleware
graphql/server/src/server.ts Reworks notify LISTEN listener lifecycle with latched connect flag and stricter teardown
graphql/server/src/middleware/__tests__/* Adds/updates unit tests for api, flush, routing, and single-flight middleware
graphql/server/src/__tests__/, graphql/server-test/__tests__/ Adds notify-listener unit and integration tests
graphql/server/README.md Documents the new listener and flush semantics

Reviewed commit: 425c725

@Anmol1696
Anmol1696 merged commit a2232fd into main Nov 14, 2026
20 checks passed
@Anmol1696
Anmol1696 deleted the fix/schema-listener-recovery branch November 14, 2026 08:53
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