Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/Servers/IIS/IIS/src/Core/IISHttpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void Dispose()
_disposed = true;

// Block any more calls into managed from native as we are unloading.
_nativeApplication.StopCallsIntoManaged();
_nativeApplication.Stop();
_shutdownSignal.TrySetResult();

if (_httpServerHandle.IsAllocated)
Expand All @@ -141,7 +141,6 @@ public void Dispose()
}

_memoryPool.Dispose();
_nativeApplication.Dispose();
}

[UnmanagedCallersOnly]
Expand Down Expand Up @@ -261,7 +260,7 @@ private static void OnRequestsDrained(IntPtr serverContext)
return;
}

server._nativeApplication.StopCallsIntoManaged();
server._nativeApplication.Stop();
server._shutdownSignal.TrySetResult();
server._cancellationTokenRegistration.Dispose();
}
Expand Down
28 changes: 14 additions & 14 deletions src/Servers/IIS/IIS/src/Core/IISNativeApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Microsoft.AspNetCore.Server.IIS.Core;
internal sealed class IISNativeApplication
{
private readonly NativeSafeHandle _nativeApplication;
private bool _hasRegisteredCallbacks;
private readonly object _sync = new object();

public IISNativeApplication(NativeSafeHandle nativeApplication)
Expand All @@ -24,14 +25,22 @@ public void StopIncomingRequests()
}
}

public void StopCallsIntoManaged()
public void Stop()
{
lock (_sync)
{
if (!_nativeApplication.IsInvalid)
if (_nativeApplication.IsInvalid)
{
return;
}

if (_hasRegisteredCallbacks)
{
NativeMethods.HttpStopCallsIntoManaged(_nativeApplication);
}

_nativeApplication.Dispose();
GC.SuppressFinalize(this);
}
}

Expand All @@ -44,6 +53,8 @@ public unsafe void RegisterCallbacks(
IntPtr pvRequestContext,
IntPtr pvShutdownContext)
{
_hasRegisteredCallbacks = true;

NativeMethods.HttpRegisterCallbacks(
_nativeApplication,
requestCallback,
Expand All @@ -55,20 +66,9 @@ public unsafe void RegisterCallbacks(
pvShutdownContext);
}

public void Dispose()
{
lock (_sync)
{
GC.SuppressFinalize(this);

// Don't need to await here because pinvokes should never been called after disposing the safe handle.
_nativeApplication.Dispose();
}
}

~IISNativeApplication()
{
// If this finalize is invoked, try our best to block all calls into managed.
StopCallsIntoManaged();
Stop();
}
}
Loading