Enforce key locking for nested transactions (#431) - #439
Draft
makoto-developer wants to merge 1 commit into
Draft
Conversation
Nested transactions previously short-circuited on a boolean flag and ran the function without acquiring locks, so an inner transaction locking a different key than the enclosing one would proceed with no locking in place. Store the set of locked keys in :cachex_transaction instead of a boolean so a nested transaction can verify that every key it requests is already locked by the enclosing transaction, raising a Cachex.Error otherwise. Refs whitfin#431
makoto-developer
force-pushed
the
fix/nested-transaction-locking
branch
from
July 28, 2026 16:08
d213d5b to
d60cdbd
Compare
whitfin
self-requested a review
July 28, 2026 18:39
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fixes #431.
Cachex.Services.Locksmith.transaction/3previously short-circuited on a boolean flag when already inside a transactional context and ran the function immediately, without acquiring any locks. As noted in the issue, an inner transaction that locks a different key than the enclosing one then proceeds with no locking actually in place — a silent correctness bug.How
Following the approach suggested in the issue,
:cachex_transactionnow stores the set of keys locked by the current transaction instead of a boolean:start_transaction/0stores an empty key list;transaction?/0still returns a boolean (!= false), so existing callers (e.g.write/3) are unaffected.Locksmith.Queue's:transactionhandler records the keys it locks in the process dictionary for the duration of the function, so nested transactions can see what is currently held.transaction/3, when already inside a transactional context, verifies that every requested key is already locked by the enclosing transaction. If so it runs inline (a redundant lock, as before); if any requested key is not locked it raisesCachex.Error, since executing against unlocked keys inside a transaction is almost certainly a logic error. Keyless transactions (clear/purge/reset) and lock-free serialized contexts remain unrestricted.Tests
Added a
LocksmithTestcase covering both the allowed (subset of locked keys → runs inline) and rejected (unlocked key → surfaces as an error) nested paths, and updated the existing internal-representation test for the new:cachex_transactionvalue.mix test,mix format --check-formattedandmix credo --allall pass locally.Question re:
write/3You mentioned this "likely applies to the similar block" in
write/3too. I kept this PR focused ontransaction/3, because enforcing the same rule onwrite/3is a wider behavioural change (writes to non-locked keys inside a transaction would start raising) and needs a carve-out to keep the lock-freeexecutepath working, since it shares the empty-lock-set representation. I have that version ready — happy to fold it in here or send it as a follow-up. Which would you prefer?