Skip to content

Commit

Permalink
fix: Fixes libdeflate threading issue
Browse files Browse the repository at this point in the history
  • Loading branch information
kamronbatman committed Jun 2, 2024
1 parent 5f8f798 commit 232b00a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
14 changes: 3 additions & 11 deletions Projects/Server/Compression/Deflate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,8 @@ namespace Server.Compression;

public static class Deflate
{
public static LibDeflateBinding Standard { get; }
[ThreadStatic]
private static LibDeflateBinding _standard;

static Deflate()
{
Standard = new LibDeflateBinding();
AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
}

private static void CurrentDomain_ProcessExit(object sender, EventArgs e)
{
Standard.Dispose();
}
public static LibDeflateBinding Standard => _standard ??= new LibDeflateBinding();
}
5 changes: 5 additions & 0 deletions Projects/Server/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Server.Compression;
using Server.Json;
using Server.Logging;
using Server.Network;
Expand Down Expand Up @@ -465,6 +466,10 @@ public static void Setup(bool profiling, Assembly applicationAssembly, Process p

Console.CancelKeyPress += Console_CancelKeyPressed;

// LibDeflate is not thread safe, so we need to create a new instance for each thread
var standard = Deflate.Standard;
AppDomain.CurrentDomain.ProcessExit += (_, _) => standard.Dispose();

ServerConfiguration.Load();

logger.Information("Running on {Framework}", RuntimeInformation.FrameworkDescription);
Expand Down

0 comments on commit 232b00a

Please sign in to comment.