# Preserve against garbage collection in LibGit2

**URL:** <https://discourse.julialang.org/t/preserve-against-garbage-collection-in-libgit2/117095>\
**Category:** Internals & Design\
**Tags:** libgit2\
**Created:** [July 16, 2024, 11:15am UTC](https://discourse.julialang.org/t/preserve-against-garbage-collection-in-libgit2/117095 "2024-07-16T11:15:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![emmt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/emmt/32/5192_2.png) [@emmt](https://discourse.julialang.org/u/emmt)\
**Post date:** [July 16, 2024, 11:15am UTC](https://discourse.julialang.org/t/preserve-against-garbage-collection-in-libgit2/117095/1 "2024-07-16T11:15:50Z")

</div>

Maybe I am wrong but I have a concern about the implementation of `LibGit2` regarding garbage collection. To ilustrate this, please have a look at the code of a simple function such as:

```julia
function Base.length(blob::GitBlob)
    ensure_initialized()
    return ccall((:git_blob_rawsize, libgit2), Int64, (Ptr{Cvoid},), blob.ptr)
end

```

in `stdlib/LibGit2/src/blob.jl`. My understanding is that there is nothing above to prevent `blob` to be garbage collected. This could be done by:

```julia
function Base.length(blob::GitBlob)
    ensure_initialized()
    GC.@preserve blob begin
        return ccall((:git_blob_rawsize, libgit2), Int64, (Ptr{Cvoid},), blob.ptr)
    end
end

```

Thanks to the specialization of the `Base.unsafe_convert` method for Git objects in `stdlib/LibGit2/src/types.jl`, another (better) possibility would be to just replace `blob.ptr` by `blob`:

```julia
Base.unsafe
function Base.length(blob::GitBlob)
    ensure_initialized()
    return ccall((:git_blob_rawsize, libgit2), Int64, (Ptr{Cvoid},), blob)
end

```

The same remark applies for other types of Git objects and other methods. If I am right, there are a number of `ccalls` in `stdlib/LibGit2/src` that could benefit from this (small) change.

---

<div class="post-metadata">

**Author:** ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)\
**Post date:** [July 16, 2024, 11:26am UTC](https://discourse.julialang.org/t/preserve-against-garbage-collection-in-libgit2/117095/2 "2024-07-16T11:26:53Z")

</div>

Yeah, the libgit2 wrappers weren’t written particularly carefully. Most of these sorts of issues that caused practical problems in CI’d usage were fixed, but there’s others remaining. PRs welcome.

---

<div class="post-metadata">

**Author:** ![emmt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/emmt/32/5192_2.png) [@emmt](https://discourse.julialang.org/u/emmt)\
**Post date:** [July 16, 2024, 11:53am UTC](https://discourse.julialang.org/t/preserve-against-garbage-collection-in-libgit2/117095/3 "2024-07-16T11:53:50Z")

</div>

OK I’d be happy to make a PR but do you confirm my analysis and the proposed fix? I do not want to introduce other errors.

---

<div class="post-metadata">

**Author:** ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)\
**Post date:** [July 16, 2024, 11:55am UTC](https://discourse.julialang.org/t/preserve-against-garbage-collection-in-libgit2/117095/4 "2024-07-16T11:55:45Z")

</div>

You are correct.

---

<div class="post-metadata">

**Author:** ![emmt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/emmt/32/5192_2.png) [@emmt](https://discourse.julialang.org/u/emmt)\
**Post date:** [July 16, 2024, 5:11pm UTC](https://discourse.julialang.org/t/preserve-against-garbage-collection-in-libgit2/117095/5 "2024-07-16T17:11:25Z")

</div>

The PR is here: [Preserve Git objects from being garbage collected by emmt · Pull Request #55142 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/55142)

There are many changes and I have a few doubts (indicated by `FIXME` comments) but all tests in `LibGit2` were successul.
