Multithread TCPServer not dequeueing packets

D

David Rodriguez

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

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 Rodriguez

Attachments:
http://www.ruby-forum.com/attachment/4322/vm_deadlock_fix.diff
 
R

Robert Klemme

2009/12/15 David Rodriguez said:
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:
=A0 =A0 =A0 =A0@csock =3D TCPServer.new(@host, port)
=A0 =A0 =A0 =A0@server_thread_id =3D Thread.new { server() }
=A0 =A0 =A0 =A0def server()
=A0 =A0 =A0 =A0 =A0 while (session_sock =3D @csock.accept)
=A0 =A0 =A0 =A0 =A0 =A0 =A0 worker_thread =3D Thread.new {
handle_connection(session_sock)}
=A0 =A0 =A0 =A0 =A0 end
=A0 =A0 =A0 =A0end

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

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?

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 =3D @csock.accept)
worker_thread =3D Thread.new session_sock do |cl|
handle_connection(cl)
end
end

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
R

Robert Klemme

2009/12/15 David Rodriguez said:
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

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
 
B

Brian Candler

David said:
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
 
D

David Rodriguez

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
 
R

Robert Klemme

2009/12/15 David Rodriguez said:
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!

You're welcome!

Kind regards

robert
 
D

David Rodriguez

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top