Skip to content

Direct setting of current_task().scope is undefined behaviour. #1

@vchuravy

Description

@vchuravy

function scoped_values_set_context(pair::Pair{<:ScopedValue}, rest::Pair{<:ScopedValue}...)
ct = Base.current_task()
original_scope = ct.scope::Union{Nothing, ScopedValues.Scope}
ct.scope = ScopedValues.Scope(original_scope, pair, rest...)
return original_scope
end
function scoped_values_reset_context(old)
ct = current_task()
ct.scope = old
return nothing
end

The direct modification of these fields is undefined behavior. JuliaLang/julia#52309

When I was asked about this in JuliaLang/ScopedValues.jl#14 (comment) I stated that this optimization was planned.

scope  = currrent_scope()
scoped_values_reset_context(#=...=#)
scope = current_scope()

Is now being transformed to:

scope  = currrent_scope()
scoped_values_reset_context(#=...=#)

The right way IMO is to "snapshot" the scope with a potential list of scoped values into a PersistentDict and then to write with(snapshot...) do f that sets those scopedvalues to those values again.

(found through Github search for ScopedValue)

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