Skip to content

fix(pg-cache): allow cache to reopen after close for process survival#787

Open
pyramation wants to merge 1 commit intomainfrom
devin/1772793999-fix-pg-cache-reopen
Open

fix(pg-cache): allow cache to reopen after close for process survival#787
pyramation wants to merge 1 commit intomainfrom
devin/1772793999-fix-pg-cache-reopen

Conversation

@pyramation
Copy link
Contributor

fix(pg-cache): allow cache to reopen after close for process survival

Summary

Fixes a server crash: Error: Cannot add to cache after it has been closed (key: postgres).

PgPoolCacheManager.close() permanently set closed = true with no reset path. When SIGTERM fires but the process doesn't exit (e.g., during provisioning or restart under load), all subsequent getPgPool() calls throw because set() is guarded by the closed flag. The module-level closePromise was also never reset, preventing future close() cycles from running.

Changes (2 lines of logic in postgres/pg-cache/src/lru.ts):

  • Reset this.closed = false after waitForDisposals() completes in PgPoolCacheManager.close(), so the cache can accept new entries post-shutdown
  • Wrap module-level close() body in try/finally to reset closePromise.promise = null, matching the existing pattern in graphile-cache.ts

Review & Testing Checklist for Human

  • Concurrent shutdown race: Verify that a set() call arriving during an active close() (between this.closed = true and this.closed = false) still correctly throws, and only succeeds after disposal completes
  • Repeated SIGTERM: Confirm that receiving a second SIGTERM after the cache reopens triggers a proper new close cycle (the closePromise reset + closed = false should allow this)
  • Manual test: Kill and restart the server process under load during provisioning to confirm the crash no longer occurs

Notes

  • The fix is intentionally minimal — two assignments and a try/finally wrapper
  • The graphile-cache package already uses this finally reset pattern (line 283 of graphile-cache.ts); this PR aligns pg-cache with that convention
  • Trailing newline added to end of file (formatting only)

Requested by: @pyramation
Link to Devin Session

The PgPoolCacheManager.close() permanently set closed=true with no way
to reopen. If the process received SIGTERM but didn't exit (e.g. during
provisioning or restart under load), all subsequent getPgPool() calls
threw 'Cannot add to cache after it has been closed'.

Changes:
- Reset closed=false after disposal completes in PgPoolCacheManager.close()
- Reset closePromise in module-level close() via finally block (matching
  the pattern already used in graphile-cache.ts)

This allows the cache to accept new entries if the process survives the
shutdown signal.
@devin-ai-integration
Copy link
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

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