Project

General

Profile

Bug #7917

Can't write to a Logger in a signal handler

Added by mperham (Mike Perham) over 4 years ago. Updated over 4 years ago.

Status:
Rejected
Priority:
Normal
Target version:
[ruby-core:52722]

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.

History

#1 [ruby-core:52725] Updated by kosaki (Motohiro KOSAKI) over 4 years ago

Because your example is deadlockable when using 1.9.3 or earlier. 2.0 detect your mistake and tell you.

#2 [ruby-core:52730] Updated by mame (Yusuke Endoh) over 4 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

#3 [ruby-core:52732] Updated by normalperson (Eric Wong) over 4 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

#4 [ruby-core:52734] Updated by kosaki (Motohiro KOSAKI) over 4 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"
end

An 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?

#5 [ruby-core:52735] Updated by kosaki (Motohiro KOSAKI) over 4 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.

#6 [ruby-core:53364] Updated by kosaki (Motohiro KOSAKI) over 4 years ago

  • Status changed from Assigned to Rejected

Closed. because it's not a bug.

Also available in: Atom PDF