Skip to content

fix(core): Return Ok(0) instead of panicking on zero-length write - #8162

Merged
Xuanwo merged 1 commit into
apache:mainfrom
lumaril-oss:fix-futures-async-writer-empty-write
Aug 26, 2026
Merged

fix(core): Return Ok(0) instead of panicking on zero-length write#8162
Xuanwo merged 1 commit into
apache:mainfrom
lumaril-oss:fix-futures-async-writer-empty-write

Conversation

@bavardage

Copy link
Copy Markdown
Contributor

FuturesAsyncWriter::poll_write panicked with frozen buffer must be valid when called with an empty buffer, even though both std::io::Write::write and futures::AsyncWrite::poll_write allow a zero-length input and specify Ok(0) as the result.

FlexBuf::put returns 0 both when the buffer is already frozen and when the input slice is empty. The loop in poll_write assumed only the first case, so on an empty input it called get() on a buffer that was never frozen and hit the expect. BufferSink already guards this case, so add the same guard to the adapter.

The blocking StdWriter path is fixed transitively, since it forwards to FuturesAsyncWriter::write.

Closes #8161

Which issue does this PR close?

Closes #8161

What changes are included in this PR?

  • core/core/src/types/write/futures_async_writer.rs: return Poll::Ready(Ok(0)) from
    poll_write when the input buffer is empty, before entering the drain loop.
  • A unit test in the same file’s existing mod tests, covering an empty write before and
    after a non-empty one, and asserting the surrounding payload still commits and reads back
    intact.

Are there any user-facing changes?

Yes but nothing breaking. The zero length previously panicked and now returns Ok(0). No API signature
changes, and no behavior change for any non-empty write.

AI Usage Statement

AI identified this bug when using opendal in my project and identified this as a potential fix.
The fix is pretty simple though!

@bavardage
bavardage requested a review from Xuanwo as a code owner August 26, 2026 15:51
`FuturesAsyncWriter::poll_write` panicked with `frozen buffer must be
valid` when called with an empty buffer, even though both
`std::io::Write::write` and `futures::AsyncWrite::poll_write` allow a
zero-length input and specify `Ok(0)` as the result.

`FlexBuf::put` returns 0 both when the buffer is already frozen and when
the input slice is empty. The loop in `poll_write` assumed only the first
case, so on an empty input it called `get()` on a buffer that was never
frozen and hit the `expect`. `BufferSink` already guards this case, so
add the same guard to the adapter.

The blocking `StdWriter` path is fixed transitively, since it forwards to
`FuturesAsyncWriter::write`.

Closes apache#8161
@bavardage
bavardage force-pushed the fix-futures-async-writer-empty-write branch from 1eccbfa to e424347 Compare August 26, 2026 15:51
@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Aug 26, 2026
@Xuanwo
Xuanwo merged commit 10eeb93 into apache:main Aug 26, 2026
135 of 136 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: FuturesAsyncWriter panics on zero-length write

2 participants