Skip to content

THRIFT-6117: Reject duplicate opens in Ruby SSL and UNIX sockets - #4195

Merged
kpumuk merged 1 commit into
apache:masterfrom
kpumuk:rb-duplicate-socket-open
Nov 18, 2026
Merged

kpumuk merged 1 commit into
apache:masterfrom
kpumuk:rb-duplicate-socket-open

Conversation

@kpumuk

@kpumuk kpumuk commented Nov 17, 2026

Copy link
Copy Markdown
Member

Thrift::SSLSocket and Thrift::UNIXSocket override Socket#open without the already-open check added in THRIFT-6112. A second open on either transport connected again and replaced the handle, leaving the first connection open but unreachable; a later close closed only the replacement. Both now raise TransportException::ALREADY_OPEN before connecting, as Thrift::Socket does, and keep the existing connection. In UNIXSocket the check sits outside the rescue that reports connect errors as NOT_OPEN.

A shared example now opens a real TCP, SSL and UNIX-domain connection, opens it again, and checks that the second open is rejected, the handle is unchanged, the server sees no new connection, and close ends the original one. The UNIX and SSL cases fail on master and pass with this change; the existing TCP-only example moved into the shared example.

Reproduction

require "socket"
require "tmpdir"
require "thrift"

path = File.join(Dir.mktmpdir, "thrift.sock")
server = UNIXServer.new(path)
transport = Thrift::UNIXSocket.new(path)

transport.open
first_handle = transport.handle
first_peer = server.accept

begin
  transport.open
  puts "second open: no error"
rescue Thrift::TransportException => e
  puts "second open: #{e.message} (type #{e.type})"
end

puts "same handle: #{transport.handle.equal?(first_handle)}"
puts "server has another connection: #{!IO.select([server], nil, nil, 0).nil?}"
transport.close
puts "first handle closed after close: #{first_handle.closed?}"

ruby -I lib/rb/lib repro.rb with Ruby 4.0.9 on master (50242d7):

second open: no error
same handle: false
server has another connection: true
first handle closed after close: false

With this change:

second open: Socket already open (type 2)
same handle: true
server has another connection: false
first handle closed after close: true
  • Did you create an Apache Jira ticket? (THRIFT-6117)
  • 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.

Client: rb

Thrift::SSLSocket and Thrift::UNIXSocket override Socket#open without
its already-open check, so a second open connected again and replaced
the handle. The first connection stayed open but unreachable, and a
later close closed only the replacement.

Both now raise TransportException::ALREADY_OPEN before connecting, as
Socket#open does, and keep the existing connection. In UNIXSocket the
check sits outside the rescue that turns connect errors into NOT_OPEN.

A shared example opens a real TCP, SSL and UNIX connection, opens it
again, and checks that the second open is rejected, that the server sees
no new connection, and that close ends the original one.

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:34

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 87398cd into apache:master Nov 18, 2026
93 checks passed
@kpumuk
kpumuk deleted the rb-duplicate-socket-open branch November 18, 2026 02:05
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