Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iterating multiple times over a taskSeq { ... } raises InvalidOperationException: An attempt was made to transition a task to a final state when it had already completed. #39

Closed
abelbraaksma opened this issue Oct 22, 2022 · 1 comment · Fixed by #42
Labels
bug Something isn't working help wanted Extra attention is needed
Milestone

Comments

@abelbraaksma
Copy link
Member

abelbraaksma commented Oct 22, 2022

As in the title. Any of these errors only happen when multiple iterations are attempted and the source is a taskSeq CE, not when the source is a user-defined or library defined IAsyncEnumerator<_>, used with the TaskSeq library functions.

I already started investigating this issue and it has to do with properly resetting state when "reaching the end" and when "getting an enumerator over the same resource". See #36 continued: #42.

Operation not valid error, MoveNextAsync()

// this throws: InvalidOperationException: Operation is not valid due to the current state of the object.
task {
    let tskSeq = taskSeq { yield 1; yield 2 }
    let enum = tskSeq.GetAsyncEnumerator()
    let! isNext = enum.MoveNextAsync()  // true
    let! isNext = enum.MoveNextAsync()  // true
    let! isNext = enum.MoveNextAsync()  // false
    let! isNext = enum.MoveNextAsync()  // error here
    ()
}

Operation not valid error, multiple GetAsyncEnumerator() with MoveNextAsync()

// throws: InvalidOperationException: Operation is not valid due to the current state of the object.
task {
    let tskSeq = getEmptyVariant variant
    use enumerator = tskSeq.GetAsyncEnumerator()
    let! isNext = enumerator.MoveNextAsync()
    use enumerator = tskSeq.GetAsyncEnumerator()
    let! isNext = enumerator.MoveNextAsync()  // throws here
    ()
}

Transition State error

// this throws: 
// InvalidOperationException: 
// An attempt was made to transition a task to a final state when it had already completed.
task {
    let tskSeq = taskSeq { yield 1; yield 2 }
    let ts1 = tskSeq |> TaskSeq.map (fun i -> i + 1)
    let result1 = TaskSeq.toArray ts1
    let ts2 = ts1 |> TaskSeq.map (fun i -> i + 1)
    let result2 = TaskSeq.toArray ts2 // error here
    ()
}
abelbraaksma added a commit that referenced this issue Oct 22, 2022
@abelbraaksma abelbraaksma changed the title Iterating multiple times over a taskSeq { ... } computation expression leads to: InvalidOperationException: An attempt was made to transition a task to a final state when it had already completed. Iterating multiple times over a taskSeq { ... } raises InvalidOperationException: An attempt was made to transition a task to a final state when it had already completed. Oct 22, 2022
@abelbraaksma abelbraaksma added bug Something isn't working help wanted Extra attention is needed labels Oct 22, 2022
abelbraaksma added a commit that referenced this issue Oct 22, 2022
@abelbraaksma abelbraaksma linked a pull request Oct 22, 2022 that will close this issue
6 tasks
abelbraaksma added a commit that referenced this issue Oct 24, 2022
abelbraaksma added a commit that referenced this issue Oct 24, 2022
abelbraaksma added a commit that referenced this issue Oct 26, 2022
abelbraaksma added a commit that referenced this issue Oct 28, 2022
@abelbraaksma
Copy link
Member Author

abelbraaksma commented Oct 29, 2022

Solved through #36, #42, #49 and #51.

@abelbraaksma abelbraaksma added this to the v0.1.0 milestone Mar 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
1 participant