From: akr@... Date: 2014-09-08T03:36:53+00:00 Subject: [ruby-core:64832] [ruby-trunk - Feature #10211] Implement Signal.current_trap(sig) Issue #10211 has been updated by Akira Tanaka. Any problem with the return value of trap? ~~~ % ruby -e ' def trap2(sig) previous_proc = Signal.trap(sig){ previous_proc.call if previous_proc yield } end trap(:INT) { p 1 } trap2(:INT) { p 2 } Process.kill :INT, $$ ' 1 2 ~~~ ---------------------------------------- Feature #10211: Implement Signal.current_trap(sig) https://bugs.ruby-lang.org/issues/10211#change-48707 * Author: Kyrylo Silin * Status: Open * Priority: Normal * Assignee: * Category: * Target version: ---------------------------------------- Here's pseudocode by ko1: ~~~ def trap2(sig) previous_proc = Signal.current_trap(sig) Signal.trap(sig){ previous_proc.call if previous_proc yield } end trap2(:INT){...} ~~~ ## Motivation I'm developing a gem that allows using multiple callbacks for a trap: [[https://github.com/kyrylo/multitrap]] It's pretty simple (and slightly broken). The problem is that if you earlier had defined traps and then required my library, it would discard your previously defined callbacks. The library overrides `Signal.trap` and stores callbacks in a hash. However, it stores only new callbacks. I cannot access previously defined callbacks for signals. They are stored in `GET_VM()->trap_list`, which isn't exposed neither to Ruby nor to the C extension API. I know when you define a `trap`, it returns a proc. However, nobody typicaly stores it, so there's no way to access it. So if my gem loads after this assignment, I'm unable to capture that proc, hence I always overwrite previous "traps". This library might be useful if you want to define a trap that conflicts with some other gem you depend on, which defines its own trap for the same signal. So I need some way to access the callbacks. -- https://bugs.ruby-lang.org/