Skip to content

Not threadsafe (or possibly even async-safe) on Julia 1.10 #146

Open

Description

@kleinschmidt

For julia 1.11+, the list of active patches (the patch env) is stored in a ScopedValue, which makes it safe to apply inside tasks, even when those tasks are scheduled on different threads and executing concurrently. For earlier versions, the patch env is stored in a package global Ref, which is definitely not concurrency friendly lol.

IIUC, the reason we're not using the ScopedValues.jl fallback is because it uses the logging machinery on julia <1.11 to smuggle in scoped values, and this in turn interferes with the test logger that @test_logs uses to, you know, test logging.

It seems really bad to me that there's silently, inconsistently thread-unsafe application of patches in some versions of julia but not others. I'd rather we allow users to opt out of using the ScopedValues fallback if they need to. I think it would be possible to allow users to control whether a global ref is used or a scoped value with a configuration option, like a boolean flag ref that can be set.

Activity

  1. omus commented on Sep 4, 2026

    @omus
    Member

    The original thinking on this was to let newer versions of Julia use the newer features. However, inconsistent behavior between different versions of Julia is asking for trouble. I see two ways out of this mess:

    1. Release new versions of Mocking.jl which require newer versions of Julia for the features that require it. I want to keep supporting the LTS so I think I would end up supporting a version for the LTS, 1.11+ (ScopedValues support), and 1.12+ (ability to drop @mock on call sites).
    2. Introduce a "strategy" global which allows the user to choose between the Ref approach, ScopedValues, or ScopedValues without @mock). If the user attempts to use a strategy which isn't supported by that version of Julia we can throw an error. The main issue with this would be maintainability of the code base.

    I think option 2 would be better if we can support it.

    There is also a challenge with what to do with the current inconsistent behavior. With option 2 I think I'd have a "inconsistent" strategy which keeps the current behavior as the default. I could also make a new breaking release which possible uses the best strategy and forces users to opt into another strategy if they want to use that version of Julia with Mocking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions