[corlib] Fix potential RegisteredWaitHandle resource leak
authorLudovic Henry <ludovic@xamarin.com>
Tue, 16 Feb 2016 20:46:23 +0000 (20:46 +0000)
committerLudovic Henry <ludovic@xamarin.com>
Wed, 17 Feb 2016 10:54:49 +0000 (10:54 +0000)
commit920ec35630785d877f22aaf27099a1c6054af874
tree132fa4703dcc8ec7b997f2340a9a139bd1a4c68b
parent8b36e3afb7b8c78462e30a8371b48e6849abbc3d
[corlib] Fix potential RegisteredWaitHandle resource leak

If we construct RegisteredWaitHandle with executeOnlyOnce = true, and never call Unregister, we will keep _waitObject.SafeWaitHandle reference count unbalanced. This will lead to never releasing the waitObject handle.

As we only need to ensure that the waitObject handle is alive while in the Wait method, we use the SafeHandle mechanism only there. The 2 possibles scenarios in the Wait method are:
 - waitObject has not been closed: we increment its ref count, so we ensure that it's alive as long as we use it here
 - waitObject has been closed: a ObjectDisposedException is thrown, we catch it and keep on without trying to wait on the handle
mcs/class/corlib/System.Threading/RegisteredWaitHandle.cs