Avoid race condition between domain unload and threadpool. (#3491)
authorJonathan Chambers <joncham@gmail.com>
Thu, 8 Sep 2016 12:28:59 +0000 (08:28 -0400)
committerLudovic Henry <ludovic@xamarin.com>
Thu, 8 Sep 2016 12:28:59 +0000 (14:28 +0200)
commit12d1d63b8a42b3df87d232104fbe55e86f8db20f
tree59cfa597d2feafa7c03b28a9b8cdbb22396ea3ac
parent43ffa41898f305515f2327eb800e5c94cbabc10c
Avoid race condition between domain unload and threadpool. (#3491)

* Avoid race condition between domain unload and threadpool.

Previous logic only ensured that domain->threadpool_jobs was 0.
There was a race condition as follows:
* worker jobs are queued into ThreadPoolDomain outstanding_request's
* domain unload starts
* domain->threadpool_jobs is 0 so unload proceeds
* queued jobs are processed from the ThreadPoolDomain outstanding_request's
* the domain can be set/entered by worker threads since
  mono_domain_set only fails for domains with state of MONO_APPDOMAIN_UNLOADED,
  while our domain is in the MONO_APPDOMAIN_UNLOADING state
* jobs are now being unexpectedly run in domain that is being shutdown

* Fix a hang when unloading the domain.

This change applies the diff from ludovic-henry to correct the hang.

https://gist.github.com/ludovic-henry/6c8a4bc8951091c0ef36df2e52d5e32e
mono/metadata/threadpool-ms.c