# Multithread TCPServer not dequeueing packets

**URL:** <https://www.ruby-forum.com/t/multithread-tcpserver-not-dequeueing-packets/180220>\
**Category:** Ruby\
**Created:** [December 15, 2009, 2:31am UTC](https://www.ruby-forum.com/t/multithread-tcpserver-not-dequeueing-packets/180220 "2009-12-15T02:31:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidjrice](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5e9695/32.png) [@davidjrice](https://www.ruby-forum.com/u/davidjrice)\
**Post date:** [December 15, 2009, 2:31am UTC](https://www.ruby-forum.com/t/multithread-tcpserver-not-dequeueing-packets/180220/1 "2009-12-15T02:31:35Z")

</div>

Hi All

I am moving a software from ruby 1.8.5 to 1.9.1. We have a multi thread  
TCP server and the code looks like:  
@csock = TCPServer.new(@host, port)  
@server\_thread\_id = Thread.new { server() }  
def server()  
while (session\_sock = @csock.accept)  
worker\_thread = Thread.new {  
handle\_connection(session\_sock)}  
end  
end

But when I moved to 1.9.1, I downloaded 1.9.1p376, it accepted all the  
connections but sometimes some of then, the thread did not dequeue any  
packets after all. So I google and I found a bug  
[http://redmine.ruby-lang.org/issues/show/1525](http://redmine.ruby-lang.org/issues/show/1525)

which seems related.So I applied the patch suggested on that thread (I  
attached the patch) and everything worked just fine after that. My  
question is: should I code the server in a different way to make it  
multithread safe using ruby 1.9.1?

Thanks,

David R.

---

<div class="post-metadata">

**Author:** ![davidjrice](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5e9695/32.png) [@davidjrice](https://www.ruby-forum.com/u/davidjrice)\
**Post date:** [December 15, 2009, 11:11am UTC](https://www.ruby-forum.com/t/multithread-tcpserver-not-dequeueing-packets/180220/2 "2009-12-15T11:11:50Z")

</div>

2009/12/15 David R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ```
> end
> 
> ```
> 
> multithread safe using ruby 1.9.1?  
> Apart from that it looks a bit more complicated than necessary and  
> that you do not keep track of threads the code looks fine. Oh wait,  
> you may have an issue with scope of variables. You better pass the  
> client socket through Thread.new to be sure, e.g.

while (session\_sock = @csock.accept)  
worker\_thread = Thread.new session\_sock do |cl|  
handle\_connection(cl)  
end  
end

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![davidjrice](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5e9695/32.png) [@davidjrice](https://www.ruby-forum.com/u/davidjrice)\
**Post date:** [December 15, 2009, 11:14am UTC](https://www.ruby-forum.com/t/multithread-tcpserver-not-dequeueing-packets/180220/3 "2009-12-15T11:14:46Z")

</div>

2009/12/15 David R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> But when I moved to 1.9.1, I downloaded 1.9.1p376, it accepted all the  
> connections but sometimes some of then, the thread did not dequeue any  
> packets after all. So I google and I found a bug  
> [http://redmine.ruby-lang.org/issues/show/1525](http://redmine.ruby-lang.org/issues/show/1525)

PS: That situation is different because you do not create a child  
process - at least that’s not visible from your example code.

Cheers

robert

---

<div class="post-metadata">

**Author:** ![davidjrice](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5e9695/32.png) [@davidjrice](https://www.ruby-forum.com/u/davidjrice)\
**Post date:** [December 15, 2009, 2:18pm UTC](https://www.ruby-forum.com/t/multithread-tcpserver-not-dequeueing-packets/180220/4 "2009-12-15T14:18:17Z")

</div>

Thanks Brian and Robert

IT looks maybe the session\_sock is the main problem. I will try testing  
that. Besides that what is kind of weird is why the patch did make  
things work.  
I’ll keep you posted

Thanks

David

---

<div class="post-metadata">

**Author:** ![davidjrice](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5e9695/32.png) [@davidjrice](https://www.ruby-forum.com/u/davidjrice)\
**Post date:** [December 15, 2009, 11:18am UTC](https://www.ruby-forum.com/t/multithread-tcpserver-not-dequeueing-packets/180220/5 "2009-12-15T11:18:56Z")

</div>

David R. wrote:

> ```
> while (session_sock = @csock.accept)
> worker_thread = Thread.new {
> 
> ```
> 
> handle\_connection(session\_sock)}  
> end

That code isn’t thread-safe, because the outer loop could change  
session\_sock before the worker thread has used it.

while session\_sock = @csock.accept  
Thread.new(session\_sock) do |conn|  
handle\_connection(conn)  
end  
end

---

<div class="post-metadata">

**Author:** ![davidjrice](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5e9695/32.png) [@davidjrice](https://www.ruby-forum.com/u/davidjrice)\
**Post date:** [December 15, 2009, 2:32pm UTC](https://www.ruby-forum.com/t/multithread-tcpserver-not-dequeueing-packets/180220/6 "2009-12-15T14:32:35Z")

</div>

2009/12/15 David R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Thanks Brian and Robert
> 
> IT looks maybe the session\_sock is the main problem. I will try testing  
> that. Besides that what is kind of weird is why the patch did make  
> things work.

That may just be a side effect of slightly changed timing.

> I’ll keep you posted

Thanks, David!

> Thanks

You’re welcome!

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![davidjrice](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5e9695/32.png) [@davidjrice](https://www.ruby-forum.com/u/davidjrice)\
**Post date:** [December 15, 2009, 9:20pm UTC](https://www.ruby-forum.com/t/multithread-tcpserver-not-dequeueing-packets/180220/7 "2009-12-15T21:20:59Z")

</div>

Hi Robert and Brian

Yes. It is confirmed. The problem was that session\_sock getting changed  
once the threads run. I assume, it worked okay on ruby 1.8 as it has  
only green threads and timing was okay. But once I moved to 1.9 the  
timing was totally changed and the code started to show my bug.

Thanks for the help,

David
