Skip to content

Fix shared event stream codec races - #716

Open
Shubham-Padkonde wants to merge 1 commit into
aws:mainfrom
Shubham-Padkonde:fix/eventstream-codec-concurrency
Open

Shubham-Padkonde wants to merge 1 commit into
aws:mainfrom
Shubham-Padkonde:fix/eventstream-codec-concurrency

Conversation

@Shubham-Padkonde

Copy link
Copy Markdown

Issue #, if available:

Fixes #714.

Description of changes:

Keep framing encoder/decoder state local to each codec call. The codec is shared by concurrent streams, but its lazy encoder/decoder initialization and payload slice writes were unsynchronized. The encoder also owns mutable bytes.Buffer instances: protecting initialization alone would still allow message corruption.

Remove the unused shared payload buffer and instantiate framing encoders/decoders at the call sites. This avoids serializing independent streams behind a shared lock. The tradeoff is that encoder buffers are no longer reused across messages.

The regression starts 32 goroutines sharing one codec, each repeatedly decoding initial responses and unknown events. Unknown-event handling also exercises re-encoding; the test checks that tags and complete encoded messages remain intact. On the original code, -race reports races and the test encountered a bytes.Buffer.WriteTo panic. With the change, five focused race runs pass.

Validation (Go 1.26.3, Linux):

  • go test -race -count=5 ./internal/eventstream
  • make unit (includes vet and unit tests across modules)
  • go test -race ./... (root module)

Includes a runtime changelog entry. No live AWS service calls were used.

AI assistance: Codex assisted with reproduction, implementation, and validation. No separate human review is claimed.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@Shubham-Padkonde
Shubham-Padkonde requested review from a team as code owners September 21, 2026 06:40
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.

Detected race condition in internal/eventstream/codec.go

1 participant