Skip to content
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

fix(wasm): don't kill task on drop #4572

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions crates/matrix-sdk-common/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ where
let _ = future.await;
});

JoinHandle { remote_handle, abort_handle }
JoinHandle { remote_handle: Some(remote_handle), abort_handle }
}

#[cfg(target_arch = "wasm32")]
#[derive(Debug)]
pub struct JoinHandle<T> {
remote_handle: RemoteHandle<T>,
remote_handle: Option<RemoteHandle<T>>,
abort_handle: AbortHandle,
}

Expand All @@ -64,6 +64,16 @@ impl<T> JoinHandle<T> {
}
}

#[cfg(target_arch = "wasm32")]
impl<T> Drop for JoinHandle<T> {
fn drop(&mut self) {
// don't abort the spawned future
if let Some(h) = self.remote_handle.take() {
h.forget();
}
}
}

#[cfg(target_arch = "wasm32")]
impl<T: 'static> Future for JoinHandle<T> {
type Output = Result<T, JoinError>;
Expand All @@ -72,8 +82,10 @@ impl<T: 'static> Future for JoinHandle<T> {
if self.abort_handle.is_aborted() {
// The future has been aborted. It is not possible to poll it again.
Poll::Ready(Err(JoinError))
} else if let Some(handle) = self.remote_handle.as_mut() {
Pin::new(handle).poll(cx).map(Ok)
} else {
Pin::new(&mut self.remote_handle).poll(cx).map(Ok)
Poll::Ready(Err(JoinError))
}
}
}
Expand Down
Loading