From: Robert Klemme Date: 2009-12-15T19:04:18+09:00 Subject: Re: Multithread TCPServer not dequeueing packets 2009/12/15 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? 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 -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/