Skip to content

Commit

Permalink
Fix double Dispose
Browse files Browse the repository at this point in the history
  • Loading branch information
mayuki committed Jan 10, 2024
1 parent 6fa47c3 commit c883625
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/YetAnotherHttpHandler/RequestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ internal class RequestContext : IDisposable
private readonly CancellationTokenSource _cancellationTokenSource;
private readonly int _requestSequence;
private readonly object _handleLock = new object();
private readonly ManualResetEventSlim _fullyCompleted = new ManualResetEventSlim(false);
private GCHandle _handle;

internal YahaContextSafeHandle _ctxHandle;
Expand Down Expand Up @@ -63,6 +64,7 @@ public void Release()
if (YahaEventSource.Log.IsEnabled()) YahaEventSource.Log.Trace($"[ReqSeq:{_requestSequence}:State:0x{Handle:X}] Releasing state");
_handle.Free();
_handle = default;
_fullyCompleted.Set();
}
}

Expand Down Expand Up @@ -273,8 +275,6 @@ private void Dispose(bool disposing)
{
_cancellationTokenSource.Cancel();
_cancellationTokenSource.Dispose();

_requestContextHandle.Dispose();
// DO NOT Dispose `_ctx` here.
}
else
Expand All @@ -283,10 +283,7 @@ private void Dispose(bool disposing)
// NOTE: Waits by blocking until the request is completed on the native side.
// If not waited here, issues such as crashes may occur when callbacks are invoked after the .NET side is destroyed by Unity's Domain Reload.
// However, caution is needed with the invocation order and timing of callbacks, as well as the handling of locks, since the finalizer thread may become blocked.
while (_handle.IsAllocated)
{
Thread.Sleep(100);
}
_fullyCompleted.Wait();
}
}
}
Expand Down

0 comments on commit c883625

Please sign in to comment.