Serguey Asael Shinder: After a failed write, the code has to decide whether anything happened
Two fixes merged this week in cryptocurrency node software made opposite choices about the same moment: the instant after an operation reports failure.
In the XRP Ledger server, rippled pull request 8662 changes what saveValidatedLedger() leaves behind when the database write fails. Before the write it marks the ledger twice, an entry in the pending-saves list and a SAVED flag on the ledger hash. Those marks used to stay after an error. Now the function clears both before the error reaches its caller, so the next save of that ledger runs as a first attempt and anyone waiting on it wakes up. The authors note that this write fails only locally, when SQLite, its disk or its schema fails, so a failed write really did not happen.
In lnd, the 0.21.8 release notes describe the reverse. When publishing a funding batch transaction returns an error, lnd used to abandon the pending channels and release their input leases. It no longer does, because, in the notes' words, an error can be returned after the transaction has already reached the network. The node keeps watching so it can recover the channels if the transaction confirms.

Both are correct, because the failures mean different things. A local write that failed left nothing in the world, so the code must leave nothing in its own state. A network call that failed may have done its work anyway, so the code must keep enough state to find out.
Java's standard library writes the first contract down precisely. The javadoc of Map.computeIfAbsent says that if the mapping function throws an unchecked exception, the exception is rethrown and no mapping is recorded. A failed computation leaves the map as it was, so the next call simply tries again. That is the property rippled restored for its ledger saves.
The second contract rarely gets written down, and that is where bugs live. A method that calls a remote service and throws IOException usually documents only that an I/O error occurred, not whether the remote side acted. Code that treats every exception as "nothing happened" will release locks, refund balances or delete pending records for operations that actually went through.
So the habit I take from these two fixes is a line of documentation on every operation with a side effect: on failure, is the state unchanged, changed, or unknown? Unchanged means clean up as computeIfAbsent does. Unknown means keep what you need to reconcile later, as lnd now does, and accept that something will sometimes need manual cleanup. The notes for lnd say exactly that about funding transactions that can never be published. Writing it down is cheaper than discovering it from a stuck channel.