WEBrick catching keyboard interrupts

Hi all,

I’m knocking up a few test scripts using WEBrick (on Ubuntu), and I’m
constantly annoyed by the fact that hitting Ctrl-C doesn’t kill it, but
makes it print the following to the console:

[2006-10-05 09:43:48] ERROR Interrupt:
/usr/lib/ruby/1.8/webrick/server.rb:91:in `select’

I have to go to another console (or suspend it) and kill -9 the process.
How can I stop this from happening? The code is very, very simple:

require ‘xmlrpc/server’

class Server < XMLRPC::Server
def initialize
super(8080, ‘0.0.0.0’, 1)
self.add_handler(‘test.unit’){ 1 }
self.serve
end
end

Server.new

I’m sure there’s a simple answer, but it eludes me…

Alex Y. wrote:

end
end

Server.new

Signal.trap(2) {
“Server killed (sigint)”
exit(1)
}

Regards,
Jordan

MonkeeSage wrote:

 self.serve

end
end

Server.new

Signal.trap(2) {
“Server killed (sigint)”
exit(1)
}

That gives me:

[2006-10-05 12:33:30] ERROR SystemExit: exit
test_server.rb:5:in `exit’

and a still-running process.

Alex Y. wrote:

 self.add_handler('test.unit'){ 1 }

That gives me:

[2006-10-05 12:33:30] ERROR SystemExit: exit
test_server.rb:5:in `exit’

and a still-running process.

Sorry, should have said: That gives me the above error on Ctrl-C, and
repeats it for every time I send the interrupt, without ending the
process. Just thought I’d clarify that…

MonkeeSage wrote:

I haven’t used webrick much but it looks like it uses an exit hook.
Apparently you use the #shutdown method:

Or better:

require ‘xmlrpc/server’
class Server < XMLRPC::Server
def initialize
Signal.trap(2) {
puts “Server killed (sigint)”
self.shutdown
}
super(8080, ‘0.0.0.0’, 1)
self.add_handler(‘test.unit’){ 1 }
self.serve
end
end
Server.new

MonkeeSage wrote:

  puts "Server killed (sigint)"
  self.shutdown
}
super(8080, '0.0.0.0', 1)
self.add_handler('test.unit'){ 1 }
self.serve

end
end
Server.new

Oh, of course… Thanks :slight_smile:

Alex Y. wrote:

Sorry, should have said: That gives me the above error on Ctrl-C, and
repeats it for every time I send the interrupt, without ending the
process. Just thought I’d clarify that…

I haven’t used webrick much but it looks like it uses an exit hook.
Apparently you use the #shutdown method:

require ‘xmlrpc/server’
class Server < XMLRPC::Server
def initialize
super(8080, ‘0.0.0.0’, 1)
self.add_handler(‘test.unit’){ 1 }
self
end
def start
self.serve
end
end
server = Server.new
Signal.trap(2) {
puts “Server killed (sigint)”
server.shutdown
}
server.start

Regards,
Jordan