signal problems on win32

S

Scott McCaskill

I have a simple program that reads from a socket, compresses the data
and writes it to a file. Because I'm using zlib, I must intercept
SIGINT so I can close the GzipWriter before exiting or the file will
be corrupted. Problem is, sometimes my handler gets called, and
sometimes it doesn't. When it doesn't, the program just hangs and I
have to kill the process from task manager.

This is with ruby 1.8.2 (2004-07-29) [i386-mswin32] on Windows XP. If
I perform some additional manipulation of the data before compressing
it (as shown in the commented lines in the loop), the problem occurs
much more frequently.

I really want to use ruby for this task, but this problem is a
showstopper. Are there any workarounds I might try?


require 'socket'
require 'zlib'

sock = UDPSocket.open
sock.bind('', 6161)

f = File.new('junk.gz', 'wb')
outFile = Zlib::GzipWriter.new(f, Zlib::BEST_COMPRESSION)

trap('INT') { puts 'exiting'; outFile.close; exit 0 }

interval = 5
channelId = 1

loop {
packet = sock.recv(8192)
outFile << packet
#outFile << [sprintf('%08i', interval), channelId, sprintf('%05i',
#packet.size), packet].pack('a8 c a5 a*') << "\n"
puts "got #{packet.size} bytes"
}
 
R

rcoder

I've encountered this as well, and had just resorted to using
mechanisms other than SIGINT to terminate the program. However, there's
a workaround suggested in the ruby-talk archives:

http://blade.nagaokaut.ac.jp/ruby/ruby-talk/82924

Basically, it amounts to putting a busy-wait loop in a background
thread; it seems that adding the thread changes Ruby's signal handling
and allows the SIGINT to get delivered correctly.

Anyone have a more satisfactory explanation for the source of the
problem?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,420
Latest member
ZitaVos505

Latest Threads

Top