Skip to content

Commit 2efc3a3

Browse files
fix: protect concurrent map access with mutexLock (#15)
1 parent b515c99 commit 2efc3a3

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

traceloop-sdk/prompt_registry.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ func (instance *Traceloop) populatePromptRegistry() {
3131
return
3232
}
3333

34+
instance.registryMutex.Lock()
3435
for _, prompt := range response.Prompts {
3536
instance.promptRegistry[prompt.Key] = &prompt
3637
}
38+
instance.registryMutex.Unlock()
3739
}
3840

3941
func (instance *Traceloop) pollPrompts() {
@@ -55,6 +57,8 @@ func (instance *Traceloop) pollPrompts() {
5557
}
5658

5759
func (instance *Traceloop) getPromptVersion(key string) (*model.PromptVersion, error) {
60+
instance.registryMutex.RLock()
61+
defer instance.registryMutex.RUnlock()
5862
if instance.promptRegistry[key] == nil {
5963
return nil, fmt.Errorf("prompt with key %s not found", key)
6064
}

traceloop-sdk/sdk.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"log"
77
"net/http"
88
"os"
9+
"sync"
910
"time"
1011

1112
"go.opentelemetry.io/otel/attribute"
@@ -21,6 +22,7 @@ const PromptsPath = "/v1/traceloop/prompts"
2122
type Traceloop struct {
2223
config Config
2324
promptRegistry model.PromptRegistry
25+
registryMutex sync.RWMutex
2426
tracerProvider *trace.TracerProvider
2527
http.Client
2628
}

0 commit comments

Comments
 (0)