Keep SortedSet membership and index consistent after failed add or update - #257
rupayon123 wants to merge 2 commits into
Conversation
|
The I measured the part the description scopes out, because I think the exposure is larger than "bulk updates" suggests and one detail undercuts the retry test you added. The excluded branch is the common one, not the edge case. Only a set much larger than the addition is protected. An ordinary The state it leaves is worse than "not transactional". With a key that raises for the new values:
Also worth noting None of this argues for widening the PR — the scope you stated is defensible. It is two things: whether an update that corrupts the index and cannot be repaired through the public API should be left as a known gap, and whether |
|
@feiiiiii5, thank you for testing the rebuild path. I reproduced your 3-plus-4 case: before the fix, |
SortedSetstores membership and its sorted index separately. A failedadd()could leave a phantom member in_set. Review feedback identified a second path: a failed rebuild-styleupdate()cleared_listafter expanding_set, so ordinary bulk updates could leave membership and iteration permanently inconsistent.This PR rolls back membership when
add()fails. For the rebuild branch ofupdate()(also used by|=), it now constructs and validates a candidate sorted list before mutating either live structure. Once validation succeeds, it installs the prepared internal list state in place, preserving the bound public bisect/index methods and the existing set object. Regression tests cover both failure paths, a retry after a temporary key error, and those bound methods after a successful bulk update.Validation on the latest head: the new bulk-update cases failed against the previous PR head (7 members but an empty sorted list);
PYTHONPATH=src uvx --from pytest pytest tests -qnow passes 304 tests. Ruff checks on both changed files andgit diff --checkpass. The originaladd()change also passed its earlier broader checks. Remote CI and other runtimes remain to be verified. Values are still expected to keep stable hashes and ordering, as the class contract states.Prepared with OpenAI Codex assistance; I reviewed the diff and regression behavior.