ruby / ruby/openssl

Consistenty - `IOError` vs `EBADF`?

Open
#798 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
276
Forks
200
Avg merge
1d 19h
Merged PRs (30d)
7

Description

SSLSocket error conditions are not consistent with IO.

  1. io.close followed by io.read can result in EBADF rather than IOError.
  2. Without sync_close, io.close followed by io.read will hang. Even if the underlying IO is not closed, I don't think the SSLSocket instance should continue to work after being closed?

Reproduction:

#!/usr/bin/env ruby

require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'
  gem 'localhost'
end

require 'socket'
require 'openssl'
require 'localhost'

def read_after_close(io)
  io.close
  io.read
end

begin
  client, server = Socket.pair(:UNIX, :STREAM)
  read_after_close(client)
rescue => error
  $stderr.puts error.full_message
end

begin
  authority = Localhost::Authority.fetch
  
  client, server = Socket.pair(:UNIX, :STREAM)
  
  ssl_server = OpenSSL::SSL::SSLSocket.new(server, authority.server_context)
  ssl_server.sync_close = true
  
  ssl_client = OpenSSL::SSL::SSLSocket.new(client, authority.client_context)
  ssl_client.sync_close = true # If this is not set, `io.read` above will hang which is also a bit odd.
  
  Thread.new{ssl_server.accept}
  
  ssl_client.connect
  
  read_after_close(ssl_client)
rescue => error
  $stderr.puts error.full_message
end

Is this something we can improve?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied Ruby reproduction using Socket.pair and OpenSSL::SSL::SSLSocket, comparing behavior with ordinary IO after close. Investigate both sync_close settings and define done as close-then-read producing consistent error behavior without hanging.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
networking, security
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.