From: Bill Kelly Date: 2006-03-29T05:48:14+09:00 Subject: Re: timeout when listening with TCPServer From: "Joel VanderWerf" > Bill Kelly wrote: >> From: "Eric Hodel" >>> >>> On Mar 28, 2006, at 9:48 AM, Shea Martin wrote: >>> >>>> I want to listen for connections for 2 seconds, then timeout. Do I >>>> have to use the Timeout module? >>> >>> That will be the easiest way. >>> >>> Timeout.timeout 2 do >>> Thread.start server.accept do |s| new_connection s end >>> end >> >> Oh, hey, cool. Using the thread there looks like it >> ought to avoid the issue with Timeout firing in the >> middle of ensure blocks and circumventing them? >> >> I've been avoiding Timeout like "the plague" since >> running into that behavior. > > But the timeout won't stop the thread. I don't think it was supposed to. The #accept is happening as an argument to Thread.start, so the thread won't be started until the connection is accepted. The only teeny race condition I can see is if the timeout fires after server.accept but before Thread.start is invoked. That would seem to leak the socket - but the GC would normally reclaim it eventually (except in cases where it wouldn't. :) Regards, Bill