Liveness & Fairness · Page 5 of 5
Case Study: Two-Phase Commit
Here is a complete distributed protocol in ~90 lines of Python: Lamport's Two-Phase Commit.
A transaction manager (tm_state) coordinates a set of resource managers
(rms), each stored in a finite map dict[RM, RMState] updated with
Python's dict spread syntax {**s.rm_state, r: "prepared"}:
- Safety (
consistent): no resource manager commits while another aborts. - Safety (
commit_requires_all_prepared): the TM never commits unless every RM has prepared. - Liveness (
decision_propagates): once the TM commits, every RM eventually commits.
Because a finished transaction has no more actions enabled, we set
check_deadlock=False on the model.
Explore & Break It: First click Check to verify all 38
states pass. Then try weakening
tm_commit's guard by removing
and s.tm_prepared == s.rms — click Check to see the checker
find a premature commit! (Or comment out rm_rcv_commit's fairness to break
decision_propagates.)