-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Unload MsQuic after checking for QUIC support to free resources. #74749
Unload MsQuic after checking for QUIC support to free resources. #74749
Conversation
Tagging subscribers to this area: @dotnet/ncl Issue DetailsFixes #74629. This PR unloads MsQuic library from the process after we check whether it is supported. This saves some resources when HTTP3 is not used (e.g. the server does not advertise HTTP3)
|
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); | ||
|
||
internal static bool TryOpenMsQuic(IntPtr msQuicHandle, out QUIC_API_TABLE* apiTable) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could alternatively use [LibraryImport] for MsQuicOpenVersion
and MsQuicClose
, it might clean up the code a bit at the cost of not being able to unload the lib from the process I think (if we care about that)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They already have imports in:
runtime/src/libraries/System.Net.Quic/src/System/Net/Quic/Interop/msquic_generated.cs
Line 2593 in bf47ac0
internal static extern int MsQuicOpenVersion([NativeTypeName("uint32_t")] uint Version, [NativeTypeName("const void **")] void** QuicApi); |
I'm not an expert on this, but we control which version of libmsquic we're loading in TryLoadMsQuic
and we do an attempt to load it that would not fail if the lib is not present. I'm not sure we can achieve the same with the import. Relevant history: #49973
Anyway, we might at least use nameof(MsQuicOpenVersion)
from the generated interop. Though we lived with the string until now, so it's certainly not critical.
Are the threads started by opening MsQuic: |
It is the MsQuicOpenVersion and MsQuicClose calls that change the number of threads visible. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, modulo some nits.
@@ -47,7 +48,8 @@ private MsQuicApi(QUIC_API_TABLE* apiTable) | |||
} | |||
} | |||
|
|||
internal static MsQuicApi Api { get; } = null!; | |||
internal static Lazy<MsQuicApi> _api = new Lazy<MsQuicApi>(AllocateMsQuicApi); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private readonly?
{ | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
// check version |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uber nitty nit 😄
// check version | |
// Check version |
throw new Exception("Failed to create MsQuicApi instance"); | ||
} | ||
|
||
internal static bool TryLoadMsQuic(out IntPtr msQuicHandle) => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should those be private?
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); | ||
|
||
internal static bool TryOpenMsQuic(IntPtr msQuicHandle, out QUIC_API_TABLE* apiTable) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They already have imports in:
runtime/src/libraries/System.Net.Quic/src/System/Net/Quic/Interop/msquic_generated.cs
Line 2593 in bf47ac0
internal static extern int MsQuicOpenVersion([NativeTypeName("uint32_t")] uint Version, [NativeTypeName("const void **")] void** QuicApi); |
I'm not an expert on this, but we control which version of libmsquic we're loading in TryLoadMsQuic
and we do an attempt to load it that would not fail if the lib is not present. I'm not sure we can achieve the same with the import. Relevant history: #49973
Anyway, we might at least use nameof(MsQuicOpenVersion)
from the generated interop. Though we lived with the string until now, so it's certainly not critical.
/backport to release/7.0 |
Started backporting to release/7.0: https://github.com/dotnet/runtime/actions/runs/2972591441 |
…es. (dotnet#74749)" This reverts commit 7a45201.
Reverted by #74984 |
* Revert "Revert "Unload MsQuic after checking for QUIC support to free resources. (#74749)" (#74984)" This reverts commit 953f524. * update helix images * update helix images * Improve diagnostics when opening MsQuic Co-authored-by: Radek Zikmund <[email protected]>
…et#75163) * Revert "Revert "Unload MsQuic after checking for QUIC support to free resources. (dotnet#74749)" (dotnet#74984)" This reverts commit 953f524. * update helix images * update helix images * Improve diagnostics when opening MsQuic Co-authored-by: Radek Zikmund <[email protected]>
…et#75163) * Revert "Revert "Unload MsQuic after checking for QUIC support to free resources. (dotnet#74749)" (dotnet#74984)" This reverts commit 953f524. * update helix images * update helix images * Improve diagnostics when opening MsQuic Co-authored-by: Radek Zikmund <[email protected]>
…sources (#75163, #75441) (#75521) * Unload MsQuic after checking for QUIC support to free resources (#75163) * Revert "Revert "Unload MsQuic after checking for QUIC support to free resources. (#74749)" (#74984)" This reverts commit 953f524. * update helix images * update helix images * Improve diagnostics when opening MsQuic Co-authored-by: Radek Zikmund <[email protected]> * Don't unload MsQuic from the process (#75441) * Revert helix queues change (to be done in another PR) * Code review feedback
Fixes #74629.
This PR unloads MsQuic library from the process after we check whether it is supported. This saves some resources when HTTP3 is not used (e.g. the server does not advertise HTTP3)
Test app:
Results on Windows11