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

[System.Net.Quic]Release configuration resource. #53857

Merged
merged 1 commit into from
Jun 9, 2021

Conversation

shuxinqin
Copy link
Contributor

Release configuration resource to prevent memory leaks.

@ghost
Copy link

ghost commented Jun 8, 2021

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

Issue Details

Release configuration resource to prevent memory leaks.

Author: shuxinqin
Assignees: -
Labels:

area-System.Net

Milestone: -

@ghost
Copy link

ghost commented Jun 9, 2021

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

Issue Details

Release configuration resource to prevent memory leaks.

Author: shuxinqin
Assignees: -
Labels:

area-System.Net.Quic

Milestone: -

@ManickaP
Copy link
Member

ManickaP commented Jun 9, 2021

Hi @shuxinqin,

Thank you for the contribution!

This needs to be locally tested with msquic before merging since we don't have a CI set up for it yet. Do you think that's something you could do? I assume you were able to build msquic and use it with runtime. Or is this something you'd rather leave to us?

@shuxinqin
Copy link
Contributor Author

I downloaded the source code(System.Net.Quic) and change some codes to make it run in unity(netstandard2.0). If the resource is not released correctly when Application quiting, the unity editor will be stuck. So i have to release configuration resource when MsQuicConnection.Dispose() called, then the problem was solved.
ps: My changes depend on msquic1.3.0.

@wfurt
Copy link
Member

wfurt commented Jun 9, 2021

I did some test runs with the change @ManickaP and it looks ok to me e.g. I did not see any side effects. I will need to figure out how to integrate with #52800.

Copy link
Member

@wfurt wfurt left a comment

Choose a reason for hiding this comment

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

LGTM

@ManickaP ManickaP merged commit 29354ef into dotnet:main Jun 9, 2021
@shuxinqin
Copy link
Contributor Author

@ManickaP @wfurt
Feedback:
Hi, thanks for accept my pr.
I did not see any code which release the MsQuicApi.Registration(https://github.com/dotnet/runtime/blob/main/src/libraries/System.Net.Quic/src/System/Net/Quic/Implementations/MsQuic/Internal/MsQuicApi.cs#L12) resource in System.Net.Quic project. I'm not sure if there will be a memory leak after the program exits.
In fact, to prevent the unity editor will be not stuck, i release configuration resource when MsQuicConnection was disposed, in addition i release MsQuicApi.Registration resource when Application quitting as follows:

public static class MsQuicInterop
{
    /// <summary>
    /// Releases Registration.
    /// </summary>
    public static void Free()
    {
        if (MsQuicApi.Api == null)
            return;

        if (MsQuicApi.Api.Registration == null)
            return;

        if (!MsQuicApi.Api.Registration.IsClosed)
        {
            MsQuicApi.Api.Registration.Close();
        }

        MsQuicApi.Api?.Registration?.Dispose();
    }
}

Calls Free() method when application quitting:

Application.quitting += () =>
{
    Debug.Log("MsQuicInterop.Free...");
    MsQuicInterop.Free(); //Unity editor will be stuck if `Free()` method is not called.
};

@ManickaP
Copy link
Member

Thanks for bringing this up.
If the process ends, all the resource will get released by the OS, there's no need to do that manually if you know the app will finish soon. And since we hold MsQuicApi as a singleton, living through the whole process lifetime, we do not need to dispose the registration (it needs to be kept alive while all its configurations and connections are still alive).

I don't think we have (or at least I'm not aware of) any plans to change the lifetime management of MsQuicApi. Any thoughts @scalablecory @geoffkizer ?

@shuxinqin
Copy link
Contributor Author

I think it should be unity editor problem, thanks!

@scalablecory
Copy link
Contributor

Thanks for bringing this up.

If the process ends, all the resource will get released by the OS, there's no need to do that manually if you know the app will finish soon. And since we hold MsQuicApi as a singleton, living through the whole process lifetime, we do not need to dispose the registration (it needs to be kept alive while all its configurations and connections are still alive).

I don't think we have (or at least I'm not aware of) any plans to change the lifetime management of MsQuicApi. Any thoughts @scalablecory @geoffkizer ?

@jeffschwMSFT is it possible to load/unload .NET from a process? This is the only danger I see.

@jeffschwMSFT
Copy link
Member

@jeffschwMSFT is it possible to load/unload .NET from a process? This is the only danger I see.

.NET is not unloaded from a process.

@ghost ghost locked as resolved and limited conversation to collaborators Jul 11, 2021
@karelz karelz added this to the 6.0.0 milestone Jul 15, 2021
Sign up for free to 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.

6 participants