Skip to content

Exception when using LibGit2Sharp with .NET 9 preview 6 #104872

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

Closed
meziantou opened this issue Jul 14, 2024 · 7 comments
Closed

Exception when using LibGit2Sharp with .NET 9 preview 6 #104872

meziantou opened this issue Jul 14, 2024 · 7 comments
Labels
area-System.Runtime.InteropServices question Answer questions and provide assistance, not an issue with source code or documentation.
Milestone

Comments

@meziantou
Copy link
Contributor

Description

When running the repro code with .NET 9 preview 6, the execution stops. The same code runs well with .NET 8.

I can also reproduce the error using the daily build (9.0.100-preview.7.24362.28 (a2689ba4aa))

image

Note: When running the code in my original project, VS reports an System.ExecutionEngineException exception. However, VS doesn't show this error in the repro code, but it still fail.

image

Reproduction Steps

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>net9.0;net8.0</TargetFrameworks>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="LibGit2Sharp" Version="0.30.0" />
  </ItemGroup>
</Project>
using System.Text;
using LibGit2Sharp;

// Init the git repository
var dir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(dir);
Repository.Init(dir, isBare: true);

// Call CreateBlob many times
using (var repo = new Repository(dir))
{
    // It doesn't fail on the first iteration
    for (var i = 0; i < 10; i++)
    {
        Console.WriteLine(i); // Prints 0, 1, 2 and then the execution stops

        using var stream = new MemoryStream(Encoding.UTF8.GetBytes($"Hello, World {i}!"));
        var blob = repo.ObjectDatabase.CreateBlob(stream);
    }
}

Console.WriteLine("Hello, World!");

Expected behavior

The code works on .NET 8 and .NET 9

Actual behavior

  • No error on .NET 8
  • Fails on .NET 9 preview 6

Regression?

Yes as it works on .NET 8

Known Workarounds

No response

Configuration

.NET SDK:
 Version:           9.0.100-preview.6.24328.19
 Commit:            ef4c241666
 Workload version:  9.0.100-manifests.7e18eb25
 MSBuild version:   17.11.0-preview-24318-05+4a45d5633

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.22635
 OS Platform: Windows
 RID:         win-x64
 Base Path:   C:\Program Files\dotnet\sdk\9.0.100-preview.6.24328.19\

.NET workloads installed:
Configured to use loose manifests when installing new manifests.
 [aspire]
   Installation Source: SDK 9.0.100-preview.6, VS 17.11.35103.136
   Manifest Version:    8.0.2/8.0.100
   Manifest Path:       C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.aspire\8.0.2\WorkloadManifest.json
   Install Type:              Msi


Host:
  Version:      9.0.0-preview.6.24327.7
  Architecture: x64
  Commit:       static

.NET SDKs installed:
  8.0.107 [C:\Program Files\dotnet\sdk]
  8.0.206 [C:\Program Files\dotnet\sdk]
  8.0.303 [C:\Program Files\dotnet\sdk]
  8.0.400-preview.0.24324.5 [C:\Program Files\dotnet\sdk]
  9.0.100-preview.6.24328.19 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 9.0.0-preview.6.24328.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.30 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.32 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 9.0.0-preview.6.24327.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 6.0.30 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.32 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 8.0.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 8.0.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 8.0.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 9.0.0-preview.6.24327.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
  x86   [C:\Program Files (x86)\dotnet]
    registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
  Not set

global.json file:
  Not found

Other information

No response

@ghost ghost added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jul 14, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jul 14, 2024
@MichalPetryka
Copy link
Contributor

It's probably caused by libgit2sharp having a gchole here in the implicit operator on a class that has a finalizer which just returns a pointer that doesn't keep the class alive.

In general as far as I've seen, the libgit2sharp sharp bindings were not great, while looking around at the code here I for example noticed another bug where their EncodingMarshaller doesn't properly account for encodings that uses more than 1B for null terminator.

@EgorBo
Copy link
Member

EgorBo commented Jul 14, 2024

There were other AV-like complains on libgit2sharp e.g. #101479 (comment), #53990 and, probably, #102313. From a quick look it indeed feels like it could be related to finalizer and lack of SafeHandle usage.

@meziantou
Copy link
Contributor Author

I'm aware of multiple issues with libgit2sharp in the past :( As this issue only occurs with .NET 9, I was wondering if there is a regression in .NET 9. If this is not the case, feel free to close the issue.

@EgorBo
Copy link
Member

EgorBo commented Jul 14, 2024

I'm aware of multiple issues with libgit2sharp in the past :( As this issue only occurs with .NET 9, I was wondering if there is a regression in .NET 9. If this is not the case, feel free to close the issue.

If it is what we think it is (here), then it could be just an internal change in GC or JIT that exposes a bug in the library (or exposes it more often now)

@EgorBo
Copy link
Member

EgorBo commented Jul 14, 2024

Although, it's possible something is wrong with the custom marshaller over git_blob_create_from_stream (StrictUtf8Marshaler). E.g. the issue doesn't repro if I rewrite this pinvoke to LibraryImporter:

        [LibraryImport(libgit2, StringMarshalling = StringMarshalling.Utf8)]
        internal static unsafe partial int git_blob_create_from_stream(
            out IntPtr stream,
            git_repository* repositoryPtr,
            string hintpath);

@jeffschwMSFT jeffschwMSFT added question Answer questions and provide assistance, not an issue with source code or documentation. area-Interop-coreclr labels Jul 15, 2024
@jkotas jkotas removed the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jul 15, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.

@stephentoub stephentoub added this to the 9.0.0 milestone Jul 22, 2024
@jkoritzinsky jkoritzinsky removed the untriaged New issue has not been triaged by the area owner label Jul 22, 2024
@jkoritzinsky
Copy link
Member

I'm going to close this in favor of the LibGit2Sharp maintainers fixing the lifetime management in libgit2sharp as mentioned above.

@github-actions github-actions bot locked and limited conversation to collaborators Aug 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Runtime.InteropServices question Answer questions and provide assistance, not an issue with source code or documentation.
Projects
Archived in project
Development

No branches or pull requests

8 participants