Skip to content

fix(remoting): add sync.RWMutex protection for global codec map#3437

Open
Aias00 wants to merge 4 commits into
apache:developfrom
Aias00:worktree-fix-codec-map-sync
Open

fix(remoting): add sync.RWMutex protection for global codec map#3437
Aias00 wants to merge 4 commits into
apache:developfrom
Aias00:worktree-fix-codec-map-sync

Conversation

@Aias00

@Aias00 Aias00 commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Problem

The global codec map in remoting/codec.go has no synchronization protection:

var codec = make(map[string]Codec, 2)

func RegistryCodec(protocol string, codecTmp Codec) { codec[protocol] = codecTmp }
func GetCodec(protocol string) Codec { return codec[protocol] }

While RegistryCodec is currently only called during init(), GetCodec is called at runtime from multiple goroutines (e.g., getty_client.go, getty_server.go). The public RegistryCodec API allows runtime registration, which would cause a Go map concurrent read-write panic.

Solution

Add sync.RWMutex to protect all map access:

  • RegistryCodec: Lock/Unlock for writes
  • GetCodec: RLock/RUnlock for reads

This follows the same pattern used throughout the project (e.g., common/extension/registry_type.go, cluster/cluster/interceptor_invoker.go).

Changes

  • remoting/codec.go: Add sync.RWMutex protection for codec map
  • remoting/codec_test.go: Add TestConcurrentCodecAccess with 100 goroutines concurrent read/write under -race

Testing

All tests pass with -race flag:

go test -race -run "TestRegistryCodec|TestGetCodecNotFound|TestRegistryCodecOverwrite|TestConcurrentCodecAccess|TestDecodeResult" ./remoting/ -v

🤖 Generated with Claude Code

The global codec map in remoting/codec.go was accessed without any
synchronization: RegistryCodec writes and GetCodec reads could race
when called from multiple goroutines. While current production code
only writes during init(), the public API allows runtime registration,
which would cause a Go map concurrent read-write panic.

Add sync.RWMutex to protect all map access:
- RegistryCodec: Lock/Unlock for writes
- GetCodec: RLock/RUnlock for reads

This follows the same pattern used throughout the project
(e.g., common/extension/registry_type.go, cluster/cluster/interceptor_invoker.go).

Co-Authored-By: Claude <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings June 17, 2026 06:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds concurrency safety to the global codec registry and introduces a regression test to exercise concurrent registry access.

Changes:

  • Protects the global codec map with an sync.RWMutex in RegistryCodec / GetCodec.
  • Adds TestConcurrentCodecAccess to concurrently read/write the registry.

Reviewed changes

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

File Description
remoting/codec.go Adds an RWMutex to make codec registry map access safe under concurrency.
remoting/codec_test.go Adds a concurrency-focused test that exercises concurrent reads/writes to the codec registry.

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

Comment thread remoting/codec_test.go
Comment on lines +106 to +129
func TestConcurrentCodecAccess(t *testing.T) {
const goroutines = 100
var wg sync.WaitGroup
wg.Add(goroutines * 2)

// Concurrent writers
for i := 0; i < goroutines; i++ {
go func(i int) {
defer wg.Done()
RegistryCodec("concurrent-"+string(rune('a'+i%26)), &mockCodec{})
}(i)
}

// Concurrent readers
for i := 0; i < goroutines; i++ {
go func(i int) {
defer wg.Done()
GetCodec("concurrent-" + string(rune('a'+i%26)))
}(i)
}

wg.Wait()
// If we reach here without a panic, the concurrent access is safe.
}
Comment thread remoting/codec_test.go
for i := 0; i < goroutines; i++ {
go func(i int) {
defer wg.Done()
RegistryCodec("concurrent-"+string(rune('a'+i%26)), &mockCodec{})
@codecov-commenter

codecov-commenter commented Jun 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 53.57%. Comparing base (60d1c2a) to head (703b1bc).
⚠️ Report is 861 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #3437      +/-   ##
===========================================
+ Coverage    46.76%   53.57%   +6.80%     
===========================================
  Files          295      493     +198     
  Lines        17172    38394   +21222     
===========================================
+ Hits          8031    20569   +12538     
- Misses        8287    16172    +7885     
- Partials       854     1653     +799     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sonarqubecloud

Copy link
Copy Markdown

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