Skip to content
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

Improve diagnostics when allocating MsQuicApi #74985

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static MsQuicApi()

try
{
if (!TryOpenMsQuic(msQuicHandle, out QUIC_API_TABLE* apiTable))
if (!TryOpenMsQuic(msQuicHandle, out QUIC_API_TABLE* apiTable, out _))
{
return;
}
Expand Down Expand Up @@ -136,31 +136,48 @@ private static MsQuicApi AllocateMsQuicApi()
{
Debug.Assert(IsQuicSupported);

int openStatus = MsQuic.QUIC_STATUS_INTERNAL_ERROR;

if (TryLoadMsQuic(out IntPtr msQuicHandle) &&
TryOpenMsQuic(msQuicHandle, out QUIC_API_TABLE* apiTable))
TryOpenMsQuic(msQuicHandle, out QUIC_API_TABLE* apiTable, out openStatus))
{
return new MsQuicApi(apiTable);
}

ThrowHelper.ThrowIfMsQuicError(openStatus);

// this should unreachable as TryOpenMsQuic returns non-success status on failure
throw new Exception("Failed to create MsQuicApi instance");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you were to invert this condition, you wouldn't need the unreachable throw:

if (!TryLoadMsQuic(out IntPtr msQuicHandle) || !TryOpenMsQuic(msQuicHandle, out QUIC_API_TABLE* apiTable, out openStatus))
{
    ThrowHelper.ThrowIfMsQuicError(openStatus);
}

return new MsQuicApi(apiTable);

You could also have a ThrowMsQuicError that was unconditional. Or just use throw GetExceptionForMsQuicStatus(openStatus);.

}

private static bool TryLoadMsQuic(out IntPtr msQuicHandle) =>
NativeLibrary.TryLoad($"{Interop.Libraries.MsQuic}.{MsQuicVersion.Major}", typeof(MsQuicApi).Assembly, DllImportSearchPath.AssemblyDirectory, out msQuicHandle) ||
NativeLibrary.TryLoad(Interop.Libraries.MsQuic, typeof(MsQuicApi).Assembly, DllImportSearchPath.AssemblyDirectory, out msQuicHandle);

private static bool TryOpenMsQuic(IntPtr msQuicHandle, out QUIC_API_TABLE* apiTable)
private static bool TryOpenMsQuic(IntPtr msQuicHandle, out QUIC_API_TABLE* apiTable, out int openStatus)
{
apiTable = null;
if (!NativeLibrary.TryGetExport(msQuicHandle, "MsQuicOpenVersion", out IntPtr msQuicOpenVersionAddress))
{
if (NetEventSource.Log.IsEnabled())
{
NetEventSource.Info(null, "Failed to get MsQuicOpenVersion export in msquic library.");
}

openStatus = MsQuic.QUIC_STATUS_NOT_FOUND;
return false;
}

QUIC_API_TABLE* table = null;
delegate* unmanaged[Cdecl]<uint, QUIC_API_TABLE**, int> msQuicOpenVersion = (delegate* unmanaged[Cdecl]<uint, QUIC_API_TABLE**, int>)msQuicOpenVersionAddress;
if (StatusFailed(msQuicOpenVersion((uint)MsQuicVersion.Major, &table)))
openStatus = msQuicOpenVersion((uint)MsQuicVersion.Major, &table);
if (StatusFailed(openStatus))
{
if (NetEventSource.Log.IsEnabled())
{
NetEventSource.Info(null, $"MsQuicOpenVersion returned {openStatus} status code.");
}

return false;
}

Expand Down