From d4d5f87b9a6af040b7f43495032302d6bc1aa6c8 Mon Sep 17 00:00:00 2001 From: kojix2 <2xijok@gmail.com> Date: Sat, 11 Jan 2025 20:41:14 +0900 Subject: [PATCH] Add system-level at_exit handlers for safe resource cleanup --- src/crystal/at_exit_handlers.cr | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/crystal/at_exit_handlers.cr b/src/crystal/at_exit_handlers.cr index 7cd7b8cbc244..8ad113c564da 100644 --- a/src/crystal/at_exit_handlers.cr +++ b/src/crystal/at_exit_handlers.cr @@ -1,11 +1,16 @@ # :nodoc: module Crystal::AtExitHandlers private class_getter(handlers) { [] of Int32, ::Exception? -> } + private class_getter(internal_handlers) { [] of Int32, ::Exception? -> } def self.add(handler) handlers << handler end + def self.__crystal_add(handler) + internal_handlers << handler + end + def self.run(status, exception = nil) if handlers = @@handlers # Run the registered handlers in reverse order @@ -19,6 +24,18 @@ module Crystal::AtExitHandlers end end + if internal_handlers = @@internal_handlers + # Run the registered internal handlers in reverse order + while handler = internal_handlers.pop? + begin + handler.call status, exception + rescue handler_ex + Crystal::System.print_error "Error running internal at_exit handler: %s\n", handler_ex.message || "" + status = 1 if status.zero? + end + end + end + status end end