Skip to content
This repository has been archived by the owner on Dec 3, 2024. It is now read-only.

Refactor read and write locks to minimize lock duration #251

Merged
merged 1 commit into from
Aug 23, 2024

Conversation

Ayiga
Copy link
Member

@Ayiga Ayiga commented Aug 23, 2024

Closes #249

This PR:

Addresses locks being held across async boundaries by instead wrapping the locks within a short-lived closure. Additionally, it acquires the locks in a separate statement to make it clear that the lock is held for the entire duration of the closure. This is also done for accesses that may not result in a lock outliving the access statement, but is done for clarity and consistency.

This PR does not:

Address any locking behavior outside of service.rs

Addresses issue #249

There are some lock acquisition behavior that exists in `service.rs` which leads to
non-obvious lock retention lifetimes.  The syntactic sugar pattern matching that
rust allows ends up hiding the lifetime acquisition of references to a locked
global state.  These locks end up getting held across async boundaries unnecessarily
which will only lead to lock contention when it is not needed.

Additionally, in at least one case, the lock acquisition lifetimes will result in a deadlock
as a `write_arc` is being attempted when the write lock is already being held in a
parent closure: `service.rs:630`.

In order to improve the lock contention and to avoid the deadlock potential the lock
guards have been made explicit and placed behind a closure ensuring that it will
be cleaned up as soon as the information needed from it is accessed.  Any information
outside of the lock is cloned in order to ensure no further lock is required.  The data
that has been cloned / copied out of the lock fields was already being cloned / copied
regardless.  The end result should be an improved representation of the lock guard's
lifetimes, and clarity of the closures that hold a lock guard.
Copy link
Member

@shenkeyao shenkeyao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@Ayiga Ayiga merged commit 39c87e5 into main Aug 23, 2024
5 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix locks in block claim functions
2 participants