Commit b0558a4
committed
fix(linter): prevent unsound use of
`Allocator` is currently `Sync`, but it shouldn't be - `Allocator` is not thread-safe and allocating into it in multiple threads can cause data corruption or, in worst case, writing out of bounds. I intend to remove the `impl Sync for Allocator` in a PR to follow.
The code altered in this PR was relying on `Allocator` being `Sync`. It was *not* actually dangerous as it stood, because access to the `Allocator` was synchronized by always holding a lock on the `messages` `Mutex` while using it. However, I suspect this was more by accident by design, and it was rather fragile.
This PR introduces a `MessageCloner` struct which provides a safe API which ensures access to the `Allocator` is correctly synchronized.
It's unfortunate to introduce a 2nd `Mutex`, and the `Mutex` inside `MessageCloner` is accessed more than necessary - on each turn of the loop in the `map` loops, instead of just once at the top of the loop. However, I couldn't find a way to achieve that without resorting to fragile unsafe code.
In any case, from what I can understand, the ultimate callers of both `run_source` and `run_test_source` convert the cloned `Message`s into other owning types (`DiagnosticReport` or `Report`) anyway. Therefore the double-conversion may be wasteful anyway, and we might be better off trying to convert to these owned types earlier, and avoid using an `Allocator` at all in these methods.
I also am wondering in what circumstances error messages would contain arena-allocated strings, and whether `Message` could instead contain `Cow<'static, str>`s, in which case the `Allocator` becomes unnecessary, and cloning would be cheaper.
Delving into the depths of the linter to understand all this is a bit beyond me, so I hope we can accept this imperfect solution for now, and refactor to remove/optimize it later on.Allocator across threads (#13032)1 parent ab685bd commit b0558a4
File tree
4 files changed
+87
-16
lines changed- crates
- oxc_language_server/src/linter
- oxc_linter/src
- service
4 files changed
+87
-16
lines changedLines changed: 3 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
92 | 92 | | |
93 | 93 | | |
94 | 94 | | |
95 | | - | |
| 95 | + | |
96 | 96 | | |
97 | | - | |
| 97 | + | |
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
| |||
143 | 143 | | |
144 | 144 | | |
145 | 145 | | |
146 | | - | |
| 146 | + | |
147 | 147 | | |
148 | 148 | | |
149 | 149 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
96 | 96 | | |
97 | 97 | | |
98 | 98 | | |
99 | | - | |
| 99 | + | |
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
| |||
105 | 105 | | |
106 | 106 | | |
107 | 107 | | |
108 | | - | |
| 108 | + | |
109 | 109 | | |
110 | 110 | | |
111 | 111 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
173 | 173 | | |
174 | 174 | | |
175 | 175 | | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
176 | 242 | | |
177 | 243 | | |
178 | 244 | | |
| |||
588 | 654 | | |
589 | 655 | | |
590 | 656 | | |
591 | | - | |
| 657 | + | |
592 | 658 | | |
593 | | - | |
594 | | - | |
595 | 659 | | |
596 | 660 | | |
| 661 | + | |
| 662 | + | |
597 | 663 | | |
598 | 664 | | |
599 | 665 | | |
| |||
614 | 680 | | |
615 | 681 | | |
616 | 682 | | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
617 | 686 | | |
618 | 687 | | |
619 | 688 | | |
| |||
655 | 724 | | |
656 | 725 | | |
657 | 726 | | |
658 | | - | |
| 727 | + | |
659 | 728 | | |
660 | 729 | | |
661 | 730 | | |
| |||
742 | 811 | | |
743 | 812 | | |
744 | 813 | | |
745 | | - | |
| 814 | + | |
746 | 815 | | |
747 | 816 | | |
748 | 817 | | |
749 | | - | |
750 | 818 | | |
751 | 819 | | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
752 | 823 | | |
753 | 824 | | |
754 | 825 | | |
| |||
773 | 844 | | |
774 | 845 | | |
775 | 846 | | |
776 | | - | |
777 | | - | |
| 847 | + | |
| 848 | + | |
778 | 849 | | |
779 | 850 | | |
780 | 851 | | |
781 | 852 | | |
782 | | - | |
| 853 | + | |
783 | 854 | | |
784 | 855 | | |
785 | 856 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
501 | 501 | | |
502 | 502 | | |
503 | 503 | | |
504 | | - | |
| 504 | + | |
505 | 505 | | |
506 | 506 | | |
507 | 507 | | |
| |||
554 | 554 | | |
555 | 555 | | |
556 | 556 | | |
557 | | - | |
| 557 | + | |
558 | 558 | | |
559 | 559 | | |
560 | 560 | | |
| |||
0 commit comments