private Interop.Error CloseHandle(IntPtr handle)
{
Interop.Error errorCode = Interop.Error.SUCCESS;
bool remappedError = false;
if (Interop.Sys.Close(handle) != 0)
{
errorCode = Interop.Sys.GetLastError();
if (errorCode == Interop.Error.ECONNRESET)
{
// Some Unix platforms (e.g. FreeBSD) non-compliantly return ECONNRESET from close().
// For our purposes, we want to ignore such a "failure" and treat it as success.
// In such a case, the file descriptor was still closed and there's no corrective
// action to take.
errorCode = Interop.Error.SUCCESS;
remappedError = true;
}
}
if (NetEventSource.IsEnabled)
{
NetEventSource.Info(this, remappedError ?
$"handle:{handle}, close():ECONNRESET, but treating it as SUCCESS" :
$"handle:{handle}, close():{errorCode}");
}
#if DEBUG
_closeSocketHandle = handle;
_closeSocketResult = SocketPal.GetSocketErrorForErrorCode(errorCode);
#endif
return(errorCode);
}