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

DocTestSetup shouldn't be re-executed with follow-up named doctests #2567

Open
maleadt opened this issue Aug 30, 2024 · 0 comments
Open

DocTestSetup shouldn't be re-executed with follow-up named doctests #2567

maleadt opened this issue Aug 30, 2024 · 0 comments

Comments

@maleadt
Copy link
Contributor

maleadt commented Aug 30, 2024

In LLVM.jl, I'm using DocTestSetup to allocate an LLVM context which contains all derived LLVM resources. I'm also cleaning up any previous context, because Documenter.jl currently doesn't have a DocTestTeardown or something like it (#2566):

```@meta
DocTestSetup = quote
    using LLVM

    # XXX; clean-up previous contexts
    while context(; throw_error=false) !== nothing
        dispose(context())
    end

    ctx = Context()
end
```

At the same time, I was using named doctests in order to share variables between different doctests:

```jldoctest mod
julia> mod = LLVM.Module("SomeModule");
```

```jldoctest mod
julia> println(mod)
LLVM.Module("SomeModule")
```

This segfaults, because the DocTestSetup triggers again for the second block, destroying the context in which mod was allocated. This is surprising to me, but I realize it can probably not be changed without breaking existing doctests...


Contrived MWE demonstrating the issue:

```@meta
DocTestSetup = quote
    # remove any previous buffer (JuliaDocs/Documenter.jl#2566)
    try
        ccall(:free, Cvoid, (Ptr{Cvoid},), task_local_storage(:buf))
    catch
    end

    # allocate a new buffer
    buf = ccall(:malloc, Ptr{Int}, (Csize_t,), 1)
    ccall(:memset, Cvoid, (Ptr{Int}, Int, Csize_t), buf, 0, 1)
    task_local_storage(:buf, buf)
end
```

```jldoctest ptr
julia> ptr = buf;

julia> unsafe_load(ptr)
0
```

```jldoctest ptr
julia> unsafe_load(ptr)
0
```

On my system, the second doctest doesn't cause a crash, but doesn't show the correct value.

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

No branches or pull requests

1 participant