Bug 321343 (MONO78615) - mono_thread_attach/mono_thread_detach causes mach port leaks on OS X
Summary: mono_thread_attach/mono_thread_detach causes mach port leaks on OS X
Status: RESOLVED MOVED
Alias: MONO78615
Product: Mono: Runtime
Classification: Mono
Component: misc (show other bugs)
Version: 1.1
Hardware: Other Other
: P3 - Medium : Normal
Target Milestone: ---
Assignee: Mono Bugs
QA Contact: Mono Bugs
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-06-09 04:19 UTC by Allan Hsu
Modified: 2007-09-15 21:24 UTC (History)
1 user (show)

See Also:
Found By: ---
Services Priority:
Business Priority:
Blocker: ---
Marketing QA Status: ---
IT Deployment: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Wiest 2007-09-15 20:01:09 UTC


---- Reported by allan@imeem.com 2006-06-08 21:19:51 MST ----

Please fill in this template when reporting a bug, unless you know what you are doing.
Description of Problem:

Use of mono_thread_attach() and mono_thread_detach() causes a mach port leak on OS X.

Steps to reproduce the problem:
1. Here is code to reproduce. This is a slightly altered version of the racy code from https://bugzilla.novell.com/show_bug.cgi?id=MONO77470, 
tweaked to reduce CPU usage and avoid most instances of the segfault/deadlock described in 
#77470, while still reproducing the port leak:

#include <pthread.h>
#include <mono/jit/jit.h>
#include <mono/metadata/metadata.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/metadata/object.h>
#include <mono/metadata/threads.h>
#include <mono/metadata/appdomain.h>

#define CHUNK_THREADCOUNT 32

static void *thread_function(void *monoDomain) {
        printf("Attaching thread...\n");
        MonoThread *monoThread = mono_thread_attach((MonoDomain *)monoDomain);
        printf("Thread (%p) attached.\n", monoThread);

        //normally, some threaded stuff would happen here.

        printf("Detaching thread (%p)\n", monoThread);
        mono_thread_detach(monoThread);
        printf("Thread (%p) detached.\n", monoThread);
}

int main(int argc, char *argv[]) {

        mono_set_defaults(0, mono_parse_default_optimizations(NULL));
        MonoDomain *monoDomain = mono_jit_init("leaky");

        while(TRUE) {
                pthread_t threads[CHUNK_THREADCOUNT];
                int i;

                for(i = 0; i < CHUNK_THREADCOUNT; i++)
                        pthread_create(threads + i, NULL, thread_function, monoDomain);


                for(i = 0; i < CHUNK_THREADCOUNT; i++) {
                        pthread_join(threads[i], NULL);
                        pthread_detach(threads[i]);
                }

                sleep(1);
        }

        return(0);
}

This code (and a simple build script) is available at http://blargle.com/~allan/leaky.tar.bz2

Actual Results:

If you use Activity Monitor to inspect the application, you can see the ports count under the 
"Statistics" tab. It will rise until the process is killed.

Expected Results:

The ports count should stay relatively stable, occasionally fluctuating around a low, sane 
number, if at all.

How often does this happen? 

Every time.

Additional Information:

If you remove the mono_thread_attach() and mono_thread_detach() calls from the sample code, 
the ports count will be stable for the life of the process.



---- Additional Comments From allan@imeem.com 2006-06-08 21:42:28 MST ----

If you run the sample code for long enough (removing the sleep will make this happen 
faster), the process will eventually die with this error:

thread_terminate(mach_thread_self()) failed: (ipc/send) invalid destination port

I assume this happens because the process hits the limit for ports.



---- Additional Comments From allan@imeem.com 2006-06-12 18:34:21 MST ----



*** This bug has been marked as a duplicate of https://bugzilla.novell.com/show_bug.cgi?id=MONO78628 ***


Unknown bug field "cf_op_sys_details" encountered while moving bug
   <cf_op_sys_details>OS X 10.4.6 on both PPC and x86</cf_op_sys_details>
Unknown operating system unknown. Setting to default OS "Other".
This bug was marked DUPLICATE in the database it was moved from.
    Changing resolution to "MOVED"