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

asyncevents: fix missing GC root and race #44956

Merged
merged 1 commit into from
Apr 13, 2022
Merged

asyncevents: fix missing GC root and race #44956

merged 1 commit into from
Apr 13, 2022

Conversation

vtjnash
Copy link
Sponsor Member

@vtjnash vtjnash commented Apr 12, 2022

The event might have triggered on another thread before we observed it
here, or it might have gotten finalized before it got triggered. Either
outcome can result in a lost event. (I observed the later situation
occurring locally during the Dates test once).

The event might have triggered on another thread before we observed it
here, or it might have gotten finalized before it got triggered. Either
outcome can result in a lost event. (I observed the later situation
occurring locally during the Dates test once).
@vtjnash vtjnash added backport 1.7 backport 1.8 Change should be backported to release-1.8 labels Apr 12, 2022
@vtjnash vtjnash requested a review from tkf April 12, 2022 20:05
Comment on lines 47 to +49
async = AsyncCondition()
t = @task while _trywait(async)
cb(async)
isopen(async) || return
t = @task begin
unpreserve_handle(async)
Copy link
Member

Choose a reason for hiding this comment

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

Isn't async reachable through t.code? Why would it be finalized in the old code?

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

t is only reachable through async

Copy link
Member

Choose a reason for hiding this comment

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

ahhhh

@@ -115,6 +124,7 @@ function _trywait(t::Union{Timer, AsyncCondition})
# full barrier now for AsyncCondition
t isa Timer || Core.Intrinsics.atomic_fence(:acquire_release)
else
t.isopen || return false
Copy link
Member

@tkf tkf Apr 13, 2022

Choose a reason for hiding this comment

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

Since this is outside a lock, don't we need to acquire load/release store t.isopen for AsyncCondition so that close(async) happens-before wait(async) throws? Not sure if that's needed though.

Copy link
Member

@tkf tkf Apr 13, 2022

Choose a reason for hiding this comment

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

I think it's conceivable that someone writes

async = AsyncCondition()

t1 = @spawn begin  # code in external library
    f1()
    ccall(:uv_async_send, Cvoid, (Ptr{Cvoid},), async)
end

t2 = @spawn begin
    try
        wait(async)
    catch err
        err isa EOFError || rethrow()
    end
    close(async)
    f2()
end

t3 = @spawn begin
    try
        wait(async)
    catch err
        err isa EOFError || rethrow()
    end
    close(async)
    f3()
end

and expect f1 to happen-before f2 and f3. There are better ways to do level-triggering based on AsyncCondition but it does not look super crazy to me.

But I'm also OK with documenting that wait does not establish any ordering if it throws.

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

Establishing happens-before seems like a good idea. We don't do that currently, so it is not relevant to this PR however.

Copy link
Member

@tkf tkf left a comment

Choose a reason for hiding this comment

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

It LGTM other than deciding what to do with the ordering of t.isopen

@vtjnash vtjnash merged commit 8cc00ff into master Apr 13, 2022
@vtjnash vtjnash deleted the jn/timer-missed branch April 13, 2022 20:26
@KristofferC KristofferC mentioned this pull request Apr 19, 2022
40 tasks
KristofferC pushed a commit that referenced this pull request Apr 19, 2022
The event might have triggered on another thread before we observed it
here, or it might have gotten finalized before it got triggered. Either
outcome can result in a lost event. (I observed the later situation
occurring locally during the Dates test once).

(cherry picked from commit 8cc00ff)
KristofferC pushed a commit that referenced this pull request Apr 19, 2022
The event might have triggered on another thread before we observed it
here, or it might have gotten finalized before it got triggered. Either
outcome can result in a lost event. (I observed the later situation
occurring locally during the Dates test once).

(cherry picked from commit 8cc00ff)
@KristofferC KristofferC mentioned this pull request Apr 19, 2022
67 tasks
@KristofferC KristofferC removed backport 1.8 Change should be backported to release-1.8 backport 1.7 labels May 26, 2022
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