Skip to content

fix: correctness bugs found during code review#26

Merged
freeformz merged 1 commit intomainfrom
ffz/Fix
Feb 16, 2026
Merged

fix: correctness bugs found during code review#26
freeformz merged 1 commit intomainfrom
ffz/Fix

Conversation

@freeformz
Copy link
Owner

Summary

  • Iter2: Move counter variable inside returned closure so the index resets to 0 when the iterator is reused, instead of continuing from the previous invocation's final value
  • Locked.Clone / LockedOrdered.Clone: Remove outer RLock that caused recursive read-lock deadlock under write contention (the inner Iterator call already acquires RLock)
  • Ordered.UnmarshalJSON: Use Add() in a loop instead of direct slice assignment, so duplicate values in JSON input are deduplicated rather than corrupting internal state (len(values) != len(idx))
  • Chunk: Panic early with a clear message when n <= 0 instead of hitting a cryptic division-by-zero inside the iterator
  • SyncMap.Clear: Delete elements during Range instead of counting first then calling Clear() separately, so the returned count accurately reflects what was removed

Test plan

  • Added TestIter2ReusedIteratorResetsIndex to verify index resets on iterator reuse
  • All existing tests pass with -race flag

🤖 Generated with Claude Code

…Map.Clear

- Iter2: move counter inside returned closure so index resets on reuse
- Locked.Clone/LockedOrdered.Clone: remove outer RLock to prevent
  recursive read-lock deadlock under write contention
- Ordered.UnmarshalJSON: use Add() loop to deduplicate, preventing
  internal state corruption from duplicate JSON values
- Chunk: panic early with clear message on n <= 0 instead of
  division-by-zero inside the iterator
- SyncMap.Clear: delete during Range for atomic count accuracy

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@freeformz freeformz requested a review from Copilot February 16, 2026 01:14
@freeformz freeformz merged commit d6fc2ee into main Feb 16, 2026
13 checks passed
@freeformz freeformz deleted the ffz/Fix branch February 16, 2026 01:17
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes five correctness bugs discovered during code review, focusing on iterator behavior, concurrency issues, and data structure invariants.

Changes:

  • Fixed Iter2 to reset index counter on each iterator reuse by moving the counter variable inside the returned closure
  • Removed recursive read-lock deadlock in Locked.Clone and LockedOrdered.Clone by removing redundant outer RLock calls
  • Fixed Ordered.UnmarshalJSON to properly deduplicate JSON arrays with duplicate values using Add() instead of direct slice assignment
  • Added early panic with clear error message for invalid Chunk parameters (n <= 0)
  • Fixed SyncMap.Clear to delete elements during Range iteration for accurate count instead of separate count-then-clear operations

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
set.go Moved Iter2 counter variable inside closure for proper reset on reuse; added panic check for Chunk with n <= 0
set_test.go Added TestIter2ReusedIteratorResetsIndex to verify index counter resets correctly
locked.go Removed redundant outer RLock from Clone to prevent recursive lock deadlock
locked_ordered.go Removed redundant outer RLock from Clone to prevent recursive lock deadlock
ordered.go Changed UnmarshalJSON to use Add() loop instead of direct slice assignment to maintain idx/values invariant
sync.go Refactored Clear to delete during Range for accurate concurrent count; reformatted var declarations

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

1 participant