[threadpool] Do not swallow exception in RegisterWaitForSingleObject callback (#4408)
authorLudovic Henry <ludovic@xamarin.com>
Tue, 21 Feb 2017 20:34:03 +0000 (15:34 -0500)
committerGitHub <noreply@github.com>
Tue, 21 Feb 2017 20:34:03 +0000 (15:34 -0500)
mcs/class/corlib/System.Threading/RegisteredWaitHandle.cs
mono/tests/Makefile.am
mono/tests/unhandled-exception-8.cs [new file with mode: 0644]

index 960ecf6e7b857595371308d71456e4c2fe0dad93..3bd11264b1a8fd04c929d3e03b624fb743621c0b 100644 (file)
@@ -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);
+                               }
                        }
                }
 
index 58ca018ac9b8d426153dd6b2067a9c288f337cf5..d809141dcf49510d0ef7a20d69575f0992b1f95f 100644 (file)
@@ -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 (file)
index 0000000..7b4de0e
--- /dev/null
@@ -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