Enforce the semaphore count <= max_count invariant - #70
Merged
Conversation
create_semaphore accepted parameters that produced an impossible
semaphore, e.g. create_semaphore('x', 5, 1) yielded count=5 with
max_count=1 — more held permits than the maximum. The table only checked
count >= 0 and max_count >= 1, never their relationship.
Add a CHECK (count <= max_count) constraint on the semaphores table and
validate the arguments in create_semaphore so callers get a clear error
instead of a silently broken semaphore. Adds a semaphore_validation
regression test covering the accepted and rejected cases.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ty3Rkif9Vfe95w8TMiKB4b
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
create_semaphoreaccepted parameters that produced an impossible semaphore:The table only checked
count >= 0andmax_count >= 1, never their relationship, so a semaphore could be created already over its own ceiling.Fix
CHECK (count <= max_count)on thesemaphorestable (enforces the invariant even against direct inserts and the existing acquire/release paths, which already keepcountwithin[0, max_count]).create_semaphoreso callers get a clear message instead of a silently broken (or cryptic constraint-named) error.Test
Adds
semaphore_validation, covering the accepted case plus the three rejected ones (count > max, negative count, max < 1) and confirming the invalid semaphores are not created. Suite green (14/14 on this branch).🤖 Generated with Claude Code
Generated by Claude Code