From: Ludovic Henry Date: Tue, 21 Feb 2017 20:34:03 +0000 (-0500) Subject: [threadpool] Do not swallow exception in RegisterWaitForSingleObject callback (#4408) X-Git-Url: http://wien.tomnetworks.com/gitweb/?p=mono.git;a=commitdiff_plain;h=0d2d3266b7eca379ca7c91d7c59677902596eef5 [threadpool] Do not swallow exception in RegisterWaitForSingleObject callback (#4408) --- diff --git a/mcs/class/corlib/System.Threading/RegisteredWaitHandle.cs b/mcs/class/corlib/System.Threading/RegisteredWaitHandle.cs index 960ecf6e7b8..3bd11264b1a 100644 --- a/mcs/class/corlib/System.Threading/RegisteredWaitHandle.cs +++ b/mcs/class/corlib/System.Threading/RegisteredWaitHandle.cs @@ -98,17 +98,16 @@ namespace System.Threading private void DoCallBack (object timedOut) { - if (_callback != null) { - try { - _callback (_state, (bool)timedOut); - } catch {} - } - - lock (this) - { - _callsInProcess--; - if (_unregistered && _callsInProcess == 0 && _finalEvent != null) - NativeEventCalls.SetEvent (_finalEvent.SafeWaitHandle); + try { + if (_callback != null) + _callback (_state, (bool)timedOut); + } finally { + lock (this) + { + _callsInProcess--; + if (_unregistered && _callsInProcess == 0 && _finalEvent != null) + NativeEventCalls.SetEvent (_finalEvent.SafeWaitHandle); + } } } diff --git a/mono/tests/Makefile.am b/mono/tests/Makefile.am index 58ca018ac9b..d809141dcf4 100644 --- a/mono/tests/Makefile.am +++ b/mono/tests/Makefile.am @@ -1763,7 +1763,8 @@ UNHANDLED_EXCEPTION_255_TESTS = \ unhandled-exception-4.exe \ unhandled-exception-5.exe \ unhandled-exception-6.exe \ - unhandled-exception-7.exe + unhandled-exception-7.exe \ + unhandled-exception-8.exe test-unhandled-exception-2: $(UNHANDLED_EXCEPTION_1_TESTS) $(UNHANDLED_EXCEPTION_255_TESTS) $(MAKE) test-unhandled-exception-2-1-with-managed-handler diff --git a/mono/tests/unhandled-exception-8.cs b/mono/tests/unhandled-exception-8.cs new file mode 100644 index 00000000000..7b4de0e97bf --- /dev/null +++ b/mono/tests/unhandled-exception-8.cs @@ -0,0 +1,24 @@ + +using System; +using System.Threading; + +class CustomException : Exception +{ +} + +class Driver +{ + /* expected exit code: 255 */ + public static void Main () + { + if (Environment.GetEnvironmentVariable ("TEST_UNHANDLED_EXCEPTION_HANDLER") != null) + AppDomain.CurrentDomain.UnhandledException += (s, e) => {}; + + ManualResetEvent mre = new ManualResetEvent(false); + + ThreadPool.RegisterWaitForSingleObject (mre, (state, timedOut) => { throw new CustomException (); }, null, -1, true); + mre.Set(); + + Thread.Sleep (5000); + } +} \ No newline at end of file