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

android: lazily init app in Client #563

Merged
merged 1 commit into from
Nov 21, 2024
Merged

android: lazily init app in Client #563

merged 1 commit into from
Nov 21, 2024

Conversation

kari-ts
Copy link
Collaborator

@kari-ts kari-ts commented Nov 19, 2024

-Lazily init app in Client to ensure that we aren't trying to make any local API calls before app has been initialized.
-Add @volatile to ensure that isInitialized is always visible across threads and uses the updated value

Updates tailscale/tailscale#14125

@kari-ts kari-ts requested a review from barnstar November 19, 2024 00:18
android/src/main/java/com/tailscale/ipn/App.kt Outdated Show resolved Hide resolved
initOnce() // Calls the synchronized initialization logic
}
}

@Synchronized
private fun initOnce() {
if (isInitialized) {
return
}
isInitialized = true
Copy link
Member

Choose a reason for hiding this comment

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

The method is synchronized so it's OK, but let's defer setting isInitialized to the end of the method since, technically, nothing isInitialzied at this point.

Does kotlin have a built in "run this block of code once and only once" construct? In Swift, we would do dispatch_once() here (or use a closure) instead of a volatile bool and the synchronization.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

it doesn't afaik. I think the best we can do is the combination of the volatile isInitialized and the synchronization.

-Lazily init app in Client to ensure that we aren't trying to make any local API calls before app has been initialized.
-Add @volatile to ensure that isInitialized is always visible across threads and uses the updated value

Updates tailscale/tailscale#14125

Signed-off-by: kari-ts <[email protected]>
@kari-ts kari-ts merged commit c56420b into main Nov 21, 2024
4 checks passed
@kari-ts kari-ts deleted the kari/app branch November 21, 2024 17:12
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