-
Notifications
You must be signed in to change notification settings - Fork 234
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
Explain what happens with async code #2360
Comments
Better docs literally just landed - https://mozilla.github.io/uniffi-rs/next/internals/async.html. That's quite a laundry list though, we're doing our best. |
Would you mind dive deep into cancellation by exception? What's the order for propagating foreign exception into rust future? I'm trying to capture python KeyboardInterrupt during a for loop while pulling from rust future, it seems that the uniffi generated foreign code are taking control of the exception instead of letting me manually calling the cancel function. This is the naive setup to test it. Basically I created an background task that fill the rust channel, and allow python to fetch the result using async. This is the exposed rust async function #[uniffi::method]
async fn try_pull(&self) -> ApiResult<Option<Sample>> {
let mut rx = self.rx.lock().await;
let res = rx.recv().await.flatten();
Ok(res)
} And this is the where the future is called try:
h = Handle()
while msg := await h.try_pull():
logger.info("incoming")
logger.info("ended")
except Exception as e:
logger.debug(e)
if h is not None:
await h.exit()
finally:
logger.warning("all done") And the exception I got is something similar to this: Exception ignored on calling ctypes callback function
in _uniffi_continuation_callback
@_UNIFFI_RUST_FUTURE_CONTINUATION_CALLBACK And my loop just hangs there without letting me call and exit function that drops the background task |
This would be a great addition to the docs, I just made #2372 for that. In general, I think it's best to implement cancellation explicitly in the interface. For example, maybe your interface can have a The alternative is to try to use the language's builtin cancellation mechanisms, but that's not very reliable. I believe it's supported on Kotlin, but not Swift and I don't think it's implemented for Python either. There was a ton of discussion in #1768 on why we didn't want to implement it for Swift. Separate from all of this is using |
Happy new year! Thanks for the detailed diagram about async-ffi. |
The current documentation is very lacking regarding what happens with async code.
Everything is very "magical".
I have the following questions:
It's certainly not Rust, or is it? I don't know, it's not documented.
How can I use my own executor? we currently have a static instance of a tokio rt where we spawn every single one of our async calls, which is not good from an ergonomics POV.
The text was updated successfully, but these errors were encountered: