Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flagsmith panics with wrong Base URL set #94

Closed
mvrhov opened this issue Aug 31, 2023 · 7 comments · Fixed by #100
Closed

flagsmith panics with wrong Base URL set #94

mvrhov opened this issue Aug 31, 2023 · 7 comments · Fixed by #100
Assignees

Comments

@mvrhov
Copy link

mvrhov commented Aug 31, 2023

There is a panic inside flagsmith when used with wrong URL set and via open-feature.

panic({0x8dcc00?, 0xb922d0?})
        /usr/local/go/src/runtime/panic.go:920 +0x290
github.com/Flagsmith/flagsmith-go-client/v3/flagengine.GetIdentitySegments(0xc000066360, 0xc0002058c8, {0x0, 0x0, 0x0})
        /home/m/go/pkg/mod/github.com/!flagsmith/flagsmith-go-client/v3@v3.1.0/flagengine/engine.go:77 +0x71
github.com/Flagsmith/flagsmith-go-client/v3/flagengine.getIdentityFeatureStatesMap(0xc000066360, 0xc0002058c8, {0x0, 0x0, 0x0})
        /home/m/go/pkg/mod/github.com/!flagsmith/flagsmith-go-client/v3@v3.1.0/flagengine/engine.go:96 +0x172
github.com/Flagsmith/flagsmith-go-client/v3/flagengine.GetIdentityFeatureStates(0xc000066360, 0xc0002058c8, {0x0, 0x0, 0x0})
        /home/m/go/pkg/mod/github.com/!flagsmith/flagsmith-go-client/v3@v3.1.0/flagengine/engine.go:41 +0x7e
github.com/Flagsmith/flagsmith-go-client/v3.(*Client).getIdentityFlagsFromEnvironment(0xc000100be0, {0x93a4cb, 0x10}, {0x0, 0x0, 0x0})
        /home/m/go/pkg/mod/github.com/!flagsmith/flagsmith-go-client/v3@v3.1.0/client.go:189 +0x1b3
github.com/Flagsmith/flagsmith-go-client/v3.(*Client).GetIdentityFlags(0xc000100be0, {0x9a5b00, 0xbcf3c0}, {0x93a4cb, 0x10}, {0x0, 0x0, 0x0})
        /home/m/go/pkg/mod/github.com/!flagsmith/flagsmith-go-client/v3@v3.1.0/client.go:98 +0xec
github.com/open-feature/go-sdk-contrib/providers/flagsmith/pkg.(*Provider).resolveFlag(0xc000016ef0, {0x9a5b00, 0xbcf3c0}, {0x93b2b4, 0x12}, {0x8c77a0, 0xb70b40}, 0xc0001a03c0)
        /home/m/go/pkg/mod/github.com/open-feature/go-sdk-contrib/providers/flagsmith@v0.1.4/pkg/provider.go:76 +0xd2d
github.com/open-feature/go-sdk-contrib/providers/flagsmith/pkg.(*Provider).BooleanEvaluation(0xc000016ef0, {0x9a5b00, 0xbcf3c0}, {0x93b2b4, 0x12}, 0x0, 0xc0001a03c0)
        /home/m/go/pkg/mod/github.com/open-feature/go-sdk-contrib/providers/flagsmith@v0.1.4/pkg/provider.go:134 +0xd3
github.com/open-feature/go-sdk/pkg/openfeature.(*Client).evaluate(0xc0000662a0, {0x9a5b00, 0xbcf3c0}, {0x93b2b4, 0x12}, 0x0, {0x8c77a0, 0xb70b40}, {{0x93a4cb, 0x10}, ...}, ...)
        /home/m/go/pkg/mod/github.com/open-feature/go-sdk@v1.5.1/pkg/openfeature/client.go:714 +0x17c4
github.com/open-feature/go-sdk/pkg/openfeature.(*Client).BooleanValue(0xc0000662a0, {0x9a5b00, 0xbcf3c0}, {0x93b2b4, 0x12}, 0x0, {{0x93a4cb, 0x10}, 0xc0001a0300}, {0x0, ...})
        /home/m/go/pkg/mod/github.com/open-feature/go-sdk@v1.5.1/pkg/openfeature/client.go:303 +0x332
