fsharp/fsharp — pull request #333 by pepeiborra (2014-09-24 21:27:47+00:00)
Title: Update control.fs

Fix a race condition causing a memory leak.

Reference update is not an atomic operation, so it can happen that the wait callback runs before the current thread has a a chance to write to rwh. The callback will set rwh to None, and at some point later the main thread will set it with the RegisteredWaitHandle object. Now the callback holds a reference to rwh and you have a memory leak.
To see why, check the docs for RegisteredWaitHandle. Unless the handle is Unregistered, the WaitCallback is kept alive by the thread pool until the RegisteredWaitHandle finaliser runs. But this will never happen, because the WaitCallback itself keeps it alive.