Feature #7918
closedCreate Signal.in_trap?()
Description
Currently, ruby library have no way to detect a method is called from trap handler or not.
This is useful because Mutex#lock under trap raises an exception and some libraries may want to avoid it.
Then, I would like to create Signal.in_trap?() class method.
Signal.in_trap?(signal = nil)
return true when running trap handler.
return false otherwise.
When signal argument is specified, return true only when running trap of specified signal.
Thought?
Updated by nagachika (Tomoyuki Chikanaga) over 11 years ago
Hello,
This feature looks reasonable for me.
I'd like to clarify the specification. Should the following code show true? or false?
Signal.trap(:USR1) do
Process.kill(:USR2, $$)
end
Signal.trap(:USR2) do
p Process.in_trap?(:USR2) # => true
p Process.in_trap?(:USR1) # => true or false?
end
Process.kill(:USR1, $$)
sleep
Updated by ko1 (Koichi Sasada) over 11 years ago
(2013/02/23 11:31), kosaki (Motohiro KOSAKI) wrote:
Currently, ruby library have no way to detect a method is called from trap handler or not.
This is useful because Mutex#lock under trap raises an exception and some libraries may want to avoid it.
I'm not sure why it is useful. If a lock is needed, then the lock should
be acquired in trap handler (and not supported yet). Detecting it is in
trap, and do what?
--
// SASADA Koichi at atdot dot net
Updated by normalperson (Eric Wong) over 11 years ago
SASADA Koichi ko1@atdot.net wrote:
(2013/02/23 11:31), kosaki (Motohiro KOSAKI) wrote:
Currently, ruby library have no way to detect a method is called from trap handler or not.
This is useful because Mutex#lock under trap raises an exception and some libraries may want to avoid it.I'm not sure why it is useful. If a lock is needed, then the lock should
be acquired in trap handler (and not supported yet). Detecting it is in
trap, and do what?
How about allowing Mutex#lock to be recursive if (and only if) called
inside trap?
Updated by kosaki (Motohiro KOSAKI) over 11 years ago
I'd like to clarify the specification. Should the following code show true? or false?
Signal.trap(:USR1) do
Process.kill(:USR2, $$)
end
Signal.trap(:USR2) do
p Process.in_trap?(:USR2) # => true
p Process.in_trap?(:USR1) # => true or false?
end
Process.kill(:USR1, $$)
sleep
false.
Because trap handler never be nested. I changed it at 2.0 for
preventing stack overflow issue.
No confusion.
Updated by kosaki (Motohiro KOSAKI) over 11 years ago
On Sun, Feb 24, 2013 at 1:15 PM, SASADA Koichi ko1@atdot.net wrote:
(2013/02/23 11:31), kosaki (Motohiro KOSAKI) wrote:
Currently, ruby library have no way to detect a method is called from trap handler or not.
This is useful because Mutex#lock under trap raises an exception and some libraries may want to avoid it.I'm not sure why it is useful. If a lock is needed, then the lock should
be acquired in trap handler (and not supported yet). Detecting it is in
trap, and do what?
The most obvious usecase is to improve better error handling and
better error messages.
And yes, we need several further steps. e.g. masking trap.
Updated by kosaki (Motohiro KOSAKI) over 11 years ago
I'm not sure why it is useful. If a lock is needed, then the lock should
be acquired in trap handler (and not supported yet). Detecting it is in
trap, and do what?How about allowing Mutex#lock to be recursive if (and only if) called
inside trap?
I disagree. Recursive or not recursive depend on protected code and data
structure. Some case is safe and some another case is unsafe for recursive.
Then instead, I suggest to make RecursiveMutex class into stdlib. and
making a chance
rubyist choose a right mutex.
Updated by kosaki (Motohiro KOSAKI) about 11 years ago
- Status changed from Open to Assigned
- Assignee set to kosaki (Motohiro KOSAKI)
Updated by hsbt (Hiroshi SHIBATA) almost 11 years ago
- Target version changed from 2.1.0 to 2.2.0
Updated by akr (Akira Tanaka) almost 11 years ago
- Status changed from Assigned to Rejected
Queue is a better solution now.
So we reject this issue.