Fix ReadIOAdapter max_len handling#294
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7add883c98
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| rescue StopIteration | ||
| @stream = nil | ||
| @buf.slice!(0..) | ||
| @buf.empty? ? nil : @buf.slice!(0..) |
There was a problem hiding this comment.
Handle EOF when
read receives an output buffer
When an enumerator-backed body ends exactly on the IO.copy_stream buffer boundary, IO.copy_stream makes one more call as read(16384, outbuf). This new EOF branch now returns nil while still inside the Enumerator path, so read reaches out_string.replace(read) with read == nil and raises TypeError instead of letting Net::HTTP finish the upload. For example, an enumerator yielding exactly 16 KiB reproduces the failure through IO.copy_stream.
Useful? React with 👍 / 👎.
Summary
ReadIOAdapter#read_enumsoread(max_len)returns exactlymax_lenbytes instead ofmax_len + 1nilwhen an enumerator-backed adapter is exhausted for bounded reads, matchingIO#read(max_len)Why
ReadIOAdapter#read_enumcurrently uses an inclusive slice when draining its buffer. That means a bounded read can return one extra byte, which is risky in the request-body path because the adapter feedsNet::HTTP.While validating the fix, the same path also showed an EOF mismatch for bounded reads: after the final chunk, the adapter returned
""once before returningnil. Tightening that behavior keeps the adapter aligned withIO#read(max_len)semantics.Validation
ASDF_RUBY_VERSION=3.4.9 asdf exec bundle exec ruby -Itest test/openai/internal/util_test.rb --name=/read/ASDF_RUBY_VERSION=3.4.9 asdf exec bundle exec ruby -Itest test/openai/internal/util_test.rb