Skip to content

Commit 2d1742b

Browse files
committed
Fix: calling Fiber::ExecutionContext#enqueue from bare Thread
A bare thread doesn't have an execution context, yet may be capable to call Fiber#enqueue to enqueue a fiber back into its original context.
1 parent a7d89af commit 2d1742b

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

src/fiber/execution_context.cr

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ module Fiber::ExecutionContext
122122
Thread.current.execution_context
123123
end
124124

125+
def self.current? : ExecutionContext?
126+
Thread.current.execution_context?
127+
end
128+
125129
# :nodoc:
126130
#
127131
# Tells the current scheduler to suspend the current fiber and resume the
@@ -181,6 +185,7 @@ module Fiber::ExecutionContext
181185
#
182186
# Enqueues a fiber to be resumed inside the execution context.
183187
#
184-
# May be called from any ExecutionContext (i.e. must be thread-safe).
188+
# May be called from any ExecutionContext (i.e. must be thread-safe). May also
189+
# be called from bare threads (outside of an ExecutionContext).
185190
abstract def enqueue(fiber : Fiber) : Nil
186191
end

src/fiber/execution_context/multi_threaded.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ module Fiber::ExecutionContext
180180

181181
# :nodoc:
182182
def enqueue(fiber : Fiber) : Nil
183-
if ExecutionContext.current == self
183+
if ExecutionContext.current? == self
184184
# local enqueue: push to local queue of current scheduler
185185
ExecutionContext::Scheduler.current.enqueue(fiber)
186186
else

src/fiber/execution_context/single_threaded.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ module Fiber::ExecutionContext
102102

103103
# :nodoc:
104104
def enqueue(fiber : Fiber) : Nil
105-
if ExecutionContext.current == self
105+
if ExecutionContext.current? == self
106106
# local enqueue
107107
Crystal.trace :sched, "enqueue", fiber: fiber
108108
@runnables.push(fiber)

0 commit comments

Comments
 (0)