Skip to content

Commit a79a4be

Browse files
authored
Oneshot should hold the inner (#222)
When using other Executors for poll tasks, such as `wasm-bindgen-futures` (yes, I removed the web-related ones and just kept the task and queue). wake and poll are separate calls, and when the host calls `sender` of `wit-bindgen-rust`'s When the host calls the `send` method of `Sender` of `wit-bindgen-rust`, `Sender` will be released and the inner instance will be released at the same time, which will cause `OneShot` to not get the `inner` instance.
1 parent 458a664 commit a79a4be

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

crates/rust-wasm/src/futures.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::cell::RefCell;
44
use std::future::Future;
55
use std::mem;
66
use std::pin::Pin;
7-
use std::rc::{Rc, Weak};
7+
use std::rc::Rc;
88
use std::sync::Arc;
99
use std::task::*;
1010

@@ -110,7 +110,7 @@ impl Wake for PollingWaker {
110110
}
111111

112112
pub struct Oneshot<T> {
113-
inner: Weak<OneshotInner<T>>,
113+
inner: Rc<OneshotInner<T>>,
114114
}
115115

116116
pub struct Sender<T> {
@@ -135,7 +135,7 @@ impl<T> Oneshot<T> {
135135
});
136136
(
137137
Oneshot {
138-
inner: Rc::downgrade(&inner),
138+
inner: Rc::clone(&inner),
139139
},
140140
Sender { inner },
141141
)
@@ -146,13 +146,7 @@ impl<T> Future for Oneshot<T> {
146146
type Output = T;
147147

148148
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T> {
149-
let inner = match self.inner.upgrade() {
150-
Some(inner) => inner,
151-
// Technically this isn't possible in the initial draft of interface
152-
// types unless there's some serious bug somewhere.
153-
None => panic!("completion callback was canceled"),
154-
};
155-
let mut state = inner.state.borrow_mut();
149+
let mut state = self.inner.state.borrow_mut();
156150
match mem::replace(&mut *state, OneshotState::Start) {
157151
OneshotState::Done(t) => Poll::Ready(t),
158152
OneshotState::Waiting(_) | OneshotState::Start => {

0 commit comments

Comments
 (0)