Skip to content

Introduce better ZoneState locks#606

Open
bal-e wants to merge 3 commits into
mainfrom
better-zone-state-locks
Open

Introduce better ZoneState locks#606
bal-e wants to merge 3 commits into
mainfrom
better-zone-state-locks

Conversation

@bal-e
Copy link
Copy Markdown
Contributor

@bal-e bal-e commented Apr 29, 2026

This PR introduces ZoneStateLock, a wrapper around the ZoneState lock with higher-level methods. ZoneStateLock alleviates boilerplate and inconsistencies around how we use the ZoneState lock.

Previously, access to zone state was obtained via zone.state.lock().unwrap(). Some call sites used .expect() or ? instead of .unwrap(), introducing inconsistencies. Most importantly: most call sites did not call .mark_dirty(), so changes to the zone were not being persisted with regularity.

ZoneStateLock wraps an RwLock instead of a Mutex because some call sites only read the zone state, and it seemed better to expose that functionality rather than use write locks everywhere. Rather than a simple .lock() method, a write lock must be obtained with the obtuse name .write_cleanly(), and that method tells users to prefer Zone::write(). Zone::write() marks the state as dirty automatically so that callers don't have to remember to.

In addition, a new OwnedZoneHandle type is introduced. This works around some borrowing limitations of ZoneHandle. The new Zone::write_handle() method obtains a write lock over the zone state, marks it as dirty, and returns an OwnedZoneHandle so that handle-related methods can be called conveniently.

All call sites of zone.state.lock() have been updated to use the new locking methods, and most of them (particularly those that needed ZoneHandle) are much simpler now.

Note: this PR is very prone to (causing and incurring) merge conflicts.

Resolves #599.


  • If you are changing Rust code or integration tests (Cargo.*, crates/, etc/, integration-tests/, src/):
    • Did you run the integration tests with act through the act-wrapper (as described in TESTING.md)?

@bal-e bal-e requested a review from tertsdiepraam April 29, 2026 11:43
@bal-e bal-e self-assigned this Apr 29, 2026
@bal-e bal-e force-pushed the better-zone-state-locks branch from c24d902 to e3fb33a Compare April 29, 2026 11:56
Comment thread src/signer/incremental.rs
.map_err(|e| SignerError::SigningError(format!("zone.state.lock() failed: {e}")))?;
policy = zone_state.policy.clone().expect("should be there");
};
let policy = { zone.read().policy.clone().unwrap() };
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The {} is important here right? Maybe we should have a comment saying that it shouldn't be removed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Actually no; the lock would be dropped at the end of the statement anyway. But I can at least document that.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yeah I guess that would be nice. Do you just put them there for extra clarity?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I think it's nice to explicitly see that. And rustfmt doesn't try to remove it.

Comment thread src/zone/mod.rs
/// consistent with each other, and that changes to the zone happen in a
/// single (sequentially consistent) order.
pub state: Mutex<ZoneState>,
/// The state is locked for consistency; see [`ZoneStateLock`].
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you link to ZoneState here? That would be nice for goto definition from rust-analyzer.

Comment thread src/zone/mod.rs
///
/// This is a convenience type. By owning the write lock, it can simplify
/// construction of `ZoneHandle`s for quick uses.
pub struct OwnedZoneHandle<'a> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This name is very confusing to me because it doesn't actually own the zone but only a write lock to it. Maybe WriteZoneHandle?

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.

Add a wrapper around Mutex<ZoneState>

2 participants