Skip to content

RFC 2: Add Fiber::ExecutionContext::Isolated#15513

Merged
straight-shoota merged 15 commits intocrystal-lang:masterfrom
ysbaddaden:feature/execution-context-isolated
Mar 22, 2025
Merged

RFC 2: Add Fiber::ExecutionContext::Isolated#15513
straight-shoota merged 15 commits intocrystal-lang:masterfrom
ysbaddaden:feature/execution-context-isolated

Conversation

@ysbaddaden
Copy link
Collaborator

@ysbaddaden ysbaddaden commented Feb 24, 2025

Aka bubble wrap a thread.

The Fiber::ExecutionContext::Isolated context is a special scheduler: it starts a single thread to execute a block a code inside its main fiber. Concurrency is disabled, no other fiber can run on this thread. The fiber owns the thread and can do any number of blocking operations as it needs to without blocking any other fiber.

But you can still:

  • do IO operations normally;
  • communicate with other fibers (in other contexts) normally through the regular sync primitives (Channel, Mutex or WaitGroup);
  • spawn fibers, they will be spawned into another context (for example the default context).

NOTE: this PR alone isn't enough to enable execution contexts. It misses either the ST or MT context.

refs #15342

Introduces the last EC scheduler that runs a single fiber in a single
thread. Contrary to the other schedulers, concurrency is disabled.

Like the ST scheduler, the scheduler doesn't need to actively park the
thread and merely waits on the event loop.
The point is to avoid having fiber being spawn in an unknown context
from the isolated fiber. While we could default to `nil` it would delay
the understanding of the behavior to a runtime error, while having a
compilation error will bring up the issue sooner, and makes the intent
more explicit.

We'll decide later if we want to have a default value, and what (nil?
current context? default context?)
protected getter thread : Thread
@main_fiber : Fiber

getter event_loop : Crystal::EventLoop = Crystal::EventLoop.create
Copy link
Collaborator Author

@ysbaddaden ysbaddaden Feb 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optimization: the discussion on the spawn context made me realize that an isolated fiber may not need an event loop. It should be lazily initialized when needed, though it means that the thread will need to be able to park itself (#enqueue and #reschedule must take care of this case) 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recall that I dismissed the following but I can never recall why: there could be a blocking event loop that would merely do blocking calls.

And I just recalled: sockets are nonblocking on UNIX (O_NONBLOCK) and the syscalls wouldn't be blocking.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Wasi event loop is also blocking only. There are no threads there, though.

@BigBoyBarney
Copy link
Contributor

For what it's worth, I've been using the EC shard's Isolated context with an MT default context for a while now in my app and it works wonderfully.

Thank you for your hard work 🫡

@ysbaddaden ysbaddaden changed the title Add Fiber::ExecutionContext::Isolated RFC 2: Add Fiber::ExecutionContext::Isolated Mar 3, 2025
Co-authored-by: Johannes Müller <straightshoota@gmail.com>
@straight-shoota straight-shoota added this to the 1.16.0 milestone Mar 20, 2025
@ysbaddaden ysbaddaden moved this from Review to Approved in Multi-threading Mar 21, 2025
@straight-shoota straight-shoota changed the title RFC 2: Add Fiber::ExecutionContext::Isolated Add Fiber::ExecutionContext::Isolated Mar 22, 2025
@straight-shoota straight-shoota merged commit 89c8a47 into crystal-lang:master Mar 22, 2025
31 of 32 checks passed
@github-project-automation github-project-automation bot moved this from Approved to Done in Multi-threading Mar 22, 2025
@ysbaddaden ysbaddaden deleted the feature/execution-context-isolated branch March 22, 2025 16:21
@straight-shoota straight-shoota changed the title Add Fiber::ExecutionContext::Isolated RFC 2: Add Fiber::ExecutionContext::Isolated Mar 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

3 participants