Skip to content

Signal#trap should return previous handler #13803

@straight-shoota

Description

@straight-shoota

When you trap a signal, you replace any previous definition with your own, overriding any default behavior.

This is an additional problem.

In Ruby-land Signal.trap returns the previous handler,
so you can safely chain them.

Crystal currently has no equivalent to this.

This is problematic when for example a shard needs
to add a signal-handler. As of today it has no way to
preserve (or even detect) an already attached handler.

I'd like to propose the following syntax to enable signal handler chaining:

Signal::INT.trap do
  puts "Trap 1"
  exit
end

Signal::INT.trap do |previous_handler|
  puts "Trap 2"
  previous_handler.call
end

Output on signal:

Trap 2
Trap 1

Originally posted by @m-o-e in #8687 (comment)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions