Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions plugins/maxim/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,12 @@ func (plugin *Plugin) PostHook(ctx *schemas.BifrostContext, result *schemas.Bifr
return result, bifrostErr, nil
}

// Capture context values BEFORE goroutine to avoid race conditions
// when the same context is reused across multiple requests
generationID, hasGenerationID := ctx.Value(GenerationIDKey).(string)
traceID, hasTraceID := ctx.Value(TraceIDKey).(string)
tags, hasTags := ctx.Value(TagsKey).(map[string]string)

go func() {
requestType, _, model := bifrost.GetResponseFields(result, bifrostErr)

Expand All @@ -507,8 +513,7 @@ func (plugin *Plugin) PostHook(ctx *schemas.BifrostContext, result *schemas.Bifr
if err != nil {
return
}
generationID, ok := ctx.Value(GenerationIDKey).(string)
if ok {
if hasGenerationID {
if bifrostErr != nil {
// Safely extract message from nested error
message := ""
Expand Down Expand Up @@ -569,14 +574,12 @@ func (plugin *Plugin) PostHook(ctx *schemas.BifrostContext, result *schemas.Bifr

logger.EndGeneration(generationID)
}
traceID, ok := ctx.Value(TraceIDKey).(string)
if ok {
if hasTraceID {
logger.EndTrace(traceID)
}

// add tags to the generation and trace
tags, ok := ctx.Value(TagsKey).(map[string]string)
if ok {
if hasTags {
for key, value := range tags {
if generationID != "" {
logger.AddTagToGeneration(generationID, key, value)
Expand Down
7 changes: 4 additions & 3 deletions plugins/semanticcache/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,10 @@ func (plugin *Plugin) PostHook(ctx *schemas.BifrostContext, res *schemas.Bifrost
}
}

// Get metadata from context BEFORE goroutine to avoid race conditions
// when the same context is reused across multiple requests
paramsHash, _ := ctx.Value(requestParamsHashKey).(string)

// Cache everything in a unified VectorEntry asynchronously to avoid blocking the response
plugin.waitGroup.Add(1)
go func() {
Expand All @@ -577,9 +581,6 @@ func (plugin *Plugin) PostHook(ctx *schemas.BifrostContext, res *schemas.Bifrost
cacheCtx, cancel := context.WithTimeout(context.Background(), CacheSetTimeout)
defer cancel()

// Get metadata from context
paramsHash, _ := ctx.Value(requestParamsHashKey).(string)

// Build unified metadata with provider, model, and all params
unifiedMetadata := plugin.buildUnifiedMetadata(provider, model, paramsHash, hash, cacheKey, cacheTTL)

Expand Down