Skip to content

Memory leaks when reverse P/Invoke is called from unmanaged thread #64509

Closed

Description

@AkiSakurai

Description

The function below is creating native threads to call managed function.
The memory consumption is increasing steadily.

using System.Runtime.InteropServices;

namespace Leak {
    class Leak
    {
        [DllImport("libpthread.so.0")]
        public static extern int pthread_create(out nuint thread, IntPtr attr, IntPtr start_routine, IntPtr arg);
        
        [DllImport("libpthread.so.0")]
        public static extern int pthread_join(nuint thread, IntPtr retval);

        [UnmanagedCallersOnly]
        static private IntPtr Callback(IntPtr arg)
        {
            return IntPtr.Zero;
        }

        static void Main(string[] args)
        {
            unsafe {
                var count = 0;
                while(true) 
                {
                    var n = 2000;
                    for(int i = 0; i < n ; i++) 
                    {
                        nuint thread;
                        delegate* unmanaged <IntPtr, IntPtr> fp = &Callback;
                        pthread_create(out thread, IntPtr.Zero, (IntPtr)fp, IntPtr.Zero);
                        pthread_join(thread, IntPtr.Zero);
                    }
                    count += n;
                    Console.WriteLine($"{count} calls");
                    GC.Collect();
                }
            }
        }
    }
}

Configuration

Ubuntu 21.10
dotnet core 6.0.1

Data

leak

Activity

  1. added
    tenet-performance
    on Jan 30, 2022
  2. ghost added
    untriaged
    on Jan 30, 2022
  3. changed the title [-]Memory leaks when reverse P/Invoke is called from unmanged thread.[/-] [+]Memory leaks when reverse P/Invoke is called from unmanged thread[/+] on Jan 30, 2022
  4. jkotas commented on Jan 30, 2022

    @jkotas
    Member

    This looks like a regression introduced by #43423 . Potential duplicate of #60566.

    @janvorli root caused it in #43423 (comment) .

    We need to explicitly touch tls_destructionMonitor when the new thread gets attached to the runtime, similar to how it is done in NativeAOT

    tls_destructionMonitor.SetThread(thread);
    .

    @VSadov We will need to get a fix for this pushed to .NET 6 servicing. Could you please take care of it?

  5. removed
    untriaged
    on Jan 30, 2022
  6. added this to the 7.0.0 milestone on Jan 30, 2022
  7. VSadov commented on Jan 31, 2022

    @VSadov
    Member

    Will take a look. Yes, most likely touching tls_destructionMonitor will fix this.

  8. changed the title [-]Memory leaks when reverse P/Invoke is called from unmanged thread[/-] [+]Memory leaks when reverse P/Invoke is called from unmanaged thread[/+] on Feb 7, 2022
  9. ghost added
    in-pr
    on Feb 15, 2022
  10. ghost removed
    in-pr
    on Feb 15, 2022
  11. ghost added
    in-pr
    on Feb 15, 2022
  12. ghost removed
    in-pr
    on Mar 8, 2022
  13. VSadov commented on Mar 9, 2022

    @VSadov
    Member

    Fixed in #65406

  14. ghost locked as resolved and limited conversation to collaborators on Apr 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions