Skip to content

fix(android): prevent onLost from triggering a session disconnect when the VPN is re-established just after#6605

Merged
conectado merged 2 commits intomainfrom
fix/kotlin/crash-loop
Sep 5, 2024
Merged

fix(android): prevent onLost from triggering a session disconnect when the VPN is re-established just after#6605
conectado merged 2 commits intomainfrom
fix/kotlin/crash-loop

Conversation

@conectado
Copy link
Contributor

On Android when when we receive the onLost callback for the VPN we disconnect the tunnel, since that's the only way to find when the user has disconnected the VPN from outside Firezone, otherwise this would kill network connection when it's this happens.

But onLost was also called when we call establish on the tunnel to recreate it and there's no obvious way to distinguish between both cases.

So we previously just stopped monitoring onLost while we were executing establish. The problem with this approach was that sometimes onLost might have a delayed since establish is called.

Therefore, to fix this, we changed the onLost call to delay the execution of session.disconnect for 2 second, if within this grace period we get a linkPropertiesChanged for our interface, which is always called after the network is re-created, we cancel the execution, otherwise we go ahead an disconnect the session.

@conectado conectado requested a review from jamilbk September 5, 2024 20:18
@vercel
Copy link

vercel bot commented Sep 5, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
firezone ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 5, 2024 8:24pm

Copy link
Member

@jamilbk jamilbk left a comment

Choose a reason for hiding this comment

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

LGTM, just some tidying up suggestions. I think with the comment we can remove the logging.

Comment on lines +25 to +26
Log.d("DisconnectMonitor", "properties: $linkProperties")

Copy link
Member

Choose a reason for hiding this comment

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

Can remove these now I think.

Comment on lines +49 to +51
// We delay the execution of disconnecting the tunnel when the network is lost since
// when the tunnel is rebuild we get an onLost just like with disabling the VPN and we
// can't distinguish between those save for getting an onLinkProperties changed later
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// We delay the execution of disconnecting the tunnel when the network is lost since
// when the tunnel is rebuild we get an onLost just like with disabling the VPN and we
// can't distinguish between those save for getting an onLinkProperties changed later
// We can't easily tell the difference between a user disconnecting the VPN from
// system settings, and the system "disconnecting" our VPN due to the fd being swapped out during buildVpnService. Both call onLost with the VPN network ID.
// If we're rebuilding the VPN, however, we'll expect an onLinkPropertiesChanged to happen soon after onLost, so delay the tunnel disconnect here and clear the scheduled disconnect if we "resume" the VPN within a couple seconds.

// can't distinguish between those save for getting an onLinkProperties changed later
sessionStopperHandle.postDelayed(
{
Log.d("DisconnectMonitor", "Disconnect tunnel service due to network lost $network")
Copy link
Member

Choose a reason for hiding this comment

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

Can clean up a tiny bit.

Suggested change
Log.d("DisconnectMonitor", "Disconnect tunnel service due to network lost $network")

@conectado conectado added this pull request to the merge queue Sep 5, 2024
Merged via the queue into main with commit a97f015 Sep 5, 2024
@conectado conectado deleted the fix/kotlin/crash-loop branch September 5, 2024 21:51
conectado added a commit that referenced this pull request Sep 5, 2024
conectado added a commit that referenced this pull request Sep 5, 2024
github-merge-queue bot pushed a commit that referenced this pull request Sep 13, 2024
Co-authored-by: Jamil <jamilbk@users.noreply.github.com>
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