✨ throw CircularTaskError when a task depends on itself or an ancestor#1162
Open
✨ throw CircularTaskError when a task depends on itself or an ancestor#1162
Conversation
A task may not wait on its own result or its own halt(), nor on any of its ancestors' — by structured concurrency the parent cannot complete until every child does, so any descendant waiting on an ancestor is a circular dependency that would deadlock. Detect synchronously inside the task[Symbol.iterator] and halt thenable[Symbol.iterator] wrappers via assertNonCircular: walk the calling scope's contexts prototype chain and throw if the targeted task's scope appears in it. Sibling and downward (parent→child) waits are unaffected. Expose createFuture's operation directly on FutureWithResolvers so the task iterator wrapper can yield to it without re-entering its own overridden Symbol.iterator (which would recurse).
commit: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
A task that awaits its own result or the result of an ancestor is a circular dependency that should deadlock. The parent cannot complete until every child does, so any descendant waiting on itself or an ancestor should hang forever. This applies equally to the main task computation as well as its
halt()We currently try to fudge this situation when we encounter it and try to allow it to muddle through for historical reasons, but that ends up making things works by introducing complexity in order to paper over a pathological use-case. It's better to be up-front about it and when we detect a circular dependency between task, explicitly call that out
The options are:
Approach
Compare the scopes of the operation yielding to a task and the task itself. If the caller is, or is descended from the task being halted, then raise a
CircularTaskError.Potential Drawbacks