Skip to content

Don't unload MsQuic from the process - #75441

Merged
jkotas merged 3 commits into
dotnet:mainfrom
rzikm:dont-unload-msquic
Sep 13, 2022
Merged

Don't unload MsQuic from the process#75441
jkotas merged 3 commits into
dotnet:mainfrom
rzikm:dont-unload-msquic

Conversation

@rzikm

@rzikmrzikm commented Sep 12, 2022

Copy link
Copy Markdown
Member

Since MsQuic library loads libmsquic.lttng.so on Linuxes it is weird to unload just the library and not the dependent libraries as well, and since we cannot touch libmsquic.lttng.so from .NET, it seems better to keep libmsquic loaded in memory to prevent any weird behavior by having the library only partially loaded.

Note that this change does not negate the benefit of #74749, the threads are still being stopped and deallocated in the MsQuicClose call.

Related:

Fixes#74629

cc: @stephentoub, @jkotas

@ghostghost assigned rzikmSep 12, 2022
@ghost

Copy link
Copy Markdown

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Issue Details

Since MsQuic library loads libmsquic.lttng.so on Linuxes it is weird to unload just the library and not the dependent libraries as well, and since we cannot touch libmsquic.lttng.so from .NET, it seems better to keep libmsquic loaded in memory to prevent any weird behavior by having the library only partially loaded.

Related:

cc: @stepehtoub, @jkotas

Author:rzikm
Assignees:-
Labels:

area-System.Net.Quic

Milestone:-

@rzikm
rzikm requested a review from a teamSeptember 12, 2022 09:07

@ManickaPManickaP left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, small comment to consider.

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 IntPtr TryLoadMsQuic() =>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should this still be named Try...? Since it doesn't return bool and out IntPtr anymore.


private static readonly Version MsQuicVersion = new Version(2, 1);

private static readonly IntPtr MsQuicHandle = TryLoadMsQuic();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If we were to keep the original shape of TryLoadMsQuic, this could be done as: TryLoadMsQuic(out IntPtr handle) ? handle : IntPtr.Zero

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

hmm, that could work, I originally wanted to set it from the static ctor using the old signature and compiler complained with Initialize all static fields in 'MsQuicApi' when those fields are declared and remove the explicit static constructor


apiTable = null;
if (!NativeLibrary.TryGetExport(msQuicHandle, "MsQuicOpenVersion", out IntPtr msQuicOpenVersionAddress))
if (!NativeLibrary.TryGetExport(MsQuicHandle, "MsQuicOpenVersion", out IntPtr msQuicOpenVersionAddress))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this should be NativeLibrary.GetExport.

The failures to get exports from the MsQuic library should be fatal. If we fail to get the export, it means that there is something very wrong.

@karelzkarelz added this to the 8.0.0 milestone Sep 12, 2022
Comment on lines +65 to +69
if (MsQuicHandle == IntPtr.Zero)
{
// MsQuic library not loaded
return;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
if(MsQuicHandle==IntPtr.Zero)
{
// MsQuic library not loaded
return;
}
if(!TryLoadMsQuic(outMsQuicHandle))
{
// MsQuic library not loaded
return;
}

and delete inline initialization of MsQuicHandle above

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Instead of storing MsQuicHandle, you can consider storing the pointers to the two exports in statics instead. It would save a bit of redundant work on the re-initialization.

}

private static bool TryCloseMsQuic(IntPtr msQuicHandle, QUIC_API_TABLE* apiTable)
private static bool TryCloseMsQuic(QUIC_API_TABLE* apiTable)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This can return void if you incorporate my other feedback

@jkotasjkotas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@jkotas
jkotas merged commit 6214022 into dotnet:mainSep 13, 2022
@wfurtwfurt mentioned this pull request Sep 13, 2022
rzikm added a commit to rzikm/dotnet-runtime that referenced this pull request Sep 13, 2022
carlossanlop pushed a commit that referenced this pull request Sep 23, 2022
…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 <radekzikmund@microsoft.com>
* Don't unload MsQuic from the process (#75441)
* Revert helix queues change (to be done in another PR)
* Code review feedback
@ghostghost locked as resolved and limited conversation to collaborators Oct 13, 2022
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HttpClient creates Quic threads even if HTTP/3 is not in use

4 participants

@rzikm@jkotas@ManickaP@karelz