From: Michael Neumann Date: 2002-01-25T05:26:08+09:00 Subject: Re: filedescriptors not returned to the system Joseph McDonald wrote: > > Hi, > > The following code eats up file descriptors like crazy. The .05 > timeout is there just to show the problem. Does anyone know how to > fix this so that filedescriptors don't get all used up? I would have > thought the s.close would have solved the problem, but it doesn't. > > #!/usr/local/bin/ruby > > require 'timeout' > require 'socket' > > s = "" > while true > > begin > timeout(.05) do > s = TCPSocket.new("www.cnn.com", 80) > puts s.type > end > rescue > $stderr.puts "failed #{$!}... retrying" > s.close if s.type == TCPSocket > # system("pstat -T") > retry > end > end s.close is only called in case of an error. You should put the s.close into the ensure section instead of rescue. begin s = TCPSocket.new... rescue .... ensure s.close if s end Alternatively (bad solution), wait for the garbage collector to clean up (and close) the unreferenced sockets. Regards, Michael -- Michael Neumann merlin.zwo InfoDesign GmbH http://www.merlin-zwo.de