Skip to content

Enforce the semaphore count <= max_count invariant - #70

Merged
seanwevans merged 2 commits into
mainfrom
claude/semaphore-invariant
Jun 26, 2026
Merged

Enforce the semaphore count <= max_count invariant#70
seanwevans merged 2 commits into
mainfrom
claude/semaphore-invariant

Conversation

@seanwevans

Copy link
Copy Markdown
Owner

Bug

create_semaphore accepted parameters that produced an impossible semaphore:

SELECT create_semaphore('broken', 5, 1);
SELECT name, count, max_count FROM semaphores WHERE name = 'broken';
--   name  | count | max_count
-- --------+-------+-----------
--  broken |     5 |         1   <- 5 held permits, max of 1

The table only checked count >= 0 and max_count >= 1, never their relationship, so a semaphore could be created already over its own ceiling.

Fix

  • Add CHECK (count <= max_count) on the semaphores table (enforces the invariant even against direct inserts and the existing acquire/release paths, which already keep count within [0, max_count]).
  • Validate the arguments in create_semaphore so 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

claude and others added 2 commits June 26, 2026 19:42
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
@seanwevans
seanwevans merged commit f9615fb into main Jun 26, 2026
0 of 2 checks passed
@seanwevans
seanwevans deleted the claude/semaphore-invariant branch June 26, 2026 19:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants