# What should TCPSocket#close do to a blocked #readline?

**URL:** <https://www.ruby-forum.com/t/what-should-tcpsocket-close-do-to-a-blocked-readline/203003>\
**Category:** Ruby\
**Created:** [February 10, 2011, 12:59pm UTC](https://www.ruby-forum.com/t/what-should-tcpsocket-close-do-to-a-blocked-readline/203003 "2011-02-10T12:59:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [February 10, 2011, 12:59pm UTC](https://www.ruby-forum.com/t/what-should-tcpsocket-close-do-to-a-blocked-readline/203003/1 "2011-02-10T12:59:23Z")

</div>

Take the following code:

require ‘socket’  
require ‘thread’

server = TCPServer.new(“localhost”, 0)  
serv\_thread = Thread.new{ server.accept }  
sleep(0.1)  
sock = TCPSocket.new(“localhost”, server.addr[1])

q = Queue.new  
client\_thread = Thread.new{  
begin  
sock.readline  
rescue StandardError =\> e  
q.push(e)  
end  
}

sleep(0.1) while client\_thread.status == “run”  
sock.close

# client\_thread.wakeup rescue nil

err = q.pop  
client\_thread.join  
fail “Failure!” unless err.is\_a?(IOError)  
puts “Success!”

So, we’ve got one thread blocked on a #readline, and another thread  
closes the socket out from under it. On 1.8.7-p302, this completes  
successfully. On 1.9.2-p136, it hangs at q.pop.

If I uncomment the client\_thread.wakeup call, both complete (although  
the rescue is necessary to prevent a ThreadError on 1.8.7).

Is this expected? Which is “correct”?

## Thanks,

Alex

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [February 10, 2011, 3:47pm UTC](https://www.ruby-forum.com/t/what-should-tcpsocket-close-do-to-a-blocked-readline/203003/2 "2011-02-10T15:47:28Z")

</div>

On Thu, Feb 10, 2011 at 12:59 PM, Alex Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> sleep(0.1) while client\_thread.status == “run”  
> successfully. On 1.9.2-p136, it hangs at q.pop.  
> I cannot confirm this:

15:40:10 Temp$ ruby19 -v  
ruby 1.9.2p136 (2010-12-25 revision 30365) [i386-cygwin]  
15:40:50 Temp$ ruby19 s.rb  
#\<IOError: closed stream\>  
Success!  
15:41:31 Temp$ cat s.rb  
require ‘socket’  
require ‘thread’

server = TCPServer.new(“localhost”, 0)  
serv\_thread = Thread.new{ server.accept }  
sleep(0.1)  
sock = TCPSocket.new(“localhost”, server.addr[1])

q = Queue.new  
client\_thread = Thread.new{  
begin  
sock.readline  
q.push “OK”  
rescue StandardError =\> e  
p e  
q.push(e)  
end  
}

sleep(0.1) while client\_thread.status == “run”  
sock.close

# client\_thread.wakeup rescue nil

err = q.pop

# client\_thread.join

fail “Failure!” unless err.is\_a?(IOError)  
puts “Success!”

15:41:36 Temp$

> If I uncomment the client\_thread.wakeup call, both complete (although  
> the rescue is necessary to prevent a ThreadError on 1.8.7).
> 
> Is this expected? Which is “correct”?

Hm… I would expect your 1.9.2 to also throw.

Btw, one thing is odd about your test case: you have two mechanisms to  
synchronize threads - you read from the queue and you join  
client\_thread. If you really only want to track status of the thread  
you could as well use Thread#value:

client\_thread = Thread.new{  
begin  
sock.readline  
nil  
rescue StandardError =\> e  
e  
end  
}

err = client\_thread.value

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [February 10, 2011, 5:10pm UTC](https://www.ruby-forum.com/t/what-should-tcpsocket-close-do-to-a-blocked-readline/203003/3 "2011-02-10T17:10:46Z")

</div>

Robert K. wrote in post #980828:

> On Thu, Feb 10, 2011 at 12:59 PM, Alex Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > sleep(0.1) while client\_thread.status == “run”  
> > successfully. On 1.9.2-p136, it hangs at q.pop.  
> > I cannot confirm this:
> 
> 15:40:10 Temp$ ruby19 -v  
> ruby 1.9.2p136 (2010-12-25 revision 30365) [i386-cygwin]

$ ruby -v  
ruby 1.9.2p136 (2010-12-25 revision 30365) [x86\_64-linux]

is mine. Might it be an architecture issue? I don’t have a 32bit machine  
handy to compare.

> 15:40:50 Temp$ ruby19 s.rb  
> #\<IOError: closed stream\>  
> Success!  
> 15:41:31 Temp$ cat s.rb  
> require ‘socket’  
> require ‘thread’
> 
> server = TCPServer.new(“localhost”, 0)  
> serv\_thread = Thread.new{ server.accept }  
> sleep(0.1)  
> sock = TCPSocket.new(“localhost”, server.addr[1])
> 
> q = Queue.new  
> client\_thread = Thread.new{  
> begin  
> sock.readline  
> q.push “OK”  
> rescue StandardError =\> e  
> p e  
> q.push(e)  
> end  
> }
> 
> sleep(0.1) while client\_thread.status == “run”  
> sock.close
> 
> # client\_thread.wakeup rescue nil
> 
> err = q.pop
> 
> # client\_thread.join
> 
> fail “Failure!” unless err.is\_a?(IOError)  
> puts “Success!”
> 
> 15:41:36 Temp$
> 
> > If I uncomment the client\_thread.wakeup call, both complete (although  
> > the rescue is necessary to prevent a ThreadError on 1.8.7).
> > 
> > Is this expected? Which is “correct”?
> 
> Hm… I would expect your 1.9.2 to also throw.

That’s what I thought, but rubinius doesn’t throw either, so I wasn’t  
sure.

> Btw, one thing is odd about your test case: you have two mechanisms to  
> synchronize threads - you read from the queue and you join  
> client\_thread.

Yes, the #join was a bit of leftover that I forgot to trim.

> If you really only want to track status of the thread  
> you could as well use Thread#value:
> 
> client\_thread = Thread.new{  
> begin  
> sock.readline  
> nil  
> rescue StandardError =\> e  
> e  
> end  
> }
> 
> err = client\_thread.value

That’s a bit neater, though. I like that.

## Thanks,

Alex

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [February 11, 2011, 1:00pm UTC](https://www.ruby-forum.com/t/what-should-tcpsocket-close-do-to-a-blocked-readline/203003/4 "2011-02-11T13:00:57Z")

</div>

Alex Y. wrote in post #980861:

> Robert K. wrote in post #980828:
> 
> > On Thu, Feb 10, 2011 at 12:59 PM, Alex Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > wrote:
> > 
> > > sleep(0.1) while client\_thread.status == “run”  
> > > successfully. On 1.9.2-p136, it hangs at q.pop.  
> > > I cannot confirm this:
> > 
> > 15:40:10 Temp$ ruby19 -v  
> > ruby 1.9.2p136 (2010-12-25 revision 30365) [i386-cygwin]
> 
> $ ruby -v  
> ruby 1.9.2p136 (2010-12-25 revision 30365) [x86\_64-linux]

Ok, so this also fails on i686-linux. Now, where’s the bug? 🙂

–  
Alex

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [February 11, 2011, 5:13pm UTC](https://www.ruby-forum.com/t/what-should-tcpsocket-close-do-to-a-blocked-readline/203003/5 "2011-02-11T17:13:48Z")

</div>

Robert K. wrote in post #981080:

> On Fri, Feb 11, 2011 at 1:01 PM, Alex Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > > > ruby 1.9.2p136 (2010-12-25 revision 30365) [i386-cygwin]
> > > 
> > > $ ruby -v  
> > > ruby 1.9.2p136 (2010-12-25 revision 30365) [x86\_64-linux]
> > 
> > Ok, so this also fails on i686-linux. Now, where’s the bug? 🙂
> 
> Maybe the network stack behaves different on these different platforms.

That doesn’t explain why 1.8.7 on Linux behaves the same as cygwin  
1.9.2, though.

–  
Alex

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [February 11, 2011, 1:15pm UTC](https://www.ruby-forum.com/t/what-should-tcpsocket-close-do-to-a-blocked-readline/203003/6 "2011-02-11T13:15:26Z")

</div>

On Fri, Feb 11, 2011 at 1:01 PM, Alex Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > ruby 1.9.2p136 (2010-12-25 revision 30365) [i386-cygwin]
> > 
> > $ ruby -v  
> > ruby 1.9.2p136 (2010-12-25 revision 30365) [x86\_64-linux]
> 
> Ok, so this also fails on i686-linux. Now, where’s the bug? 🙂

Maybe the network stack behaves different on these different platforms.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [February 12, 2011, 5:06pm UTC](https://www.ruby-forum.com/t/what-should-tcpsocket-close-do-to-a-blocked-readline/203003/7 "2011-02-12T17:06:07Z")

</div>

Alex Y. wrote in post #981138:

> Robert K. wrote in post #981080:
> 
> > On Fri, Feb 11, 2011 at 1:01 PM, Alex Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > wrote:
> > 
> > > > > ruby 1.9.2p136 (2010-12-25 revision 30365) [i386-cygwin]
> > > > 
> > > > $ ruby -v  
> > > > ruby 1.9.2p136 (2010-12-25 revision 30365) [x86\_64-linux]
> > > 
> > > Ok, so this also fails on i686-linux. Now, where’s the bug? 🙂
> > 
> > Maybe the network stack behaves different on these different platforms.
> 
> That doesn’t explain why 1.8.7 on Linux behaves the same as cygwin  
> 1.9.2, though.

Well, that was quick 🙂

[http://redmine.ruby-lang.org/issues/show/4390](http://redmine.ruby-lang.org/issues/show/4390)

–  
Alex

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [February 13, 2011, 2:00pm UTC](https://www.ruby-forum.com/t/what-should-tcpsocket-close-do-to-a-blocked-readline/203003/8 "2011-02-13T14:00:58Z")

</div>

On Sat, Feb 12, 2011 at 5:06 PM, Alex Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > > Ok, so this also fails on i686-linux. Now, where’s the bug? 🙂  
> > > > [http://redmine.ruby-lang.org/issues/show/4390](http://redmine.ruby-lang.org/issues/show/4390)  
> > > > Yep, Nobu is great! Thanks for filing the bug, Alex.

Cheers

robert
