Commit d209433
authored
Ignore exceptions thrown from Thread.UncaughtExceptionHandler (#4525)
Fixes #4516
Background
----------
Whenever an *uncaught* exception happens in a coroutine,
it gets reported to the `CoroutineExceptionHandler`.
See <https://kotlinlang.org/docs/exception-handling.html#coroutineexceptionhandler>.
However, if it's not installed, a platform-specific handler
is used.
On the JVM, this means invoking the thread's
`UncaughtExceptionHandler`, which logs the exception to the console
by default, but can be configured to do other things
(for example, on Android, it will crash the application).
Problem
-------
User-specified `UncaughtExceptionHandler` instances are allowed to
throw exceptions.
Java's documentation says so
(https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Thread.UncaughtExceptionHandler.html):
> Any exception thrown by this method will be ignored by the Java Virtual Machine.
This means a user is allowed to write a throwing
`UncaughtExceptionHandler`, and the caller has to deal with it.
In our implementation, however, we are simply invoking the
exception handler as a plain function, and if that function
throws an exception, we allow this exception to propagate
to the coroutine machinery, causing it to fail.
Solution
--------
To comply with the contract defined for `UncaughtExceptionHandler`,
we also ignore the exceptions thrown from there.1 parent fdb01da commit d209433
File tree
2 files changed
+62
-27
lines changed- kotlinx-coroutines-core/jvm
- src/internal
- test/exceptions
2 files changed
+62
-27
lines changedLines changed: 12 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
31 | | - | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
32 | 43 | | |
33 | 44 | | |
34 | 45 | | |
| |||
Lines changed: 50 additions & 26 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | 11 | | |
25 | | - | |
| 12 | + | |
26 | 13 | | |
27 | | - | |
28 | | - | |
29 | | - | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
30 | 19 | | |
31 | | - | |
32 | | - | |
33 | 20 | | |
34 | | - | |
| 21 | + | |
35 | 22 | | |
36 | 23 | | |
37 | 24 | | |
38 | 25 | | |
39 | 26 | | |
40 | 27 | | |
41 | | - | |
| 28 | + | |
42 | 29 | | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
47 | 36 | | |
48 | | - | |
| 37 | + | |
49 | 38 | | |
50 | 39 | | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
51 | 75 | | |
0 commit comments