Xuanwo opened a new pull request, #8862:
URL: https://github.com/apache/opendal/pull/8862
# Which issue does this PR close?
None.
# Rationale for this change
`oio::OneShotCopier` had two bugs:
1. **Cancellation safety bug in `OneShotCopier::close`**: `close()` removed
the in-flight future via `self.fut.take()` before awaiting it. When an inner
`TimeoutLayer` times out and drops the pending `close()` (or `next()`) future
and an outer `RetryLayer` retries, `self.fut` is already `None`. For
`OneShotCopier::new`, this falls through to `"one-shot copier has no future to
drive"` marked persistent, turning a transient timeout into a permanent copy
failure; for `OneShotCopier::new_with`, it drops the pending future and
unnecessarily rebuilds a duplicate copy request.
2. **Soundness bug (safe-code UB) in `OneShotCopier::new_with`**:
`OneShotCopier` had an unconditional `unsafe impl Send for OneShotCopier {}`,
while `OneShotCopier::new_with` only bounded `F: FnMut() -> Fut + 'static`
(missing `MaybeSend`) and boxed the factory without `+ Send` on non-wasm32
targets. Safe Rust could pass a `!Send` closure (e.g., capturing `Rc<Cell<_>>`)
and move `OneShotCopier` across threads, causing undefined behavior.
# What changes are included in this PR?
- Poll `self.fut` in place (`self.fut.as_mut()`) across `.await` in
`OneShotCopier::close`, clearing `self.fut = None` only after the future
resolves (`Ok` or `Err`). On `Ok`, record `meta`, mark `consumed = true`, and
drop `self.factory`. On `Err` when `self.factory.is_none()`, mark `consumed =
true` and return the error as persistent.
- Tighten `OneShotCopier::new_with` to require `F: FnMut() -> Fut +
MaybeSend + 'static` (a soundness fix closing safe-code UB; zero workspace
callers affected), store the factory as `Box<dyn FnMut() ->
BoxedStaticFuture<Result<Metadata>> + Send>` on non-wasm32 targets (and without
`+ Send` on `wasm32`), and gate `unsafe impl Send for OneShotCopier {}` with
`#[cfg(target_arch = "wasm32")]` so the compiler verifies `Send` on non-wasm32
targets.
- Add deterministic unit tests in `core/core/src/raw/oio/copy/api.rs`
covering `OneShotCopier::new` and `OneShotCopier::new_with` cancellation and
retry, simulated `TimeoutLayer` + `RetryLayer` retry via `next()`, `abort()`
dropping an in-flight future, and `Send + Sync` bounds.
# Are there any user-facing changes?
- `oio::OneShotCopier::new_with` now requires the factory closure `F` to
implement `MaybeSend` (`Send` on non-wasm32 targets). This is a soundness fix
closing safe-code UB; callers that previously passed a `!Send` factory on
non-wasm32 targets relied on unsound behavior, and zero workspace callers are
affected.
- `OneShotCopier` now preserves its in-flight future when `next()` or
`close()` is cancelled while pending (for example, by `TimeoutLayer`), allowing
retries to resume cleanly.
# Breaking changes
# AI Usage Statement
- Harness: pi / T3 Code
- Model: Gemini 4 Neon
- Effort: High
- Role: Implemented the `OneShotCopier` cancellation-safety and `MaybeSend`
soundness fixes, added deterministic unit tests in
`core/core/src/raw/oio/copy/api.rs`, verified all workspace
`OneShotCopier::new_with` callers and local checks (including
`wasm32-unknown-unknown`), and drafted this PR.
--
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]