-
Notifications
You must be signed in to change notification settings - Fork 11
feat(swift): transactions run withTaskCancellationHandler [WPB-27117]
#2340
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
Open
SimonThormeyer
wants to merge
18
commits into
main
Choose a base branch
from
simon/feat/swift-cancellation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
172f35e
feat(crypto-ffi): add `CoreCryptoCancellationToken`
SimonThormeyer 854a581
chore(crypto-ffi): add `CancellationSlot`
SimonThormeyer d6a5265
chore(crypto-ffi): `CoreCrypto` and `..Context` get a `CancellationSlot`
SimonThormeyer 95a9016
chore(crypto-ffi): add `Cancelled` future
SimonThormeyer 55dec1f
chore(crypto-ffi): add `CoreCryptoFfi.transaction_cancellable()` [WPB…
SimonThormeyer e75ee99
chore(crypto-ffi): share cancellation slot between `CoreCryptoFfi` an…
SimonThormeyer 92f2a59
chore: `CoreCryptoFfi` knows `PkiEnvironment`
SimonThormeyer 59c79cf
feat: add cancellation slot to pki env if it exists
SimonThormeyer 9eedabe
feat(swift): transactions run `withTaskCancellationHandler` [WPB-27117]
SimonThormeyer 6156564
test(swift): transaction cancellation propagates to callbacks
SimonThormeyer c67d8d1
build: add feature `cancellable-transactions`
SimonThormeyer aa42993
build: use feature flag when building swift/ios
SimonThormeyer c65e405
ci: update CI to conform with the new swift ffi lib target path
SimonThormeyer e425917
ci: simplify ffi-library action
SimonThormeyer c19624b
fixup! feat(crypto-ffi): add `CoreCryptoCancellationToken`
SimonThormeyer 1723008
fixup! build: use feature flag when building swift/ios
SimonThormeyer 4e7fca6
fixup! test(swift): transaction cancellation propagates to callbacks
SimonThormeyer 8bf9ff7
fixup! test(swift): transaction cancellation propagates to callbacks
SimonThormeyer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| use std::{ | ||
| pin::Pin, | ||
| sync::Arc, | ||
| task::{Context, Poll}, | ||
| }; | ||
|
|
||
| use crate::CoreCryptoCancellationToken; | ||
|
|
||
| pub(crate) struct Cancelled { | ||
| pub(crate) token: Arc<CoreCryptoCancellationToken>, | ||
| pub(crate) wakers_index: Option<usize>, | ||
| } | ||
|
|
||
| impl Future for Cancelled { | ||
| type Output = (); | ||
|
|
||
| fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
| let this = self.get_mut(); | ||
|
|
||
| if this.token.is_cancelled() { | ||
| return Poll::Ready(()); | ||
| } | ||
|
|
||
| let mut wakers = this.token.wakers(); | ||
| // Cancellation may have happened while waiting for the wakers lock. | ||
| if this.token.is_cancelled() { | ||
| return Poll::Ready(()); | ||
| } | ||
|
|
||
| wakers.insert(&mut this.wakers_index, cx.waker()); | ||
| Poll::Pending | ||
| } | ||
| } | ||
|
|
||
| impl Drop for Cancelled { | ||
| fn drop(&mut self) { | ||
| let Some(index) = self.wakers_index.take() else { | ||
| return; | ||
| }; | ||
|
|
||
| self.token.wakers().remove(index); | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| mod future; | ||
| mod slot; | ||
| mod token; | ||
|
|
||
| pub(crate) use future::Cancelled; | ||
| pub(crate) use slot::CancellationSlot; | ||
| pub use token::CoreCryptoCancellationToken; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| use std::sync::{Arc, Mutex}; | ||
|
|
||
| use crate::{CoreCryptoCancellationToken, CoreCryptoError, CoreCryptoResult}; | ||
|
|
||
| /// Makes the current transaction's cancellation token available to foreign callbacks. | ||
| /// | ||
| /// Cancelling the outer transaction does not automatically stop a nested Rust call that is | ||
| /// waiting for a Swift callback. The transport uses this token to stop waiting, which | ||
| /// lets the nested call return and the transaction unwind. | ||
| #[derive(Debug, Default)] | ||
| pub(crate) struct CancellationSlot { | ||
| current: Mutex<Option<Arc<CoreCryptoCancellationToken>>>, | ||
| } | ||
|
|
||
| /// Clears the active cancellation token from the slot when dropped. | ||
| #[derive(Debug)] | ||
| pub(crate) struct CancellationGuard { | ||
| slot: Arc<CancellationSlot>, | ||
| } | ||
|
|
||
| impl CancellationSlot { | ||
| /// Installs the token for the transaction currently holding the semaphore. | ||
| /// | ||
| /// Only one token may be installed at a time. | ||
| pub(crate) fn enter( | ||
| self: &Arc<Self>, | ||
| token: Arc<CoreCryptoCancellationToken>, | ||
| ) -> CoreCryptoResult<CancellationGuard> { | ||
| let mut current = self.current.lock().map_err(CoreCryptoError::ad_hoc)?; | ||
|
|
||
| assert!( | ||
| current.is_none(), | ||
| "only one transaction cancellation token may be in the slot; correct wrapper implementation never hits this" | ||
| ); | ||
|
|
||
| *current = Some(token); | ||
| Ok(CancellationGuard { slot: self.clone() }) | ||
| } | ||
|
|
||
| pub(crate) fn current(&self) -> CoreCryptoResult<Option<Arc<CoreCryptoCancellationToken>>> { | ||
| self.current | ||
| .lock() | ||
| .map_err(CoreCryptoError::ad_hoc) | ||
| .map(|guard| guard.clone()) | ||
| } | ||
| } | ||
|
|
||
| impl Drop for CancellationGuard { | ||
| fn drop(&mut self) { | ||
| let mut current = self.slot.current.lock().unwrap_or_else(|poisoned| { | ||
| log::warn!("recovering poisoned cancellation slot"); | ||
| poisoned.into_inner() | ||
| }); | ||
|
|
||
| *current = None; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.