Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

refactor/rewrite concurrent.rs event loop#143

Merged
dicej merged 2 commits into
mainfrom
dicej/refactor-concurrent-rs
Apr 30, 2025
Merged

refactor/rewrite concurrent.rs event loop#143
dicej merged 2 commits into
mainfrom
dicej/refactor-concurrent-rs

Conversation

@dicej
Copy link
Copy Markdown
Collaborator

@dicej dicej commented Apr 30, 2025

The event loop and scheduler in concurrent.rs have undergone a lot of changes over time, reflecting significant changes in the component model spec. Some features, such as yielding, backpressure, and waitable sets, were retrofitted onto the existing code in a somewhat haphazard way, making the code more convoluted than it needed to be. In addition, it made control flow hard to understand and debug, with multiply-nested fibers and no clear division of responsibility among tasks and the fibers they ran on.

This commit rewrites most of that code to make it more cohesive:

  • No nested fibers: we always suspend the current fiber before resuming the next one
  • One single function (poll_until) runs the event loop (rather than several functions with slightly different semantics, as before)
  • The code paths for {Typed}Func::call_{async,concurrent} have been unified, reducing redundancy and unsafe code
  • We now manage reentrance, yielding, and backpressure by explicitly deferring work as appropriate
    • Work is deferred to either:
      • a high priority queue (work to be done ASAP)
      • a low priority queue (work to be done only once the high priority queue has been drained)
      • a per-instance pending bucket (work that can't be done until a current activation for that instance returns and/or backpressure is unset)
  • The new code is more waitable-set-native, removing obsolete ties between parent and child tasks which no longer apply
  • Fixed previous unsoundness due to recursively modifying ConcurrentState::futures while polling it
  • More types implement Debug, making logs easier to follow

@dicej dicej force-pushed the dicej/refactor-concurrent-rs branch from d45126a to a89bf44 Compare April 30, 2025 14:42
The event loop and scheduler in `concurrent.rs` have undergone a lot of changes
over time, reflecting significant changes in the component model spec.  Some
features, such as yielding, backpressure, and waitable sets, were retrofitted
onto the existing code in a somewhat haphazard way, making the code more
convoluted than it needed to be.  In addition, it made control flow hard to
understand and debug, with multiply-nested fibers and no clear division of
responsibility among tasks and the fibers they ran on.

This commit rewrites most of that code to make it more cohesive:

- No nested fibers: we always suspend the current fiber before resuming the next one
- One single function (`poll_until`) runs the event loop (rather than several functions with slightly different semantics, as before)
- The code paths for `{Typed}Func::call_{async,concurrent}` have been unified, reducing redundancy and unsafe code
- We now manage reentrance, yielding, and backpressure by explicitly deferring work as appropriate
    - Work is deferred to either:
        - a high priority queue (work to be done ASAP)
        - a low priority queue (work to be done only once the high priority queue has been drained)
        - a per-instance `pending` bucket (work that can't be done until a current activation for that instance returns and/or backpressure is unset)
- The new code is more `waitable-set`-native, removing obsolete ties between parent and child tasks which no longer apply
- Fixed previous unsoundness due to recursively modifying `ConcurrentState::futures` while polling it
- More types implement `Debug`, making logs easier to follow

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
@dicej dicej force-pushed the dicej/refactor-concurrent-rs branch from a89bf44 to ef11ee9 Compare April 30, 2025 14:51
@dicej dicej added this pull request to the merge queue Apr 30, 2025
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Apr 30, 2025
`StoreFiber`s are now stored in a few other places besides
`ConcurrentState::table`, so we need to clear those out as well.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
@dicej dicej enabled auto-merge April 30, 2025 15:35
@dicej dicej added this pull request to the merge queue Apr 30, 2025
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Apr 30, 2025
@dicej dicej added this pull request to the merge queue Apr 30, 2025
Merged via the queue into main with commit c31ea25 Apr 30, 2025
44 checks passed
@dicej dicej deleted the dicej/refactor-concurrent-rs branch April 30, 2025 16:34
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant