From a901506a536bf12b081eea6d6c70777d0fdcc565 Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Wed, 16 Oct 2024 17:48:31 +0200 Subject: [PATCH] fix(ffi): don't panic when joining after having cancelled a media upload Fixes #3573. --- bindings/matrix-sdk-ffi/src/timeline/mod.rs | 23 ++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/bindings/matrix-sdk-ffi/src/timeline/mod.rs b/bindings/matrix-sdk-ffi/src/timeline/mod.rs index 0dd994da43d..87dbb859356 100644 --- a/bindings/matrix-sdk-ffi/src/timeline/mod.rs +++ b/bindings/matrix-sdk-ffi/src/timeline/mod.rs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::{collections::HashMap, fmt::Write as _, fs, sync::Arc}; +use std::{collections::HashMap, fmt::Write as _, fs, panic, sync::Arc}; use anyhow::{Context, Result}; use as_variant::as_variant; @@ -1205,11 +1205,28 @@ impl SendAttachmentJoinHandle { #[matrix_sdk_ffi_macros::export] impl SendAttachmentJoinHandle { + /// Wait until the attachment has been sent. + /// + /// If the sending had been cancelled, will return immediately. pub async fn join(&self) -> Result<(), RoomError> { - let join_hdl = self.join_hdl.clone(); - RUNTIME.spawn(async move { (&mut *join_hdl.lock().await).await.unwrap() }).await.unwrap() + let handle = self.join_hdl.clone(); + let mut locked_handle = handle.lock().await; + let join_result = (&mut *locked_handle).await; + match join_result { + Ok(res) => res, + Err(err) => { + if err.is_cancelled() { + return Ok(()); + } + error!("task panicked! resuming panic from here."); + panic::resume_unwind(err.into_panic()); + } + } } + /// Cancel the current sending task. + /// + /// A subsequent call to [`Self::join`] will return immediately. pub fn cancel(&self) { self.abort_hdl.abort(); }