Skip to content

Commit 7f00f35

Browse files
committed
Break the cycle between the task and the fiber as early as possible.
1 parent 975b9fa commit 7f00f35

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

lib/async/task.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,9 @@ def warn(...)
456456

457457
# Finish the current task, moving any children to the parent.
458458
def finish!
459+
# Break the cycle:
460+
@fiber&.async_task = nil
461+
459462
# Don't hold references to the fiber or block after the task has finished:
460463
@fiber = nil
461464
@block = nil # If some how we went directly from initialized to finished.

test/async/task.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@
8585
expect(was_current).to be == true
8686
expect(task).not.to be(:current?)
8787
end
88+
89+
it "clears async_task on fiber after task finishes" do
90+
fiber = nil
91+
92+
task = reactor.async do |task|
93+
fiber = Fiber.current
94+
end
95+
96+
# After the task completes, the fiber should no longer reference the task:
97+
expect(fiber.async_task).to be_nil
98+
end
8899
end
89100

90101
with "#async" do

0 commit comments

Comments
 (0)