Skip to content

Fix ReadIOAdapter max_len handling#294

Open
fallintoplace wants to merge 2 commits into
openai:mainfrom
fallintoplace:fix/read-io-adapter-off-by-one
Open

Fix ReadIOAdapter max_len handling#294
fallintoplace wants to merge 2 commits into
openai:mainfrom
fallintoplace:fix/read-io-adapter-off-by-one

Conversation

@fallintoplace

Copy link
Copy Markdown

Summary

  • fix ReadIOAdapter#read_enum so read(max_len) returns exactly max_len bytes instead of max_len + 1
  • return nil when an enumerator-backed adapter is exhausted for bounded reads, matching IO#read(max_len)
  • add a regression that reads across chunk boundaries and verifies exact-length slices through EOF

Why

ReadIOAdapter#read_enum currently 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 feeds Net::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 returning nil. Tightening that behavior keeps the adapter aligned with IO#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

@fallintoplace fallintoplace requested a review from a team as a code owner June 26, 2026 22:30

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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..)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant