PostHog / posthog-go

Official PostHog Go library
MIT License
20 stars 17 forks source link

Impossible to close the client with feature flag poller #28

Open zaynetro opened 7 months ago

zaynetro commented 7 months ago

Hi! It is impossible to close posthog.Client when feature flags are enabled and no requests to get all flags were made.

What happens:

  1. FeatureFlagsPoller.run starts
  2. Execution is blocked on poller.loaded <- false in fetchNewFeatureFlags because nobody reads from that channel
  3. Infinite loop never starts so <-poller.shutdown is never consumed hence blocking Client.Close method here.
  4. Things work if we GetFeatureFlags at least once. It starts reading poller.loaded channel here

Minimal reproduction example:

package main

import (
    "fmt"
    "os"

    "github.com/posthog/posthog-go"
)

func main() {
    client, err := posthog.NewWithConfig("123", posthog.Config{
        PersonalApiKey: "123",
    })
    if err != nil {
        fmt.Println("could not initialize posthog client", err)
        os.Exit(1)
    }

    // NOTE: Uncomment for things to start working
    // flags, err := client.GetFeatureFlags()
    // fmt.Println("Flags", flags, err)

    fmt.Println("Trying to close the client...")
    if err := client.Close(); err != nil {
        fmt.Println("Failed to close:", err)
    }

    fmt.Println("Client closed.")
}