Skip to content

Conversation

@snowmead
Copy link
Contributor

@snowmead snowmead commented Jan 22, 2026

Replaces the forest root write lock channel with a semaphore.

The goal is to simplify the locking mechanism for tasks to acquire them and release them in any eventuality (i.e. when the task either succeeds, errors out or panics) without having to manually release the lock within the event handler.

A RAII guard with a channel sender is passed to the task event handlers so when the event itself is dropped, the guard executes it's drop implementation and sends a unit value () to the BlockchainServiceEventLoop receiver channel to reassign the lock to another event.

With this approach we remove all boilerplate locking semantics in the event handlers, and ensure that, in any case, the forest write lock reassigns to pending events waiting on the lock immediately.

@snowmead snowmead added B5-clientnoteworthy Changes should be mentioned client-related release notes D2-noauditneeded🙈 PR doesn't need to be audited not-breaking Does not need to be mentioned in breaking changes labels Jan 22, 2026
@snowmead snowmead requested a review from ffarall January 22, 2026 20:59
@snowmead snowmead requested a review from TDemeco January 28, 2026 16:32
…k-semaphore

# Conflicts:
#	client/blockchain-service/src/handler_bsp.rs
@snowmead snowmead force-pushed the feat/forest-write-lock-semaphore branch from a9b0b3b to 405aeee Compare January 29, 2026 19:08
Copy link
Collaborator

@ffarall ffarall left a comment

Choose a reason for hiding this comment

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

Very neat. Love the way this ended up looking.

Besides some comments, I want to understand why this hasn't had an impact on any of our integration tests. Didn't anything release faster? And if that's not the case, could we make such a test case that evidences that this releases the lock faster on failure cases?

Anyway, not fully approving yet, just to merge other PRs first. Although this is not breaking, it is a change in one of the core functionalities of BSPs and MSPs, and I'd like it to be tested for some time in Stagenet.

fn drop(&mut self) {
// Send notification to the blockchain service event loop to process pending requests.
// Ignore errors as the receiver may be dropped during shutdown.
let _ = self.release_notifier.send(());
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this merits at least a log if this fails. Just so that we at least know something is off.

// This avoids a subtle event-loop spin: acquiring + immediately dropping the permit
// would notify `permit_release_receiver`, which would call back into this method
// even though there is no work to do.
let has_pending_work = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of doing this "pre-check" to check for pending work, and then acquiring the lock, why not using the next_event_data variable that is instantiated later, that will tell you if there is pending work or not, and only then try to acquire the lock.

My understanding is that when you checked bsp_handler.forest_root_write_semaphore.available_permits() you already checked that the lock is available.

// This avoids a subtle event-loop spin: acquiring + immediately dropping the permit
// would notify `permit_release_receiver`, which would call back into this method
// even though there is no work to do.
let has_pending_work = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of doing this "pre-check" to check for pending work, and then acquiring the lock, why not using the next_event_data variable that is instantiated later, that will tell you if there is pending work or not, and only then try to acquire the lock.

My understanding is that when you checked bsp_handler.forest_root_write_semaphore.available_permits() you already checked that the lock is available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

B5-clientnoteworthy Changes should be mentioned client-related release notes D2-noauditneeded🙈 PR doesn't need to be audited not-breaking Does not need to be mentioned in breaking changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants