Bug #7917
closedCan't write to a Logger in a signal handler
Description
Looks like Ruby 2.0 does not allow Mutex#lock within a signal handler. This prevents Logger from working since it uses an underlying mutex.
log writing failed. can't be called from trap context
log writing failed. can't be called from trap context
log writing failed. can't be called from trap context
log writing failed. can't be called from trap context
Here's Ruby code which reproduces the problem:
require 'logger'
LOG = Logger.new(STDOUT)
LOG.info "Now logging!"
trap 'INT' do
LOG.info "Hello"
end
sleep
LOG.info "Done"
Run it and hit Ctrl-C.
Updated by kosaki (Motohiro KOSAKI) almost 12 years ago
Because your example is deadlockable when using 1.9.3 or earlier. 2.0 detect your mistake and tell you.
Updated by mame (Yusuke Endoh) almost 12 years ago
- Status changed from Open to Assigned
- Assignee set to kosaki (Motohiro KOSAKI)
- Target version set to 2.1.0
This issue is arguable, but anyway I don't consider it a showstopper.
I postpone it to 2.1.0.
It is very arguable if the fault is in the code, or in the design of Ruby core.
--
Yusuke Endoh mame@tsg.ne.jp
Updated by normalperson (Eric Wong) almost 12 years ago
"mperham (Mike Perham)" mperham@gmail.com wrote:
trap 'INT' do
LOG.info "Hello"
end
An ugly workaround I use is to spawn a thread inside trap:
trap 'INT' do
Thread.new do
LOG.info "Hello"
end
end
Updated by kosaki (Motohiro KOSAKI) almost 12 years ago
On Sat, Feb 23, 2013 at 3:06 AM, Eric Wong normalperson@yhbt.net wrote:
"mperham (Mike Perham)" mperham@gmail.com wrote:
trap 'INT' do
LOG.info "Hello"
endAn ugly workaround I use is to spawn a thread inside trap:
trap 'INT' do
Thread.new do
LOG.info "Hello"
end
end
Um. this is safe.
btw, following is unsafe when 1.9.x. because trap safe th.join is a
new feature of 2.0.
trap 'INT' do
t = Thread.new do
LOG.info "Hello"
end
t.join
end
see http://bugs.ruby-lang.org/issues/6416
Hm.
Should we backport this fix to 1.9.3 too?
Updated by kosaki (Motohiro KOSAKI) almost 12 years ago
trap 'INT' do
t = Thread.new do
LOG.info "Hello"
end
t.join
end
Oops, no. This code is completely wrong. Sorry for confusing.
Please don't mind my previous mail. This code is deadlockable and
should never be used.
Updated by kosaki (Motohiro KOSAKI) almost 12 years ago
- Status changed from Assigned to Rejected
Closed. because it's not a bug.
Updated by hkmaly (Honza Maly) almost 7 years ago
What IS the recommended way to fix this if Mutex nor Thread are safe in signal handler? Also created question for what is safe in signal handler in general as #14222
Updated by normalperson (Eric Wong) almost 7 years ago
hkmaly@matfyz.cz wrote:
What IS the recommended way to fix this if Mutex nor Thread
are safe in signal handler? Also created question for what is
safe in signal handler in general as #14222
See my response for [ruby-core:84410] [Misc #14222] :>
https://bugs.ruby-lang.org/issues/14222