From: Matthew Bloch Date: 2002-03-03T12:08:54+09:00 Subject: UNIX domain sockets madness Hi there; I've been fiddling with the same piece of code for quite a while and wondered if my problem will ring a bell with somebody: I'm trying to use UNIXServer and UNIXSocket together to create a program for filtering output from compile runs. The idea is that error messages from a make job get put into a separate 'throwback' window by aliasing gcc in the shell to 'throwback.rb gcc ...' and the script will launch gcc, redirect its stderr and list anything that looks like an error in a separate window. It's something I miss from RISC OS, bear with me o main process 1 | | | +- - -o 'background' window task (forked & broken off as a daemon) | ^ ^ | | | | | | UNIX domain socket \-----o command task: gcc / jikes etc. ---/ | connections | | o main process 2 | | | | | | | \-----o command task: gcc / jikes etc. ------/ So your first run of the throwback.rb script forks off a background process if, since it can't find a socket corresponding to the X DISPLAY that it's launched from, and subsequent runs connect to the same socket so all the errors appear in the same window. My problem is that the background window code seems to block indefinitely the first time you try to read anything from it after accepting a connection: @server = UNIXServer.new("/tmp/throwback-socket-:0.0") ... @thread = Thread.new() { begin print "Starting accept\n" timeout(5) { client = @server.accept } rescue TimeoutError print "Timeout on accept, exiting\n" @server.close Thread.exit end # Test code print "about to get eof\n" print "eof = #{client.eof?}\n" # Freezes here, as does .read print "got eof\n" client.each_line { |line| .... } } whereas the 'client' code executes fine every time: @sendto = UNIXSocket.open("/tmp/throwback-socket-:0.0") ... # fork task etc. @thread = Thread.new() { $stdin.each_line { |l| $stderr.puts "Sending #{l}" @sendto.puts l } @sendto.close i.e. if I put debug lines in, I see the connections made successfully, every line output and then it closes okay too. So is there something I need to do to this pipe at one end or the other before I can get it to pass data without blocking? Where does that blocking behaviour come from anyway? Any tips will be gratefully received. -- Matthew