Skip to content

feat: make it easier to eagerly consume completed tasks - #1

Merged
hawkw merged 13 commits into
mainfrom
eliza/fix-ilianas-problem
Dec 1, 2026
Merged

hawkw merged 13 commits into
mainfrom
eliza/fix-ilianas-problem

Conversation

@hawkw

@hawkw hawkw commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

This branch makes a large, breaking API change to the ParallelTaskSet API as
imported from the omicron repo, for a purpose that is somewhat hard to
summarize in a one-line commit message title. This is all intended to help avoid
an issue @iliana ran into while using this API, which is that when tasks
complete faster than they are spawned, the completed tasks remain in memory
until the entire ParallelTaskSet is drained. This means that the heap used by
those tasks is not freed, and if they have acquired RAII resources, they do not
release those resources until they are consumed. In order to prevent resource
exhaustion, this branch makes two main changes to ParallelTaskSet:

  1. Rather than returning an Option<T> containing the output of one task,
    ParallelTaskSet::spawn now returns a new CompletedTasks iterator. This
    iterator will return both the output of any task that was awaited in order to
    spawn a new one, and will continue to return the output of any tasks that
    can be joined without awaiting. This way, it's easier for callers to ensure
    that all currently completed tasks are joined more eagerly, freeing any
    memory or other resources those tasks are holding. The iterator is marked as
    must_use to try to encourage callers to ensure it is always drained fully.

  2. In addition, the parallelism limit continues to count tasks until they are
    fully consumed, by having the spawned tasks return their Semaphore permit
    when they are joined, rather than dropping it as soon as the inner future
    completes. This way, the parallelism limit bounds the memory and/or resources
    held by tasks regardless of whether they are currently running or have
    completed but have not yet been consumed.

    This also allows us to make a smallish optimization to spawn where we
    re-use the permit held by the task that we await in order to spawn a new one.
    This way, we don't have to do a few atomic increment/decrement operations
    (and potentially lock the semaphore) in order to free the permit and then
    immediately acquire a new one. This probably doesn't matter a whole lot, but
    I thought it was cute.

Comment thread src/lib.rs Resolved
@hawkw
hawkw force-pushed the eliza/fix-ilianas-problem branch from 878bb0b to 6db21f5 Compare November 27, 2026 21:05
@hawkw
hawkw added this pull request to stack #4 November 27, 2026 21:56
@hawkw hawkw changed the title attempt to fix the problem iliana ran into feat: make it easier to eagerly consume completed tasks Nov 27, 2026
@hawkw
hawkw marked this pull request as ready for review November 27, 2026 23:29
@hawkw
hawkw force-pushed the eliza/fix-ilianas-problem branch from 9706862 to c16524c Compare November 27, 2026 23:32
@hawkw
hawkw requested review from iliana and smklein November 27, 2026 23:33
@hawkw
hawkw enabled stacked auto-merge (squash) November 27, 2026 23:46
Comment thread src/lib.rs Outdated

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Before this PR, we vended out permits from the semaphore which lived alongside the tasks, and were dropped after the spawned future completed.

After this PR, the permit is no longer dropped, but returned from the completed task itself. This means, I think, that the permit only "dropped" after someone reclaims it explicitly by joining the next task in the set.

I think this means that the permits are now tightly coupled with the size of the internal JoinSet. Does this mean we don't actually need to use a semaphore/permit at all anymore?

Could we change spawn to the following:

// "max" is whatever size the semaphore used to be.
// but here there's no semaphore anymore
let output = if self.set.len() >= self.max {
  self.set.join_next().await
} else {
  None
};

self.set.spawn(future)

(this obviously removes the need for the (Permit, T) tuple too, if we think this is legit?)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think this means that the permits are now tightly coupled with the size of the internal JoinSet. Does this mean we don't actually need to use a semaphore/permit at all anymore?

Could we change spawn to the following:

I had also wondered about this. I wasn't sure about whether I wanted to do that as part of this change or not, but it's certainly much simpler that way...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Okay, I went ahead and did this, and...wow, it's a lot simpler now. Good call.

@hawkw
hawkw requested a review from smklein November 30, 2026 23:16

@smklein smklein left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nice, that looks way simpler

stacked auto-merge was automatically disabled November 30, 2026 23:21

Auto-merge was disabled for another pull request in the stack

@hawkw
hawkw enabled stacked auto-merge (squash) November 30, 2026 23:50
@hawkw
hawkw merged commit 7468140 into main Dec 1, 2026
5 checks passed
@hawkw
hawkw deleted the eliza/fix-ilianas-problem branch December 1, 2026 01:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants