Commit f57d28d
committed
fix(folders): proactive sweep for missing in-tx lock rechecks + parent-delete race
Proactively audited the whole codebase for the two bug classes Greptile has
found repeatedly this session (missing in-transaction lock recheck; target
parent lock-checked but not re-verified as still active) rather than waiting
for another round to find each remaining instance. Found and fixed 9 sites:
1. Greptile P1 (new finding, on last round's own fix): the just-added
workflow performUpdateFolder transaction checks the target parent's LOCK
state but not whether it's still ACTIVE -- assertFolderMutable's lock
walker treats a deleted parent as unlocked (it stops the walk when the
row is missing rather than erroring), so a parent deleted between the
earlier assertFolderParentValid precheck and this transaction could still
have the moved folder's parentId written to point at it. Added a FOR
UPDATE + isNull(deletedAt) recheck, race-free once the lock is held.
Found and fixed the identical gap in the sibling generic kb/table
performUpdateFolder in folders/orchestration.ts, which had the exact same
shape from the same earlier round.
2. performDeleteResourceFolder (kb/table folder delete) -- wrapped in a
transaction but never checked the folder's own lock state at all before
cascading the delete. Added assertFolderMutable inside the tx.
3. deleteKnowledgeBase -- already row-locks via SELECT ... FOR UPDATE but
never re-invoked assertResourceMutable after acquiring it. Added the
recheck bound to tx.
4. deleteTable -- pre-check only, no transaction and no recheck at all.
Wrapped the write in a transaction with an in-tx recheck.
5. archiveWorkspaceFileFolderRecursive (file-folder delete) -- transaction
existed but never checked lock state. Added assertFolderMutable inside
the tx, matching the sibling restoreWorkspaceFileFolder in the same file.
6. bulkArchiveWorkspaceFileItems (file/folder bulk delete) -- same gap.
Since this function already acquires the workspace-scoped
pg_advisory_xact_lock as its first step (which every other file-folder
mutation in this file also acquires first), it's already fully
serialized against concurrent mutations in the same workspace -- unlike
the reorder batch fix, a simple per-id recheck loop is safe here without
a closure-based ordered lock.
7. renameWorkspaceFile -- no transaction, no recheck. Wrapped in a
transaction with the same "unless unlocking" in-tx recheck pattern used
at the other lock-toggle-capable rename/update sites.
8. restoreWorkspaceFile -- no lock check anywhere. Added a direct `.locked`
check on the already-fetched soft-deleted row (matches the pattern used
by the other resource restore paths, since the generic lock engine only
reads active rows and can't see a soft-deleted resource's own flag).
9. performDeleteFolder/deleteFolderRecursively (workflow folder delete) --
entirely non-transactional recursive cascade with zero lock checks.
archiveWorkflowsByIdsInWorkspace (called mid-cascade) doesn't accept a
`tx`, so wrapping the whole recursive cascade in one transaction isn't a
clean fix without a larger, riskier change to that shared function.
Applied the narrower, still-real improvement: a tightly-scoped
transaction around just each folder's own lock recheck + its own delete
write, closing the race for that specific row even though the cascade as
a whole still isn't one atomic unit.
Full local verification passed (typecheck, full vitest suite 11437 tests,
check:api-validation, lint all clean, 302 tests across the folders/locking
surface specifically).1 parent 7685991 commit f57d28d
6 files changed
Lines changed: 201 additions & 62 deletions
File tree
- apps/sim/lib
- folders
- knowledge
- table
- uploads/contexts/workspace
- workflows/orchestration
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
486 | 486 | | |
487 | 487 | | |
488 | 488 | | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
489 | 496 | | |
490 | 497 | | |
491 | 498 | | |
| |||
760 | 767 | | |
761 | 768 | | |
762 | 769 | | |
763 | | - | |
764 | | - | |
765 | | - | |
766 | | - | |
767 | | - | |
768 | | - | |
769 | | - | |
770 | | - | |
771 | | - | |
772 | | - | |
773 | | - | |
774 | | - | |
775 | | - | |
776 | | - | |
777 | | - | |
778 | | - | |
779 | | - | |
780 | | - | |
781 | | - | |
782 | | - | |
783 | | - | |
784 | | - | |
785 | | - | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
786 | 816 | | |
787 | | - | |
788 | | - | |
789 | | - | |
790 | | - | |
791 | | - | |
792 | | - | |
793 | | - | |
794 | | - | |
795 | | - | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
796 | 827 | | |
797 | | - | |
798 | | - | |
799 | | - | |
800 | | - | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
801 | 831 | | |
802 | | - | |
803 | | - | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
804 | 844 | | |
805 | 845 | | |
806 | 846 | | |
| |||
839 | 879 | | |
840 | 880 | | |
841 | 881 | | |
| 882 | + | |
| 883 | + | |
| 884 | + | |
842 | 885 | | |
843 | 886 | | |
844 | 887 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
593 | 593 | | |
594 | 594 | | |
595 | 595 | | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
596 | 602 | | |
597 | 603 | | |
598 | 604 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
737 | 737 | | |
738 | 738 | | |
739 | 739 | | |
740 | | - | |
741 | | - | |
742 | | - | |
743 | | - | |
744 | | - | |
745 | | - | |
746 | | - | |
747 | | - | |
748 | | - | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
749 | 757 | | |
750 | 758 | | |
751 | 759 | | |
| |||
Lines changed: 18 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
961 | 961 | | |
962 | 962 | | |
963 | 963 | | |
| 964 | + | |
| 965 | + | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
964 | 971 | | |
965 | 972 | | |
966 | 973 | | |
| |||
1173 | 1180 | | |
1174 | 1181 | | |
1175 | 1182 | | |
| 1183 | + | |
| 1184 | + | |
| 1185 | + | |
| 1186 | + | |
| 1187 | + | |
| 1188 | + | |
| 1189 | + | |
| 1190 | + | |
| 1191 | + | |
| 1192 | + | |
| 1193 | + | |
1176 | 1194 | | |
1177 | 1195 | | |
1178 | 1196 | | |
| |||
Lines changed: 44 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
10 | 14 | | |
11 | 15 | | |
12 | 16 | | |
| |||
989 | 993 | | |
990 | 994 | | |
991 | 995 | | |
| 996 | + | |
| 997 | + | |
992 | 998 | | |
993 | 999 | | |
994 | | - | |
995 | | - | |
996 | | - | |
997 | | - | |
998 | | - | |
999 | | - | |
1000 | | - | |
1001 | | - | |
1002 | | - | |
1003 | | - | |
1004 | | - | |
1005 | | - | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
1006 | 1028 | | |
1007 | | - | |
1008 | | - | |
| 1029 | + | |
| 1030 | + | |
1009 | 1031 | | |
1010 | 1032 | | |
1011 | 1033 | | |
| |||
1077 | 1099 | | |
1078 | 1100 | | |
1079 | 1101 | | |
| 1102 | + | |
| 1103 | + | |
| 1104 | + | |
| 1105 | + | |
| 1106 | + | |
| 1107 | + | |
| 1108 | + | |
| 1109 | + | |
1080 | 1110 | | |
1081 | 1111 | | |
1082 | 1112 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
25 | 28 | | |
26 | 29 | | |
27 | 30 | | |
| |||
215 | 218 | | |
216 | 219 | | |
217 | 220 | | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
218 | 237 | | |
219 | 238 | | |
220 | 239 | | |
| |||
244 | 263 | | |
245 | 264 | | |
246 | 265 | | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
247 | 269 | | |
248 | 270 | | |
249 | 271 | | |
| |||
307 | 329 | | |
308 | 330 | | |
309 | 331 | | |
310 | | - | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
311 | 345 | | |
312 | 346 | | |
313 | 347 | | |
| |||
0 commit comments