diff --git a/src/crystal/system/thread.cr b/src/crystal/system/thread.cr index b40a7dceb32b..e134edb3ec1f 100644 --- a/src/crystal/system/thread.cr +++ b/src/crystal/system/thread.cr @@ -54,7 +54,9 @@ class Thread getter name : String? def self.unsafe_each(&) - threads.unsafe_each { |thread| yield thread } + # nothing to iterate when @@threads is nil + don't lazily allocate in a + # method called from a GC collection callback! + @@threads.try(&.unsafe_each { |thread| yield thread }) end # Creates and starts a new system thread. diff --git a/src/fiber.cr b/src/fiber.cr index 049b7e9bb0c0..e8353d20e701 100644 --- a/src/fiber.cr +++ b/src/fiber.cr @@ -73,7 +73,9 @@ class Fiber # :nodoc: def self.unsafe_each(&) - fibers.unsafe_each { |fiber| yield fiber } + # nothing to iterate when @@fibers is nil + don't lazily allocate in a + # method called from a GC collection callback! + @@fibers.try(&.unsafe_each { |fiber| yield fiber }) end # Creates a new `Fiber` instance.