Clean up completed entries from the LockRegistry - #4545
Open
alecgrieser wants to merge 2 commits into
Open
Conversation
This updates the `LockRegistry` so that it cleans up entries that are no longer necessary. It does this by scheduling a clean up task to delete the entry for the given key from the map, but only if the most recently registered lock for that key still matches the current lock. It is safe to do this when that lock is equivalent to the behavior when there is no lock present, which is to say, when the lock's pending reads and writes are all done, along with the lock's task. Note that we cannot clean up the lock if the only thing that has happened is that the lock has been released. The reason for this is that if we have a key that has: 1. A read lock `l1` 1. A second read lock `l2` with parent `l1` So assume `l2` is the current lock in `heldLocks` for a given lock identifier. The `l1` and `l2` tasks can be executed at the same time, so if `l2` removed the entry for the lock in `heldLocks` when it was done, then we'd have nothing to stop a new write operation `l3` from being acquired before `l1` had actually finished, breaking our contract. We instead need to wait for `l1` to also complete, which is done by having the clean-up operation wait on `l2`'s pending reads (even if `l2` is a read lock). This resolves FoundationDB#4544.
g31pranjal
approved these changes
Sep 4, 2026
g31pranjal
left a comment
Member
There was a problem hiding this comment.
I think we are pretty well-off with the cleanup proposed in the PR. However, should we add a couple of unit tests around it ?
Collaborator
Author
|
I've added some unit tests here. It ended up being pretty easy, though they depend on the fact that as we're not using |
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.
This updates the
LockRegistryso that it cleans up entries that are no longer necessary. It does this by scheduling a clean up task to delete the entry for the given key from the map, but only if the most recently registered lock for that key still matches the current lock. It is safe to do this when that lock is equivalent to the behavior when there is no lock present, which is to say, when the lock's pending reads and writes are all done, along with the lock's task.Note that we cannot clean up the lock if the only thing that has happened is that the lock has been released. The reason for this is that if we have a key that has:
l1l2with parentl1So assume
l2is the current lock inheldLocksfor a given lock identifier. Thel1andl2tasks can be executed at the same time, so ifl2removed the entry for the lock inheldLockswhen it was done, then we'd have nothing to stop a new write operationl3from being acquired beforel1had actually finished, breaking our contract. We instead need to wait forl1to also complete, which is done by having the clean-up operation wait onl2's pending reads (even ifl2is a read lock).This resolves #4544.