Open zaynetro opened 6 months ago
We were running into this as well, and it appears fixed by #38 (merged as commit 87b23fe
). Upgrading to v0.0.0-20240327112532-87b23fe11103
removed the data race from our logs. Thanks for the fix! This can probably be closed now.
indeed, thanks!
This is not fully fixed. Here is an example:
package main
import (
"sync"
"testing"
"time"
"github.com/posthog/posthog-go"
)
var pollInterval = 2 * time.Second
func nextTick() time.Duration {
now := time.Now()
elapsed := time.Duration(now.Minute()*60+now.Second()) * time.Second
remaining := pollInterval - elapsed%pollInterval
return remaining.Abs()
}
func TestRaces(t *testing.T) {
client, err := posthog.NewWithConfig("<set your API key>", posthog.Config{
PersonalApiKey: "<set your personal API key>",
Verbose: true,
NextFeatureFlagsPollingTick: nextTick,
})
if err != nil {
t.Fatal(err)
}
var wg sync.WaitGroup
for i := 0; i < 2; i += 1 {
wg.Add(1)
go func() {
defer wg.Done()
timer := time.NewTimer(nextTick())
defer timer.Stop()
<-timer.C
client.IsFeatureEnabled(posthog.FeatureFlagPayload{
Key: "hello",
DistinctId: "my-user",
})
}()
}
wg.Wait()
<-time.After(6 * time.Second)
}
SDK with feature flag poller enabled has data races:
There seem two be a couple of offenders:
poller.fetchedFlagsSuccessfullyOnce
(this and this)poller.featureFlags
and everywhere where they are read withpoller.GetFeatureFlags()
Full log: test.log