Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

fix(tokenizer): fix concurrent map writes in jwt tokenizer (#9960)

Pandeyabae0c7f27b9
diff --git a/pkg/tokenizer/jwttokenizer/provider.go b/pkg/tokenizer/jwttokenizer/provider.go
index 2e602d32f4..2163c27850 100644
--- a/pkg/tokenizer/jwttokenizer/provider.go
+++ b/pkg/tokenizer/jwttokenizer/provider.go
@@ -3,6 +3,7 @@ package jwttokenizer
import (
"context"
"slices"
+ "sync"
"time"
"github.com/SigNoz/signoz/pkg/cache"
@@ -29,6 +30,7 @@ type provider struct {
cache cache.Cache
tokenStore authtypes.TokenStore
lastObservedAtCache *ristretto.Cache[string, map[valuer.UUID]time.Time]
+ lastObservedAtMtx sync.RWMutex
stopC chan struct{}
}
@@ -61,6 +63,7 @@ func New(ctx context.Context, providerSettings factory.ProviderSettings, config
cache: cache,
tokenStore: tokenStore,
lastObservedAtCache: lastObservedAtCache,
+ lastObservedAtMtx: sync.RWMutex{},
stopC: make(chan struct{}),
}), nil
}
@@ -161,6 +164,10 @@ func (provider *provider) SetLastObservedAt(ctx context.Context, accessToken str
return nil
}
+ // Ristretto is returning the same map on concurrent calls.
+ // Lock the last observed at cache to avoid race condition on writing to the same map obtained from the cache.
+ provider.lastObservedAtMtx.Lock()
+ defer provider.lastObservedAtMtx.Unlock()
cachedLastObservedAts, ok := provider.lastObservedAtCache.Get(claims.OrgID)
if !ok {
cachedLastObservedAts = make(map[valuer.UUID]time.Time)
diff --git a/pkg/tokenizer/jwttokenizer/provider_test.go b/pkg/tokenizer/jwttokenizer/provider_test.go
new file mode 100644
index 0000000000..f15da7ea8f
--- /dev/null
+++ b/pkg/tokenizer/jwttokenizer/provider_test.go
@@ -0,0 +1,99 @@
+package jwttokenizer
+
+import (
+ "context"
+ "sync"
+ "testing"
+ "time"
+
+ "github.com/DATA-DOG/go-sqlmock"
+ "github.com/SigNoz/signoz/pkg/cache"
+ "github.com/SigNoz/signoz/pkg/cache/cachetest"
+ "github.com/SigNoz/signoz/pkg/instrumentation/instrumentationtest"
+ "github.com/SigNoz/signoz/pkg/sqlstore"
+ "github.com/SigNoz/signoz/pkg/sqlstore/sqlstoretest"
+ "github.com/SigNoz/signoz/pkg/tokenizer"
+ "github.com/SigNoz/signoz/pkg/tokenizer/tokenizerstore/sqltokenizerstore"
+ "github.com/SigNoz/signoz/pkg/types"
+ "github.com/SigNoz/signoz/pkg/types/authtypes"
+ "github.com/SigNoz/signoz/pkg/valuer"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func newTestProvider(t *testing.T) tokenizer.Tokenizer {
+ instrumentation := instrumentationtest.New()
+ cache, err := cachetest.New(cache.Config{
+ Memory: cache.Memory{
+ NumCounters: 1000,
+ MaxCost: 1 << 26,
+ },
+ })
+ require.NoError(t, err)
+ sqlstore := sqlstoretest.New(sqlstore.Config{Provider: "sqlite"}, sqlmock.QueryMatcherRegexp)
+ require.NoError(t, err)
+
+ provider, err := New(
+ context.Background(),
+ instrumentation.ToProviderSettings(),
+ tokenizer.Config{
+ Rotation: tokenizer.RotationConfig{
+ Interval: 1 * time.Second, // 1 second
+ Duration: 60 * time.Second, // 60 seconds
+ },
+ Lifetime: tokenizer.LifetimeConfig{
+ Idle: 7 * 24 * time.Hour, // 7 days
+ Max: 30 * 24 * time.Hour, // 30 days
+ }},
+ cache,
+ sqltokenizerstore.NewStore(sqlstore),
+ )
+ require.NoError(t, err)
+
+ return provider
+}
+
+func TestLastObservedAt_Concurrent(t *testing.T) {
+ provider := newTestProvider(t)
+ orgID := valuer.GenerateUUID()
+
+ token1, err := provider.CreateToken(
+ context.Background(),
+ &authtypes.Identity{
+ UserID: valuer.GenerateUUID(),
+ OrgID: orgID,
+ Role: types.RoleAdmin,
+ Email: valuer.MustNewEmail("[email protected]"),
+ },
+ map[string]string{},
+ )
+ require.NoError(t, err)
+
+ token2, err := provider.CreateToken(
+ context.Background(),
+ &authtypes.Identity{
+ UserID: valuer.GenerateUUID(),
+ OrgID: orgID,
+ Role: types.RoleAdmin,
+ Email: valuer.MustNewEmail("[email protected]"),
+ },
+ map[string]string{},
+ )
+ require.NoError(t, err)
+
+ wg := sync.WaitGroup{}
+ for i := 0; i < 5; i++ {
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ assert.NoError(t, provider.SetLastObservedAt(context.Background(), token1.AccessToken, time.Now()))
+ }()
+
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ assert.NoError(t, provider.SetLastObservedAt(context.Background(), token2.AccessToken, time.Now()))
+ }()
+ }
+ wg.Wait()
+}