Project

General

Profile

Backport #4132

Socket.close attempting to close the socket twice

Added by devmod (Claudio Villalobos) almost 9 years ago. Updated 3 months ago.

Status:
Closed
Priority:
Normal
Assignee:
-
[ruby-core:33628]

Description

=begin
Socket.close calls shutdown twice on the same file descriptor

## socktest.rb ##
require 'socket'
include Socket::Constants
socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
socket.bind( sockaddr )
socket.listen( 5 )
puts "socket.to_i: #{socket.to_i}"
socket.close
#############

$ strace -e trace=open,close ruby1.8 socktest.rb
...
socket.to_i: 3
close(3) = 0
close(3) = -1 EBADF (Bad file descriptor)


socket.c
-
socks_s_close(sock)
...
1386 shutdown(fileno(fptr->f), 2);
1387 shutdown(fileno(fptr->f2), 2);
=end

History

#1

Updated by matz (Yukihiro Matsumoto) almost 9 years ago

=begin
Hi,

In message "Re: [ruby-core:33628] [Ruby 1.8-Bug#4132][Open] Socket.close attempting to close the socket twice"
on Wed, 8 Dec 2010 07:57:58 +0900, Claudio Villalobos redmine@ruby-lang.org writes:

|Bug #4132: Socket.close attempting to close the socket twice
|http://redmine.ruby-lang.org/issues/show/4132
|
|Author: Claudio Villalobos
|Status: Open, Priority: Normal
|Category: ext
|ruby -v: ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]
|
|Socket.close calls shutdown twice on the same file descriptor

Ruby 1.8 creates two FILE* streams on a socket to implement
bidirectional communication. Two calls to close(2) is a side effect.
Since it shouldn't do any harm, we don't consider it a bug. Tell us
if it would cause any trouble (other than one useless system call, and
a failure in strace log).

                        matz.

=end

#2

Updated by devmod (Claudio Villalobos) almost 9 years ago

=begin
Hi,

Since file descriptors are reused after closing, wouldn't there be an edge case where that file descriptor could have been assigned to another socket before the second close had been executed?

=end

#3

Updated by matz (Yukihiro Matsumoto) almost 9 years ago

=begin
Hi,

In message "Re: [ruby-core:33630] [Ruby 1.8-Bug#4132] Socket.close attempting to close the socket twice"
on Wed, 8 Dec 2010 10:18:22 +0900, Claudio Villalobos redmine@ruby-lang.org writes:
|
|Since file descriptors are reused after closing, wouldn't there be an edge case where that file descriptor could have been assigned to another socket before the second close had been executed?

On 1.8 (and mostly on 1.9 too), the execution is protected from
multithreading, so that we don't have to worry about concurrency here.

                        matz.

=end

#4

Updated by devmod (Claudio Villalobos) almost 9 years ago

=begin
Understood. Thanks
=end

#5

Updated by jeremyevans0 (Jeremy Evans) 3 months ago

  • ruby -v deleted (ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux])
  • Status changed from Open to Closed
  • Description updated (diff)
  • Project changed from Ruby 1.8 to Backport187
  • Tracker changed from Bug to Backport

Also available in: Atom PDF