Commit f86cbc7
Fix a bug that could leave the coroutine scheduler's PRNG in an invalid state (#4052)
A potential bug is fixed, where `nextInt` would always return zero due to incorrect initialization of rngState with zero.
This could happen during the creation of a worker with thread id = `1595972770` (JDK >=8), or unpredictable if fallback thread-local random is used (android SDK <34 or JDK <7), approximate probability is 2.4E-10
Also, this slightly optimizes the performance of coroutines initialization. During the creation of `CoroutineScheduler.Worker`, we need to initialize its embedded random number generator.
To avoid additional class-loading costs (#4051), `rngState` is now directly initialized from the current nanoTime (that is used as seed).
Fixes #4051
Co-authored-by: Vsevolod Tolstopyatov <qwwdfsad@gmail.com>1 parent 6c21369 commit f86cbc7
File tree
1 file changed
+12
-3
lines changed- kotlinx-coroutines-core/jvm/src/scheduling
1 file changed
+12
-3
lines changedLines changed: 12 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
12 | 11 | | |
13 | 12 | | |
14 | 13 | | |
| |||
658 | 657 | | |
659 | 658 | | |
660 | 659 | | |
661 | | - | |
| 660 | + | |
662 | 661 | | |
663 | 662 | | |
664 | 663 | | |
665 | | - | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
666 | 675 | | |
667 | 676 | | |
668 | 677 | | |
| |||
0 commit comments