Skip to content

Commit

Permalink
Fix: Don't allocate in Fiber.unsafe_each and Thread.unsafe_each (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ysbaddaden authored and straight-shoota committed May 29, 2024
1 parent 4cea101 commit ed70393
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/crystal/system/thread.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion src/fiber.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit ed70393

Please sign in to comment.