Skip to content

Return false when an unless_exist write fails - #323

Open
tadhgboyle wants to merge 1 commit into
rails:mainfrom
tadhgboyle:fix-unless-exist-failed-writes
Open

tadhgboyle wants to merge 1 commit into
rails:mainfrom
tadhgboyle:fix-unless-exist-failed-writes

Conversation

@tadhgboyle

@tadhgboyle tadhgboyle commented Oct 14, 2026 •

Copy link
Copy Markdown

Problem

write(..., unless_exist: true) sets written = true inside the lock_and_write block, before the entry is persisted. If persistence then raises a handled database error, Failsafe returns nil, but write_entry ignores that result and still returns true.

This also causes the local cache to retain a value that was never persisted. Callers using the return value to acquire a lock can proceed without owning it.

Fix

Derive written from the result of entry_lock_and_write instead of mutating it inside the block. A completed write returns its serialized payload and becomes true; an existing unexpired entry or a handled failure returns nil and becomes false.

Tests

Added regressions for:

  • A deadlock during persistence returning false rather than true.
  • A statement timeout during persistence not populating the local cache.

Use the result of lock_and_write instead of marking a write successful before persistence. Cover handled write failures and avoid populating the local cache with an unpersisted value.
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