-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose Semaphore::close
#3065
Expose Semaphore::close
#3065
Conversation
I believe that @darkson is correct. In order to expose If we want to make this change, we would need to make it in 1.0 as it is breaking. |
That makes sense. Should I close that for the time being then? |
I assigned this to the 1.0 milestone. We can leave it open for now. |
triage: this is no longer blocked |
@carllerche @Darksonn can we get this reviewed? I don't have much context |
or figure out how to move forward here. |
I'll take a look. |
To move forward this needs to have the acquire methods return a result in case the semaphore has been closed. Or an alternative error-handling should be proposed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As @Darksonn pointed out in https://github.com/tokio-rs/tokio/pull/3065/files#r513557608, the Semaphore::acquire
and Semaphore::acquire_owned
methods currently unwrap
the inner semaphore's result --- this will panic if we close the semaphore:
tokio/tokio/src/sync/semaphore.rs
Lines 98 to 105 in 917ce40
/// Acquires permit from the semaphore. | |
pub async fn acquire(&self) -> SemaphorePermit<'_> { | |
self.ll_sem.acquire(1).await.unwrap(); | |
SemaphorePermit { | |
sem: &self, | |
permits: 1, | |
} | |
} |
These need to be changed to also return Result
, like
/// Acquires permit from the semaphore.
pub async fn acquire(&self) -> Result<SemaphorePermit<'_>, AcquireError> {
self.ll_sem.acquire(1).await?;
Ok(SemaphorePermit {
sem: &self,
permits: 1,
})
}
We'll also need to add a public error type for indicating that the semaphore is closed (could be a re-export of batch_semaphore::AcquireError
). Similarly, we should update TryAcquireError
to distinguish between cases where no permits are available and cases where the semaphore is closed (again, this could be a re-export of the inner batch_semaphore
error type). Finally, we will want to add to the docs to explain the errors returned by these methods.
Then, I think this should be good to merge. Thanks!
@zaharidichev we branched off 1.0. Are you able to complete this PR? Thanks! |
@carllerche Yes, I am able. Does it need to happen today? |
Not today. This week is fine! |
Fixes: tokio-rs#3061 Signed-off-by: Zahari Dichev <[email protected]>
Signed-off-by: Zahari Dichev <[email protected]>
Signed-off-by: Zahari Dichev <[email protected]>
917ce40
to
cbd629a
Compare
Signed-off-by: Zahari Dichev <[email protected]>
tokio/src/sync/batch_semaphore.rs
Outdated
Closed, | ||
|
||
/// The has no available permits. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The semaphore
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall, the change looks correct to me! I had some docs suggestions that it would be nice to address before merging — and let's also address Carl's comment about adding a usage example to Semaphore::close
.
tokio/src/sync/batch_semaphore.rs
Outdated
@@ -41,15 +41,21 @@ struct Waitlist { | |||
closed: bool, | |||
} | |||
|
|||
/// Error returned by `Semaphore::try_acquire`. | |||
/// Error returned from the `Semaphore::try_acquire` function. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This probably ought to be a link?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hawkw Correct me if I am wrong, but it seems we cannot link to non public methods. Having this type be located in batch_semaphore
and linking to the method in semaphore
seems a bit odd. Am I missing something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type is exposed in the public API. Users will only ever see it via the public Semaphore
type's methods; they shouldn't need to be aware of batch_semaphore
, which is an implementation detail. I think it's correct for this to link to the public methods for readers of the documentation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. Thank you !
tokio/src/sync/batch_semaphore.rs
Outdated
Closed, | ||
|
||
/// The has no available permits. | ||
NoPermits, | ||
} | ||
/// Error returned by `Semaphore::acquire`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and this one?
tokio/src/sync/semaphore.rs
Outdated
@@ -96,21 +88,21 @@ impl Semaphore { | |||
} | |||
|
|||
/// Acquires permit from the semaphore. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be nice if this comment said something about errors, like
/// Acquires permit from the semaphore. | |
/// Acquires a permit from the semaphore. | |
/// | |
/// If the semaphore has been closed, this returns an [`AcquireError`]. | |
/// Otherwise, this returns a [`SemaphorePermit`] representing the | |
/// acquired permit. |
or something?
tokio/src/sync/semaphore.rs
Outdated
sem: &self, | ||
permits: 1, | ||
} | ||
}) | ||
} | ||
|
||
/// Acquires `n` permits from the semaphore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and maybe this should say something like
/// Acquires `n` permits from the semaphore | |
/// Acquires `n` permits from the semaphore. | |
/// | |
/// If the semaphore has been closed, this returns an [`AcquireError`]. | |
/// Otherwise, this returns a [`SemaphorePermit`] representing the | |
/// acquired permits. |
tokio/src/sync/batch_semaphore.rs
Outdated
#[derive(Debug)] | ||
pub(crate) enum TryAcquireError { | ||
pub enum TryAcquireError { | ||
/// The semaphore has been closed and cannot issue new permits |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worth linking to the Semaphore::close
method here?
/// The semaphore has been closed and cannot issue new permits | |
/// The semaphore has been [closed] and cannot issue new permits. | |
/// | |
/// [closed]: crate::sync::Semaphore::close |
tokio/src/sync/batch_semaphore.rs
Outdated
NoPermits, | ||
} | ||
/// Error returned by `Semaphore::acquire`. | ||
/// | ||
/// An `acquire` operation can only fail if the semaphore has been | ||
/// closed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might be worth linking to the close method here too?
/// closed. | |
/// [closed]. | |
/// | |
/// [closed]: crate::sync::Semaphore::close |
@zaharidichev Thanks for updating the PR. I have no additional feedback. Once @hawkw's comments are addressed, I'm fine w/ merging this 👍. |
Signed-off-by: Zahari Dichev <[email protected]>
I added some additional docs to other methods as well. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, the docs look great, thanks for adding examples. I'm going to merge this.
Thanks again for working on this, @zaharidichev!
Motivation
The need to expose
Semaphore::close
as explained in #3061.Solution
Expose
Semaphore::close