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

Asynchronous tasks can accidentally call patched functions #89

Closed
omus opened this issue Sep 17, 2021 · 6 comments · Fixed by #91
Closed

Asynchronous tasks can accidentally call patched functions #89

omus opened this issue Sep 17, 2021 · 6 comments · Fixed by #91
Assignees

Comments

@omus
Copy link
Member

omus commented Sep 17, 2021

In a scenario where an background asynchronous task is calling a function using @mock and a patch is applied during that time the async task can call the patched function instead of the original:

julia> using Mocking

julia> Mocking.activate()

julia> f() = 1
f (generic function with 1 method)

julia> function g(name, n)
           for i in 1:n
               x = @mock f()
               println(name, ": ", x)
               sleep(1)
           end
       end
g (generic function with 1 method)

julia> p = @patch f() = 2

Patch{typeof(f)}(f, var"##f_patch#259")

julia> @async g("async", 5)
async: Task (runnable) @0x000000012f5973101


julia> apply(p) do
           g("apply", 1)
       end
apply: 2
async: 2

julia> async: 1
async: 1
async: 1
julia>
@oxinabox
Copy link
Member

oxinabox commented Oct 1, 2021

We might want to push the variable that says if we are mocking or not into a dynamically scoped context from
ContextVariablesX.jl
I think that supports tasks and threads.
We use it in Checkpoints.jl as of invenia/Checkpoints.jl#15

@omus
Copy link
Member Author

omus commented Oct 1, 2021

I've confirmed that ContextVariablesX.jl does fix the issue as demonstrated above. I'll work on a PR for this.

@oxinabox I appreciate you bringing that package to my attention ❤️

@omus omus self-assigned this Oct 1, 2021
@omus omus closed this as completed in #91 Oct 7, 2021
@omus omus reopened this Oct 14, 2021
@omus
Copy link
Member Author

omus commented Oct 14, 2021

Unfortunately using ContextVariablesX.jl doesn't play nice with @test_logs or @test_deprecated so using that had to be reverted.

@oxinabox
Copy link
Member

@tkf any ideas?

@tkf
Copy link
Member

tkf commented Oct 14, 2021

omus already figured it out. But, linking the discussion for the record: tkf/ContextVariablesX.jl#16 (comment)

@omus
Copy link
Member Author

omus commented Jul 15, 2024

Issue was fixed in #112 but requires Julia 1.11

@omus omus closed this as completed Jul 15, 2024
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 a pull request may close this issue.

3 participants