{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/corefx/issues/2900","labels_url":"https://api.github.com/repos/dotnet/corefx/issues/2900/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/corefx/issues/2900/comments","events_url":"https://api.github.com/repos/dotnet/corefx/issues/2900/events","html_url":"https://github.com/dotnet/corefx/pull/2900","id":102179571,"number":2900,"title":"Fix Process TestEnableRaiseEvent race condition","user":{"login":"stephentoub","id":2642209,"avatar_url":"https://avatars.githubusercontent.com/u/2642209?v=3","gravatar_id":"","url":"https://api.github.com/users/stephentoub","html_url":"https://github.com/stephentoub","followers_url":"https://api.github.com/users/stephentoub/followers","following_url":"https://api.github.com/users/stephentoub/following{/other_user}","gists_url":"https://api.github.com/users/stephentoub/gists{/gist_id}","starred_url":"https://api.github.com/users/stephentoub/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stephentoub/subscriptions","organizations_url":"https://api.github.com/users/stephentoub/orgs","repos_url":"https://api.github.com/users/stephentoub/repos","events_url":"https://api.github.com/users/stephentoub/events{/privacy}","received_events_url":"https://api.github.com/users/stephentoub/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/dotnet/corefx/labels/2%20-%20In%20Progress","name":"2 - In Progress","color":"CCCCCC"},{"url":"https://api.github.com/repos/dotnet/corefx/labels/cla-already-signed","name":"cla-already-signed","color":"009800"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-08-20T16:39:00Z","updated_at":"2015-08-21T17:57:37Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/dotnet/corefx/pulls/2900","html_url":"https://github.com/dotnet/corefx/pull/2900","diff_url":"https://github.com/dotnet/corefx/pull/2900.diff","patch_url":"https://github.com/dotnet/corefx/pull/2900.patch"},"body":"Process.WaitForExit and the callback invoked when a Process exits both call the RaiseOnExited method, which is implemented like this:\r\n```C#\r\nprivate void RaiseOnExited()\r\n{\r\n    if (!_raisedOnExited)\r\n    {\r\n        lock (this)\r\n        {\r\n            if (!_raisedOnExited)\r\n            {\r\n                _raisedOnExited = true;\r\n                OnExited();\r\n            }\r\n        }\r\n    }\r\n}\r\n```\r\nBecause _raisedOnExited is set prior to invoking OnExited, if Process.WaitForExit races with the exited callback to invoke RaiseOnExited, WaitForExit may see _raisedOnExited as true even if OnExited hasn't yet been executed or completed execution.  That means that when WaitForExit returns, there's no guarantee that the Exited delegate has been invoked, yet the TestEnableRaiseEvents method was relying on that.\r\n\r\nThis commit fixes the test to not rely on that, by spinning a bit waiting for the flag to be set.  I also took the opportunity to rewrite the test as a theory to avoid some duplication.\r\n\r\nI contemplated changing the RaiseOnExited implementation itself to do:\r\n```C#\r\ntry\r\n{\r\n    OnExited();\r\n}\r\nfinally\r\n{\r\n    _raisedOnExited = true;\r\n}\r\n```\r\ninstead of:\r\n```C#\r\n_raisedOnExited = true;\r\nOnExited();\r\n```\r\nbut that would potentially make certain deadlock conditions more likely, so I left it as is and changed the test instead.\r\n\r\nFIxes https://github.com/dotnet/corefx/issues/2562\r\ncc: @Priya91 "},"comment":{"url":"https://api.github.com/repos/dotnet/corefx/issues/comments/133512671","html_url":"https://github.com/dotnet/corefx/pull/2900#issuecomment-133512671","issue_url":"https://api.github.com/repos/dotnet/corefx/issues/2900","id":133512671,"user":{"login":"Priya91","id":7853870,"avatar_url":"https://avatars.githubusercontent.com/u/7853870?v=3","gravatar_id":"","url":"https://api.github.com/users/Priya91","html_url":"https://github.com/Priya91","followers_url":"https://api.github.com/users/Priya91/followers","following_url":"https://api.github.com/users/Priya91/following{/other_user}","gists_url":"https://api.github.com/users/Priya91/gists{/gist_id}","starred_url":"https://api.github.com/users/Priya91/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Priya91/subscriptions","organizations_url":"https://api.github.com/users/Priya91/orgs","repos_url":"https://api.github.com/users/Priya91/repos","events_url":"https://api.github.com/users/Priya91/events{/privacy}","received_events_url":"https://api.github.com/users/Priya91/received_events","type":"User","site_admin":false},"created_at":"2015-08-21T17:57:37Z","updated_at":"2015-08-21T17:57:37Z","body":"LGTM! Good idea :)"}}