Skip to content

Control jvm thread on our side #26

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

Merged
merged 2 commits into from
Sep 1, 2022
Merged

Control jvm thread on our side #26

merged 2 commits into from
Sep 1, 2022

Conversation

AGulev
Copy link
Contributor

@AGulev AGulev commented Aug 31, 2022

Now we have the following problem:
Firebase attach thread and detach only when the main thread shut down (if shutdown thread without detach it will crash the app). Firebase does it safe - it attaches thread before detaching to avoid situation of attempt to detach thread which isn't attached.

It looks ok: just attach thread when you need it and don't detach - because you don't know who needs it except you.

But we use a bit different pattern for that. Our ThreadAttacher checks if thread attached or not, if it's not attached - then this particular attacher is "owner" and have to detach thread (see code m_IsAttached flag)

That means that even if we have create 10 ThreadAttacher instances only one instance will detach thread - the instance who attached the thread "the owner". But in case when the thread was attached by another code our ThreadAttacher will never be the owner and will never detach the thread.
That's why we have this leak.

My fix just keep management of the thread on our side using ThreadAttacher. If our ThreadAttacher attached thread - then it will detach.
But what about multiple call of the AttachCurrentThread()?

According to the documentation:

Attaching a natively-created thread causes a java.lang.Thread object to be constructed and added to the “main” ThreadGroup, making it visible to the debugger. Calling AttachCurrentThread on an already-attached thread is a no-op.

I wasn't sure if no-op in this case means does nothing or costs nothing. I checked Android source code where I found that environment pointer still returned and it is JNI_OK operation. So no-op means costs nothing (does nothing as well, but returning of the pointer is kinda operation).

@AGulev AGulev requested a review from britzl August 31, 2022 15:48
Copy link
Contributor

@britzl britzl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this work? Could you please explain what the change achieves?

@AGulev
Copy link
Contributor Author

AGulev commented Aug 31, 2022

@britzl sure! I added explanation in the first post

@@ -27,9 +29,11 @@ void FirebaseAnalytics_Safe_LogEvent(lua_State*, const char* name, const firebas
{
LogEvent(name, params, number_of_parameters);
}
#define THREAD_ATTACHER() dmAndroid::ThreadAttacher threadAttacher;
#else
#define THREAD_ATTACHER()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This define helps to avoid multiple #if defined(DM_PLATFORM_ANDROID) checks and makes code clear

@AGulev AGulev requested a review from britzl September 1, 2022 06:48
@AGulev AGulev merged commit b3fab38 into master Sep 1, 2022
@AGulev AGulev deleted the control-thread branch September 1, 2022 11:41
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 this pull request may close these issues.

2 participants