silver-ymz commented on PR #2192:
URL:
https://github.com/apache/incubator-opendal/pull/2192#issuecomment-1537041869
What I mean is not `Send + Sync`.
```
stdio_cloned.set(Ok((stdin, stdout))).unwrap();
drop(stdio_cloned);
if let Some(once_cell) = Arc::get_mut(&mut stdio)
```
Because these two strings of code may be executed in different threads, it
may happen that `Arc strong_count` decrements behavior earlier than setting
stdio_cloned. We need to set extra memory order to avoid it.
This is the modified code.
<details>
```
let mut stdio = Arc::new(once_cell::sync::OnceCell::new());
let stdio_cloned = Arc::clone(&stdio);
+ let has_set = Arc::new(AtomicBool::new(false));
+ let has_set_cloned = has_set.clone();
let mut future = Box::pin(async move {
let res = session
.subsystem("sftp")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::null())
.spawn()
.await;
let mut child = match res {
Ok(child) => child,
Err(err) => {
stdio_cloned.set(Err(err)).unwrap(); // Err
drop(stdio_cloned);
+ has_set_cloned.store(true, Ordering::Release);
return;
}
};
let stdin = child.stdin().take().unwrap();
let stdout = child.stdout().take().unwrap();
stdio_cloned.set(Ok((stdin, stdout))).unwrap();
drop(stdio_cloned);
+ has_set_cloned.store(true, Ordering::Release);
// Wait forever until being dropped
std::future::pending::<()>().await;
debug!("sftp child process exited");
// Use child, session after await to keep them alive
drop(child);
drop(session);
});
let (stdin, stdout) = std::future::poll_fn(|cx| {
let _ = future.as_mut().poll(cx);
+ if has_set.load(Ordering::Acquire) {
let once_cell = Arc::get_mut(&mut stdio).unwrap();
// future must have set some value before dropping stdio_cloned
return Poll::Ready(once_cell.take().unwrap());
}
Poll::Pending
})
.await?;
```
</details>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]