You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/fiber.md
+58Lines changed: 58 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -212,6 +212,64 @@ I/O. Windows is a notable example where socket I/O can be non-blocking but pipe
212
212
I/O is blocking. Provided that there *is* a scheduler and the current thread *is
213
213
non-blocking*, the operation will invoke the scheduler.
214
214
215
+
##### `IO#close`
216
+
217
+
Closing an IO interrupts all blocking operations on that IO. When a thread calls `IO#close`, it first attempts to interrupt any threads or fibers that are blocked on that IO. The closing thread waits until all blocked threads and fibers have been properly interrupted and removed from the IO's blocking list. Each interrupted thread or fiber receives an `IOError` and is cleanly removed from the blocking operation. Only after all blocking operations have been interrupted and cleaned up will the actual file descriptor be closed, ensuring proper resource cleanup and preventing potential race conditions.
218
+
219
+
For fibers managed by a scheduler, the interruption process involves calling `rb_fiber_scheduler_fiber_interrupt` on the scheduler. This allows the scheduler to handle the interruption in a way that's appropriate for its event loop implementation. The scheduler can then notify the fiber, which will receive an `IOError` and be removed from the blocking operation. This mechanism ensures that fiber-based concurrency works correctly with IO operations, even when those operations are interrupted by `IO#close`.
220
+
221
+
```mermaid
222
+
sequenceDiagram
223
+
participant ThreadB
224
+
participant ThreadA
225
+
participant Scheduler
226
+
participant IO
227
+
participant Fiber1
228
+
participant Fiber2
229
+
230
+
Note over ThreadA: Thread A has a fiber scheduler
231
+
activate Scheduler
232
+
ThreadA->>Fiber1: Schedule Fiber 1
233
+
activate Fiber1
234
+
Fiber1->>IO: IO.read
235
+
IO->>Scheduler: rb_thread_io_blocking_region
236
+
deactivate Fiber1
237
+
238
+
ThreadA->>Fiber2: Schedule Fiber 2
239
+
activate Fiber2
240
+
Fiber2->>IO: IO.read
241
+
IO->>Scheduler: rb_thread_io_blocking_region
242
+
deactivate Fiber2
243
+
244
+
Note over Fiber1,Fiber2: Both fibers blocked on same IO
0 commit comments