Xuanwo opened a new pull request, #8846:
URL: https://github.com/apache/opendal/pull/8846

   # Which issue does this PR close?
   
   N/A.
   
   # Rationale for this change
   
   Two write-lifecycle bugs accepted data that never reached the service, with 
no public API change.
   
   **`StdWriter::drop` lost written data.** `StdWriter` documents that files 
are closed when they go out of scope and that close errors are ignored on drop, 
but `Drop` only dropped the inner writer. Bytes written through 
`std::io::Write` stay in the 256 KiB `FuturesAsyncWriter` buffer until the 
writer is closed, so `std::io::copy`, `serde_json::to_writer`, and any other 
caller that relies on drop lost everything it wrote on `Memory`, 
`OneShotWriter`, `MultipartWriter`, and `BlockWriter`.
   
   **`BufferSink` state validation was incomplete.** 
`FuturesAsyncWriter::poll_write` only checked `BufferSink::is_closed()`. When 
`poll_close` moved the sink into `State::Closing(_)` and returned 
`Poll::Pending`, a concurrent `poll_write` still buffered bytes and returned 
`Ok(n)`; every later `poll_close` then failed with `state invalid: sink is 
closing` and those bytes were lost. At the sink level, `start_send` only 
rejected a closed sink: in `State::Closing(_)` it buffered an item that close 
discarded, in `State::Writing(_)` it overwrote the pending item while `w.write` 
was in flight, and in `State::Idle(Some(_))` with a non-empty buffer it dropped 
the previously buffered item.
   
   # What changes are included in this PR?
   
   - `StdWriter`'s `Drop` closes the writer when it is not already closed, 
ignoring close errors as documented, and then drops it in the async context. A 
writer that `close` already closed is skipped, so no writer is closed twice.
   - Add `pub(crate) FuturesAsyncWriter::is_closed`, which delegates to 
`BufferSink::is_closed` and is used by `StdWriter::drop`.
   - Add `pub(crate) BufferSink::is_closing`, and make `BufferSink::start_send` 
reject items that cannot be kept: closed sink, closing sink, sink with an 
in-flight write, and sink with an unflushed buffered item.
   - `FuturesAsyncWriter::poll_write` rejects writes with `state invalid: sink 
is closing` once close has started, using the same `format_std_io_error` path 
as the existing closed-sink check.
   - Add deterministic unit tests: `StdWriter` drop with and without `flush`, 
`StdWriter` drop after an explicit `close`, `FuturesAsyncWriter` rejected write 
while close is parked in `State::Closing`, and `BufferSink::start_send` 
rejection in the closing, in-flight write, and non-empty buffer states.
   
   # Are there any user-facing changes?
   
   Yes, both are bug fixes with unchanged public signatures.
   
   - Dropping a `blocking::Writer` converted through `into_std_write` now 
commits the data written through `std::io::Write` instead of discarding it, and 
close errors are still ignored.
   - `BufferSink` and `FuturesBytesSink` return an `Unexpected` error when an 
item is sent before the sink is ready, instead of accepting and later losing or 
corrupting data. `SinkExt::send`, `SinkExt::feed`, and `FuturesAsyncWriter` 
always call `poll_ready` first, so their behavior is unchanged.
   
   # Breaking changes
   
   # AI Usage Statement
   
   - Harness: pi
   - Model: DeepSeek
   - Effort: max
   - Role: Reproduced both write-lifecycle bugs against `memory` and local 
gated test writers, implemented the fixes in `std_writer.rs`, `buffer_sink.rs`, 
and `futures_async_writer.rs`, and wrote the regression tests. Each new test 
was confirmed to fail with the fixes reverted. Validation was run locally with 
`cargo fmt --all -- --check`, clippy for default, no-default, and all features, 
and `cargo test -p opendal-core --lib` for default, no-default, and blocking 
features. Assumption: `StdWriter::close` keeps the writer (rather than clearing 
it), so `Drop` relies on the closed-sink flag to avoid a second close.


-- 
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]

Reply via email to