Commit 938a94c
committed
Make sure we never context switch while holding VM lock.
We were seeing errors in our application that looked like:
```
[BUG] unexpected situation - recordd:1 current:0
/error.c:1097 rb_bug_without_die_internal
/vm_sync.c:275 disallow_reentry
/eval_intern.h:136 rb_ec_vm_lock_rec_check
/eval_intern.h:147 rb_ec_tag_state
/vm.c:2619 rb_vm_exec
/vm.c:1702 rb_yield
/eval.c:1173 rb_ensure
```
We concluded that there was context switching going on while a thread
held the VM lock. During the investigation into the issue, we added
assertions that we never yield to another thread with the VM lock held.
We enabled these VM lock assertions even in single ractor mode. These
assertions were failing in a few places, but most notably in finalizers.
We were running finalizers with the VM lock held, and they were context
switching and causing this issue.
These rules must be held going forward to ensure we don't context switch unexpectedly:
If you have the VM lock held,
* Don't enter the interpreter loop.
* Don't yield to ruby code.
* Don't call rb_nogvl (it will context switch you and will not unlock the VM lock).
* You can still check interrupts, but we won't allow context switching
If you don't have the GVL:
* Don't call rb_ensure/rb_protect, etc (these are old rules but good to have assertions for).
Rework global variables, don't lock when calling getter or setter
Add a test that fails without these lock_rec changes.
Add ASSERT_vm_unlocking() to vm_call0_body
This uncovered many more test failures.
Revert changes introduced in 2f6c694
This doesn't appear to be a correct fix. We should allow raising
NoMemoryError even if we're under the VM lock.
another commit1 parent 8f040a5 commit 938a94c
File tree
22 files changed
+333
-100
lines changed- gc/default
- test/ruby
- yjit/src
22 files changed
+333
-100
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4199 | 4199 | | |
4200 | 4200 | | |
4201 | 4201 | | |
| 4202 | + | |
4202 | 4203 | | |
| 4204 | + | |
4203 | 4205 | | |
4204 | 4206 | | |
4205 | 4207 | | |
| |||
13362 | 13364 | | |
13363 | 13365 | | |
13364 | 13366 | | |
| 13367 | + | |
13365 | 13368 | | |
13366 | 13369 | | |
13367 | 13370 | | |
| |||
13545 | 13548 | | |
13546 | 13549 | | |
13547 | 13550 | | |
| 13551 | + | |
13548 | 13552 | | |
13549 | 13553 | | |
13550 | 13554 | | |
| |||
14791 | 14795 | | |
14792 | 14796 | | |
14793 | 14797 | | |
| 14798 | + | |
14794 | 14799 | | |
| 14800 | + | |
14795 | 14801 | | |
14796 | 14802 | | |
14797 | 14803 | | |
| |||
17722 | 17728 | | |
17723 | 17729 | | |
17724 | 17730 | | |
| 17731 | + | |
| 17732 | + | |
| 17733 | + | |
| 17734 | + | |
17725 | 17735 | | |
17726 | 17736 | | |
| 17737 | + | |
17727 | 17738 | | |
17728 | 17739 | | |
17729 | 17740 | | |
17730 | 17741 | | |
| 17742 | + | |
17731 | 17743 | | |
| 17744 | + | |
17732 | 17745 | | |
| 17746 | + | |
17733 | 17747 | | |
| 17748 | + | |
17734 | 17749 | | |
17735 | 17750 | | |
17736 | 17751 | | |
17737 | 17752 | | |
| 17753 | + | |
17738 | 17754 | | |
17739 | 17755 | | |
| 17756 | + | |
17740 | 17757 | | |
17741 | 17758 | | |
17742 | 17759 | | |
| |||
17905 | 17922 | | |
17906 | 17923 | | |
17907 | 17924 | | |
| 17925 | + | |
17908 | 17926 | | |
| 17927 | + | |
17909 | 17928 | | |
17910 | 17929 | | |
| 17930 | + | |
| 17931 | + | |
| 17932 | + | |
17911 | 17933 | | |
17912 | 17934 | | |
17913 | 17935 | | |
| 17936 | + | |
| 17937 | + | |
17914 | 17938 | | |
17915 | 17939 | | |
| 17940 | + | |
17916 | 17941 | | |
| 17942 | + | |
17917 | 17943 | | |
17918 | 17944 | | |
17919 | 17945 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
| |||
344 | 345 | | |
345 | 346 | | |
346 | 347 | | |
| 348 | + | |
347 | 349 | | |
348 | 350 | | |
349 | 351 | | |
| |||
421 | 423 | | |
422 | 424 | | |
423 | 425 | | |
| 426 | + | |
424 | 427 | | |
425 | 428 | | |
426 | 429 | | |
| |||
439 | 442 | | |
440 | 443 | | |
441 | 444 | | |
442 | | - | |
| 445 | + | |
443 | 446 | | |
444 | 447 | | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
445 | 451 | | |
446 | 452 | | |
447 | 453 | | |
| |||
472 | 478 | | |
473 | 479 | | |
474 | 480 | | |
| 481 | + | |
475 | 482 | | |
476 | | - | |
| 483 | + | |
477 | 484 | | |
478 | 485 | | |
479 | 486 | | |
480 | | - | |
481 | 487 | | |
| 488 | + | |
482 | 489 | | |
483 | 490 | | |
484 | 491 | | |
485 | 492 | | |
486 | 493 | | |
487 | 494 | | |
| 495 | + | |
488 | 496 | | |
489 | 497 | | |
490 | 498 | | |
| 499 | + | |
491 | 500 | | |
492 | 501 | | |
493 | 502 | | |
| |||
535 | 544 | | |
536 | 545 | | |
537 | 546 | | |
538 | | - | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
539 | 551 | | |
540 | | - | |
541 | 552 | | |
542 | 553 | | |
543 | 554 | | |
| |||
552 | 563 | | |
553 | 564 | | |
554 | 565 | | |
555 | | - | |
556 | 566 | | |
557 | 567 | | |
| 568 | + | |
558 | 569 | | |
559 | 570 | | |
560 | 571 | | |
| |||
575 | 586 | | |
576 | 587 | | |
577 | 588 | | |
| 589 | + | |
| 590 | + | |
578 | 591 | | |
579 | 592 | | |
580 | 593 | | |
| |||
588 | 601 | | |
589 | 602 | | |
590 | 603 | | |
| 604 | + | |
591 | 605 | | |
592 | 606 | | |
593 | 607 | | |
| |||
605 | 619 | | |
606 | 620 | | |
607 | 621 | | |
| 622 | + | |
| 623 | + | |
608 | 624 | | |
609 | 625 | | |
610 | 626 | | |
| |||
671 | 687 | | |
672 | 688 | | |
673 | 689 | | |
674 | | - | |
| 690 | + | |
675 | 691 | | |
676 | 692 | | |
677 | | - | |
678 | | - | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
679 | 696 | | |
680 | 697 | | |
681 | 698 | | |
682 | 699 | | |
683 | 700 | | |
684 | 701 | | |
685 | 702 | | |
| 703 | + | |
686 | 704 | | |
687 | 705 | | |
688 | 706 | | |
| |||
691 | 709 | | |
692 | 710 | | |
693 | 711 | | |
694 | | - | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
695 | 717 | | |
696 | 718 | | |
697 | 719 | | |
| |||
701 | 723 | | |
702 | 724 | | |
703 | 725 | | |
| 726 | + | |
704 | 727 | | |
705 | 728 | | |
706 | 729 | | |
707 | 730 | | |
708 | 731 | | |
709 | 732 | | |
710 | 733 | | |
711 | | - | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
712 | 738 | | |
713 | 739 | | |
714 | 740 | | |
| |||
717 | 743 | | |
718 | 744 | | |
719 | 745 | | |
720 | | - | |
721 | | - | |
722 | | - | |
723 | | - | |
724 | | - | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
725 | 751 | | |
726 | 752 | | |
727 | | - | |
728 | | - | |
729 | | - | |
730 | | - | |
731 | | - | |
732 | | - | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
733 | 759 | | |
734 | 760 | | |
735 | 761 | | |
736 | | - | |
737 | | - | |
738 | | - | |
739 | | - | |
740 | | - | |
741 | | - | |
742 | | - | |
743 | | - | |
744 | | - | |
745 | | - | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
746 | 772 | | |
747 | | - | |
| 773 | + | |
| 774 | + | |
748 | 775 | | |
749 | 776 | | |
750 | 777 | | |
| |||
865 | 892 | | |
866 | 893 | | |
867 | 894 | | |
868 | | - | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
869 | 900 | | |
870 | 901 | | |
871 | 902 | | |
| |||
1812 | 1843 | | |
1813 | 1844 | | |
1814 | 1845 | | |
| 1846 | + | |
1815 | 1847 | | |
1816 | 1848 | | |
1817 | 1849 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
| 98 | + | |
| 99 | + | |
98 | 100 | | |
| 101 | + | |
99 | 102 | | |
100 | 103 | | |
101 | 104 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5081 | 5081 | | |
5082 | 5082 | | |
5083 | 5083 | | |
5084 | | - | |
5085 | | - | |
5086 | | - | |
| 5084 | + | |
5087 | 5085 | | |
5088 | 5086 | | |
5089 | 5087 | | |
5090 | | - | |
5091 | | - | |
5092 | | - | |
5093 | | - | |
5094 | | - | |
5095 | | - | |
5096 | | - | |
| 5088 | + | |
| 5089 | + | |
5097 | 5090 | | |
| 5091 | + | |
| 5092 | + | |
| 5093 | + | |
5098 | 5094 | | |
5099 | 5095 | | |
5100 | 5096 | | |
| |||
0 commit comments