Commit 8d8159e
authored
Fix thread scheduler issue with thread_sched_wait_events (ruby#15392)
Fix race between timer thread dequeuing waiting thread and thread
skipping sleeping due to being dequeued. We now use `th->event_serial` which
is protected by `thread_sched_lock`. When a thread is put on timer thread's waiting
list, the event serial is saved on the item. The timer thread checks
that the saved serial is the same as current thread's serial before
calling `thread_sched_to_ready`.
The following script (taken from a test in `test_thread.rb` used to crash on
scheduler debug assertions. It would likely crash in non-debug mode as well.
```ruby
def assert_nil(val)
if val != nil
raise "Expected #{val} to be nil"
end
end
def assert_equal(expected, actual)
if expected != actual
raise "Expected #{expected} to be #{actual}"
end
end
def test_join2
ok = false
t1 = Thread.new { ok = true; sleep }
Thread.pass until ok
Thread.pass until t1.stop?
t2 = Thread.new do
Thread.pass while ok
t1.join(0.01)
end
t3 = Thread.new do
ok = false
t1.join
end
assert_nil(t2.value)
t1.wakeup
assert_equal(t1, t3.value)
ensure
t1&.kill&.join
t2&.kill&.join
t3&.kill&.join
end
rs = 30.times.map do
Ractor.new do
test_join2
end
end
rs.each(&:join)
```1 parent 7d9558f commit 8d8159e
File tree
4 files changed
+42
-25
lines changed4 files changed
+42
-25
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1122 | 1122 | | |
1123 | 1123 | | |
1124 | 1124 | | |
| 1125 | + | |
1125 | 1126 | | |
1126 | 1127 | | |
| 1128 | + | |
1127 | 1129 | | |
1128 | 1130 | | |
1129 | 1131 | | |
| |||
1149 | 1151 | | |
1150 | 1152 | | |
1151 | 1153 | | |
| 1154 | + | |
1152 | 1155 | | |
1153 | 1156 | | |
1154 | 1157 | | |
| |||
1338 | 1341 | | |
1339 | 1342 | | |
1340 | 1343 | | |
1341 | | - | |
| 1344 | + | |
1342 | 1345 | | |
1343 | 1346 | | |
1344 | 1347 | | |
| |||
2868 | 2871 | | |
2869 | 2872 | | |
2870 | 2873 | | |
2871 | | - | |
| 2874 | + | |
2872 | 2875 | | |
2873 | 2876 | | |
2874 | 2877 | | |
| |||
2970 | 2973 | | |
2971 | 2974 | | |
2972 | 2975 | | |
2973 | | - | |
| 2976 | + | |
2974 | 2977 | | |
2975 | 2978 | | |
2976 | 2979 | | |
| |||
2987 | 2990 | | |
2988 | 2991 | | |
2989 | 2992 | | |
2990 | | - | |
| 2993 | + | |
| 2994 | + | |
| 2995 | + | |
2991 | 2996 | | |
2992 | 2997 | | |
2993 | 2998 | | |
2994 | 2999 | | |
2995 | 3000 | | |
2996 | 3001 | | |
2997 | | - | |
| 3002 | + | |
2998 | 3003 | | |
2999 | | - | |
| 3004 | + | |
3000 | 3005 | | |
3001 | 3006 | | |
3002 | | - | |
3003 | | - | |
3004 | | - | |
3005 | 3007 | | |
3006 | 3008 | | |
3007 | 3009 | | |
3008 | | - | |
| 3010 | + | |
3009 | 3011 | | |
3010 | 3012 | | |
3011 | 3013 | | |
3012 | 3014 | | |
3013 | 3015 | | |
3014 | 3016 | | |
3015 | | - | |
| 3017 | + | |
3016 | 3018 | | |
3017 | 3019 | | |
3018 | 3020 | | |
| |||
3022 | 3024 | | |
3023 | 3025 | | |
3024 | 3026 | | |
| 3027 | + | |
3025 | 3028 | | |
3026 | 3029 | | |
3027 | 3030 | | |
3028 | | - | |
3029 | | - | |
| 3031 | + | |
| 3032 | + | |
| 3033 | + | |
| 3034 | + | |
3030 | 3035 | | |
3031 | 3036 | | |
3032 | 3037 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
| 42 | + | |
42 | 43 | | |
43 | 44 | | |
44 | 45 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
19 | | - | |
20 | | - | |
| 18 | + | |
21 | 19 | | |
22 | 20 | | |
23 | 21 | | |
| |||
57 | 55 | | |
58 | 56 | | |
59 | 57 | | |
60 | | - | |
| 58 | + | |
61 | 59 | | |
62 | 60 | | |
63 | 61 | | |
| |||
67 | 65 | | |
68 | 66 | | |
69 | 67 | | |
| 68 | + | |
| 69 | + | |
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
76 | | - | |
| 76 | + | |
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
84 | | - | |
| 84 | + | |
| 85 | + | |
85 | 86 | | |
86 | 87 | | |
| 88 | + | |
87 | 89 | | |
88 | 90 | | |
89 | 91 | | |
| |||
111 | 113 | | |
112 | 114 | | |
113 | 115 | | |
114 | | - | |
| 116 | + | |
| 117 | + | |
115 | 118 | | |
116 | 119 | | |
117 | 120 | | |
| |||
680 | 683 | | |
681 | 684 | | |
682 | 685 | | |
683 | | - | |
| 686 | + | |
684 | 687 | | |
685 | 688 | | |
686 | 689 | | |
| |||
807 | 810 | | |
808 | 811 | | |
809 | 812 | | |
| 813 | + | |
810 | 814 | | |
811 | 815 | | |
812 | 816 | | |
| |||
855 | 859 | | |
856 | 860 | | |
857 | 861 | | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
858 | 866 | | |
859 | 867 | | |
860 | 868 | | |
| |||
885 | 893 | | |
886 | 894 | | |
887 | 895 | | |
888 | | - | |
| 896 | + | |
889 | 897 | | |
890 | 898 | | |
891 | 899 | | |
| |||
986 | 994 | | |
987 | 995 | | |
988 | 996 | | |
| 997 | + | |
989 | 998 | | |
990 | | - | |
| 999 | + | |
991 | 1000 | | |
992 | 1001 | | |
993 | 1002 | | |
| |||
1031 | 1040 | | |
1032 | 1041 | | |
1033 | 1042 | | |
| 1043 | + | |
1034 | 1044 | | |
1035 | | - | |
| 1045 | + | |
1036 | 1046 | | |
1037 | 1047 | | |
1038 | 1048 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1127 | 1127 | | |
1128 | 1128 | | |
1129 | 1129 | | |
| 1130 | + | |
1130 | 1131 | | |
1131 | 1132 | | |
1132 | 1133 | | |
| |||
0 commit comments