Skip to content

Improve concurrent package#2810

Merged
dgageot merged 12 commits into
docker:mainfrom
dgageot:improve-concurrent
May 18, 2026
Merged

Improve concurrent package#2810
dgageot merged 12 commits into
docker:mainfrom
dgageot:improve-concurrent

Conversation

@dgageot
Copy link
Copy Markdown
Member

@dgageot dgageot commented May 18, 2026

Improve concurrent package and use it in more places

- Buffer: add Len() and Bytes() methods with proper locking
- Map: lazy-initialize values; Range() uses snapshot to allow safe callback mutations
- Slice: initialize values to empty slice for consistency; document lock guarantees in Range, Find, Update
- Add comprehensive tests for Buffer, Map; add AllEmpty test for Slice
@dgageot dgageot requested a review from a team as a code owner May 18, 2026 07:28
Copy link
Copy Markdown

@docker-agent docker-agent left a comment

Choose a reason for hiding this comment

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

Assessment: 🔴 CRITICAL

This PR improves the concurrent package and migrates callers to use it. The primitives themselves (Buffer, Map, Slice) look correct. However, two issues in the callers introduce real data races.

Comment thread pkg/tools/builtin/shell/shell.go
Comment thread pkg/userid/userid.go Outdated
dgageot added 11 commits May 18, 2026 10:28
Add LoadOrStore method to concurrent.Map with double-checked locking pattern
for efficient concurrent access. Simplifies snapshot.Manager.lock() by replacing
manual sync.Mutex map management with the new LoadOrStore helper.
exec.Cmd spawns separate goroutines for Stdout and Stderr, both writing
to the same buffer. bytes.Buffer is not safe for concurrent writes, so
use concurrent.Buffer instead. Rename boundedBuffer to boundedWriter to
reflect the actual interface.
@dgageot dgageot force-pushed the improve-concurrent branch from dfa5d0a to 9153372 Compare May 18, 2026 08:36
@dgageot
Copy link
Copy Markdown
Member Author

dgageot commented May 18, 2026

/review

Comment thread pkg/snapshot/snapshot.go
Comment thread cmd/root/run_event_hooks.go
Comment thread pkg/concurrent/slice.go
@dgageot dgageot merged commit c8783d1 into docker:main May 18, 2026
9 checks passed
Copy link
Copy Markdown

@docker-agent docker-agent left a comment

Choose a reason for hiding this comment

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

Assessment: 🟢 APPROVE

Reviewed 1082 lines across 16 files. The concurrent package improvements look well-implemented:

  • concurrent.Map: LoadOrStore correctly uses double-checked locking (read lock → re-check under write lock). Range clones the map under a read lock before iterating, so callbacks can safely mutate the map without deadlocking. Clear and Delete hold the write lock as expected.
  • concurrent.Slice: All() returns a slices.Clone for snapshot isolation. Range and Find document the constraint that callbacks must not acquire the write lock.
  • concurrent.Buffer: All operations (Write, String, Bytes, Drain) correctly hold the mutex. Bytes() returns a slices.Clone to prevent external mutation of the internal buffer.
  • Callers (fast_renderer.go, editfile/render.go, styles/composite.go, hooks/handler.go, hooks/model_handler.go, snapshot/snapshot.go, evaluation/progress.go, chatserver/conversation_lock.go): All refactored usages correctly replace manual mutex+map patterns with the new primitives. No races or logic errors introduced.

No bugs were found in the changed code.

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.

3 participants