Skip to content

THRIFT-6562: Report EOF from Ruby BufferedTransport when the wrapped read ends - #4197

Merged
kpumuk merged 1 commit into
apache:masterfrom
kpumuk:rb-buffered-transport-eof
Nov 18, 2026
Merged

kpumuk merged 1 commit into
apache:masterfrom
kpumuk:rb-buffered-transport-eof

Conversation

@kpumuk

@kpumuk kpumuk commented Nov 17, 2026

Copy link
Copy Markdown
Member

Thrift::BufferedTransport refills its read buffer from the wrapped transport and then slices or indexes the result without checking it. IOStreamTransport#read delegates to Ruby IO#read, which returns nil at the end of input, so read_all, read_byte and read_into_buffer raised NoMethodError there. A bare IOStreamTransport already raises Thrift::TransportException (END_OF_FILE) in the same situation. When the wrapped transport returned an empty string at the end of input instead, read_byte returned nil and read_into_buffer raised TypeError.

With this change:

  • read treats a nil refill as an empty buffer and returns an empty string, so read_all raises END_OF_FILE. Partial reads work as before.
  • read_byte and read_into_buffer raise END_OF_FILE when a refill is nil or empty, instead of reading past the buffer.

Reproduction, run from lib/rb with bundle exec ruby -Ilib -Iext repro.rb:

require "stringio"
require "thrift"

eof = -> { Thrift::IOStreamTransport.new(StringIO.new("".b), StringIO.new) }
{
  "read_all(4)" => ->(t) { t.read_all(4) },
  "read_byte" => ->(t) { t.read_byte },
  "read_into_buffer" => ->(t) { t.read_into_buffer("\0".b * 4, 4) },
}.each do |name, call|
  begin
    p call.call(Thrift::BufferedTransport.new(eof.call))
  rescue StandardError => e
    puts "BufferedTransport #{name}: #{e.class}: #{e.message}"
  end
end

Before, on master 50242d7ea1:

BufferedTransport read_all(4): NoMethodError: undefined method 'slice' for nil
BufferedTransport read_byte: NoMethodError: undefined method 'getbyte' for nil
BufferedTransport read_into_buffer: NoMethodError: undefined method 'getbyte' for nil

After:

BufferedTransport read_all(4): Thrift::TransportException: No more data available
BufferedTransport read_byte: Thrift::TransportException: No more data available
BufferedTransport read_into_buffer: Thrift::TransportException: No more data available

The new specs in spec/buffered_transport_spec.rb cover each method over IOStreamTransport and over a transport that returns an empty string at the end of input. The behavior is the same with and without the native extension.

  • Did you create an Apache Jira ticket? (THRIFT-6562)
  • If a ticket exists: Does your pull request title follow the pattern "THRIFT-NNNN: describe my issue"?
  • Did you squash your changes to a single commit? (not required, but preferred)
  • Did you do your best to avoid breaking changes? If one was needed, did you label the Jira ticket with "Breaking-Change"?
  • CI required for this change.

…read ends

Client: rb

BufferedTransport stored the wrapped transport's read result as its buffer
without checking it. IOStreamTransport returns nil at the end of its input,
as IO#read does, so read_all, read_byte and read_into_buffer raised
NoMethodError there instead of a TransportException. An empty string from
the wrapped transport made read_byte return nil and read_into_buffer raise
TypeError.

A nil refill now becomes an empty buffer, so read returns an empty string
and read_all reports END_OF_FILE. read_byte and read_into_buffer raise
END_OF_FILE when a refill is empty instead of reading past the buffer.

Generated-By: amp/0.0.1794916837-g564d50 (model: anthropic/claude-opus-5-5; operator: kpumuk@apache.org)
Copilot AI balanced review requested due to automatic review settings November 17, 2026 23:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@mergeable mergeable Bot added the ruby Pull requests that update Ruby code label Nov 17, 2026
@kpumuk
kpumuk merged commit 1a9051f into apache:master Nov 18, 2026
93 checks passed
@kpumuk
kpumuk deleted the rb-buffered-transport-eof branch November 18, 2026 02:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ruby Pull requests that update Ruby code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants