Multithread TCPServer not dequeueing packets

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

···

--
Posted via http://www.ruby-forum.com/.

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

···

2009/12/15 David Rodriguez <david.francisco.rodriguez@gmail.com>:

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?

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

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

···

2009/12/15 David Rodriguez <david.francisco.rodriguez@gmail.com>:

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

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

David Rodriguez 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

···

--
Posted via http://www.ruby-forum.com/\.

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

···

--
Posted via http://www.ruby-forum.com/.

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

···

2009/12/15 David Rodriguez <david.francisco.rodriguez@gmail.com>:

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

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

···

--
Posted via http://www.ruby-forum.com/.