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

Feature Value fails in multiprocess environment #103

Open
sameerjj opened this issue May 8, 2024 · 16 comments
Open

Feature Value fails in multiprocess environment #103

sameerjj opened this issue May 8, 2024 · 16 comments

Comments

@sameerjj
Copy link

sameerjj commented May 8, 2024

I have a ForegroundService which requires a feature flag, I declare it to have a separate process in the manifest:
android:process=":location"

when I call
gb.feature("baseurl").value as String
it fails, despite it working in the main process.

@sameerjj
Copy link
Author

sameerjj commented May 8, 2024

sample initialization code:

val gb = GBSDKBuilder(
                apiKey = "sdk-key",
                hostURL = "https://cdn.growthbook.io/",
                networkDispatcher = DefaultGBNetworkClient(),
                attributes = mapOf("app_id" to appId),
                trackingCallback = { gbExperiment, gbExperimentResult ->
                    Log.d(TAG, "Variation Id: ${gbExperimentResult.variationId}")
                },
            ).initialize()
            
            val baseUrl = gb.feature("samplekey").value as String
            val legacyEndpoints = gb.feature("sampleflag").off
            

@Bohdan-Kim
Copy link
Contributor

Bohdan-Kim commented May 8, 2024

Hello @sameerjj !
Could you please give us more details about "fails".
Is it java.lang.NullPointerException?

@Bohdan-Kim
Copy link
Contributor

Try to use
gb.feature("baseurl").value as String?
instead of
gb.feature("baseurl").value as String

@sameerjj
Copy link
Author

sameerjj commented May 8, 2024 via email

@Bohdan-Kim
Copy link
Contributor

Bohdan-Kim commented May 9, 2024

@sameerjj It is not safe to cast. gb.feature("baseurl").value is null because it takes some time to get features from Backend. Some delay needed between SDK initialization and accessing the feature. The delay needed only for the first time. Once features have been got from Backend they will have been cached. We tried the code you provided. The features were not got because of cast to String.
One more point is make sure network request is allowed from the thread you are trying to get features.
Or maybe you want to initialize the SDK from main thread but to access features from another thread?

@sameerjj
Copy link
Author

sameerjj commented May 10, 2024

Hi @Bohdan-Kim, is there a way to know when the initialization is complete? .initialize() is synchronous in latest version 1.1.47

@sameerjj
Copy link
Author

btw, I induced a delay of 1 second between initialize() and the use of the feature, and it still didn't work in the foreground service separate process. I also tried a retry mechanism. It simply doesn't load here.
Anyways, I'm just going to use my own cache, but there's definitely an issue here

@Bohdan-Kim
Copy link
Contributor

Bohdan-Kim commented May 10, 2024

The issue is hard to reproduce.

@Bohdan-Kim
Copy link
Contributor

is there a way to know when the initialization is complete?
setRefreshHandler() in GBSDKBuilder class. setRefreshHandler() can be used to be notified about features refresh.

@Bohdan-Kim
Copy link
Contributor

Bohdan-Kim commented May 10, 2024

We tried just to wrap the code with thread {}. Everything works the same. We understand that this is not the foreground service.

@Bohdan-Kim
Copy link
Contributor

It seems like we reproduced the issue.

@Bohdan-Kim
Copy link
Contributor

Bohdan-Kim commented May 10, 2024

This foreground service works fine for us:
https://gist.github.com/Bohdan-Kim/d2dd0f0871fe3019996e7adab368d79d

@sameerjj
Copy link
Author

sameerjj commented May 10, 2024

Hi @Bohdan-Kim, you would have to add to your manifest that it's a separate process as I mentioned

<service
            android:name="com.example.growthbookdemoapp.ForegroundService"
            android:exported="false"
            android:foregroundServiceType="location"
            android:process=":location" />

@Bohdan-Kim
Copy link
Contributor

Bohdan-Kim commented May 11, 2024

We followed your instruction and noticed that when foregroundServiceType and process attributes are applied the features list are not restored from cache.

@Bohdan-Kim
Copy link
Contributor

The pull request was created according to this issue.
After merge in main branch there will be an option to pass Service context to the GrowthBook SDK. For similar issues we will ask to paste this line:

CachingAndroid.consumeContext(this)

where this is a reference to a Service instance.

vazarkevych added a commit that referenced this issue May 23, 2024
@Bohdan-Kim
Copy link
Contributor

We released v1.1.58 version of the SDK. The changes from pull request are available in v1.1.58

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

No branches or pull requests

2 participants