Why two people paying each other can freeze your wallet
transcript
Ann pays Ben. At the same moment, Ben pays Ann. Each transfer needs both accounts locked. What could go wrong? It works on one thread. It works in every simple test.
Thread one locks A, then wants B. Thread two locks B, then wants A. Both wait forever. A cycle. The wallet freezes. No error. No crash. Just two requests that never finish.
One rule: lock the smaller account id first. Now both threads ask for A first. One waits. Nobody waits in a circle. A single helper does it. Every two-account operation uses it.
Test it on purpose, with two threads. A thousand payments each way. Without the order, this test hangs. It fails by freezing, not by a wrong number. With the order, it finishes. Both balances end exactly where they began.
Lock in a fixed order, always. Whichever side of the transfer you are on.
Full breakdown in the lecture.