main.helloServer({0x9a53c0, 0xc0001fc000}, 0xc0001f6000)
        /home/m/ff/flagsmith/main.go:54 +0x39a
net/http.HandlerFunc.ServeHTTP(0x97e160, {0x9a53c0, 0xc0001fc000}, 0xc0001f6000)

This is the main.go

package main

import (
	"context"
	"github.com/Flagsmith/flagsmith-go-client/v3"
	openFlagsmith "github.com/open-feature/go-sdk-contrib/providers/flagsmith/pkg"
	"github.com/open-feature/go-sdk/pkg/openfeature"
	"io"
	"log"
	"net/http"
	"time"
)

var feature *openfeature.Client
var fc *flagsmith.Client

func init() {
	ctx := context.Background()
	// Initialize the flagsmith client
	client := flagsmith.NewClient(
		"ser.redactedredacted",
		flagsmith.WithLocalEvaluation(context.Background()),
		flagsmith.WithBaseURL("http://localhost:8000/"),
		//flagsmith.WithBaseURL("http://localhost:8000/api/v1/"),
		flagsmith.WithEnvironmentRefreshInterval(30*time.Second),
		flagsmith.WithAnalytics(ctx),
		flagsmith.WithRetries(3, 5*time.Second),
		flagsmith.WithDefaultHandler(func(s string) (flagsmith.Flag, error) {
			return flagsmith.Flag{Enabled: false}, nil
		}),
	)

	// Initialize the flagsmith provider
	provider := openFlagsmith.NewProvider(client, openFlagsmith.WithUsingBooleanConfigValue())

	openfeature.SetProvider(provider)

	// Create open feature client
	feature = openfeature.NewClient("prod")
	fc = client
}

func helloServer(w http.ResponseWriter, req *http.Request) {
	f, err := fc.GetEnvironmentFlags(context.Background())
	_, err = f.IsFeatureEnabled("bool_feature")
	_, err = f.IsFeatureEnabled("engine_status")
	_ = err

	evalCtx := openfeature.NewEvaluationContext(
		"openfeature_user",
		map[string]interface{}{},
	)

	if enabled, err := feature.BooleanValue(context.Background(), "engine_status", false, evalCtx); enabled && err == nil {
		io.WriteString(w, "Feature enabled\n")
	} else {
		io.WriteString(w, "hello, world!\n")
	}
}

func main() {
	http.HandleFunc("/", helloServer)
	log.Fatal(http.ListenAndServe(":8080", nil))
}
@gagantrivedi
Copy link
Member

@mvrhov Thanks - I will take a look

@gagantrivedi gagantrivedi self-assigned this Aug 31, 2023
@gagantrivedi
Copy link
Member

gagantrivedi commented Sep 4, 2023

@mvrhov I'm unable to replicate the error you're encountering. Could you please confirm that the code you've provided is accurate and corresponds to the error you're experiencing?

@gagantrivedi
Copy link
Member

I am going to close this for now. Feel free to reopen with more information if you are still facing this

@mvrhov
Copy link
Author

mvrhov commented Oct 18, 2023

It seems that the above example doesn't reproduce it each time. However I've jut got this in one production server where we have disabled flagsmith . (By not setting up the URL)

@mvrhov
Copy link
Author

mvrhov commented Oct 19, 2023

Ah. I have figured it out. flagsmith.WithBaseURL("http://localhost:8000/"). You MUST have server responding on that URL. But not flagsmith itself. And it will crash with above panic

@mvrhov
Copy link
Author

mvrhov commented Nov 7, 2023

Can you please release/tag 3.1.1 now that both #100 and #101 are merged. Thanks.

@gagantrivedi
Copy link
Member

Can you please release/tag 3.1.1 now that both #100 and #101 are merged. Thanks.

Released: https://github.com/Flagsmith/flagsmith-go-client/releases/tag/v3.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants