Skip to content

Commit c023053

Browse files
ihabadhamclaude
andcommitted
Fix race condition in drain_streams_concurrently ensure block
The ensure block was checking client_disconnected before waiting for writing_task to complete. This caused a race where the flag might still be false when checked, even though writing_task would set it to true moments later. Fixed by reordering the ensure block: 1. Close queue (unblocks writing_task if waiting on dequeue) 2. Wait for writing_task (ensures flag is set if disconnect occurred) 3. Check flag and stop barrier if needed This ensures producers are properly stopped when clients disconnect, preventing wasted CPU cycles. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent b0c86d0 commit c023053

File tree

1 file changed

+8
-4
lines changed
  • react_on_rails_pro/lib/react_on_rails_pro/concerns

1 file changed

+8
-4
lines changed

react_on_rails_pro/lib/react_on_rails_pro/concerns/stream.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,16 @@ def drain_streams_concurrently(parent_task)
9393
raise e
9494
end
9595
ensure
96-
# If client disconnected, stop all producer tasks to avoid wasted work
97-
@async_barrier.stop if client_disconnected
98-
99-
# Close the queue to signal end of streaming
96+
# Close the queue first to unblock writing_task (it may be waiting on dequeue)
10097
@main_output_queue.close
98+
99+
# Wait for writing_task to ensure client_disconnected flag is set
100+
# before we check it (fixes race condition where ensure runs before
101+
# writing_task's rescue block sets the flag)
101102
writing_task.wait
103+
104+
# If client disconnected, stop all producer tasks to avoid wasted work
105+
@async_barrier.stop if client_disconnected
102106
end
103107

104108
def log_client_disconnect(context, exception)

0 commit comments

Comments
 (0)