[threadpool] Fix `MonoTests.runtime.unload-appdomain-on-shutdown.exe` (#4135)
[mono.git] / mono / metadata / threadpool.c
index ab88e67f4b960c6de4e6d2e52e3f38edd3cd7b29..9b404869ea436ca4de3ea43e0596c4aee45d0d2e 100644 (file)
@@ -607,12 +607,18 @@ mono_threadpool_remove_domain_jobs (MonoDomain *domain, int timeout)
         * The is_unloading () check in worker_request () ensures that
         * no new jobs are added after we enter the lock below.
         */
-       mono_lazy_initialize (&status, initialize);
+
+       if (!mono_lazy_is_initialized (&status))
+               return TRUE;
+
+       mono_refcount_inc (threadpool);
+
        domains_lock ();
 
        tpdomain = tpdomain_get (domain, FALSE);
        if (!tpdomain) {
                domains_unlock ();
+               mono_refcount_dec (threadpool);
                return TRUE;
        }
 
@@ -647,6 +653,8 @@ mono_threadpool_remove_domain_jobs (MonoDomain *domain, int timeout)
        mono_coop_cond_destroy (&tpdomain->cleanup_cond);
        tpdomain_free (tpdomain);
 
+       mono_refcount_dec (threadpool);
+
        return ret;
 }