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

Avoid deadlocking the Packet Tunnel after a reboot #7089

Merged
merged 1 commit into from
Oct 30, 2024

Conversation

buggmagnet
Copy link
Contributor

@buggmagnet buggmagnet commented Oct 28, 2024

This PR fixes a deadlock that was triggered by rebooting the phone when the VPN was connected.


This change is Reviewable

@buggmagnet buggmagnet added the iOS Issues related to iOS label Oct 28, 2024
@buggmagnet buggmagnet self-assigned this Oct 28, 2024
Copy link

linear bot commented Oct 28, 2024

Copy link
Contributor

@rablador rablador left a comment

Choose a reason for hiding this comment

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

Reviewed 3 of 3 files at r1, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @buggmagnet)


ios/PacketTunnel/PacketTunnelProvider/PacketTunnelProvider.swift line 176 at r1 (raw file):

    }

    private func performSettingsMigration() {

Although we won't deadlock here, the code could potentially repeat forever. That's still by design then?

Copy link
Collaborator

@mojganii mojganii left a comment

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @buggmagnet)


ios/PacketTunnel/PacketTunnelProvider/PacketTunnelProvider.swift line 177 at r1 (raw file):

    private func performSettingsMigration() {
        var hasNotMigratead = true

typo : hasNotMigrated


ios/PacketTunnel/PacketTunnelProvider/PacketTunnelProvider.swift line 178 at r1 (raw file):

    private func performSettingsMigration() {
        var hasNotMigratead = true
        repeat {

this loop tries until the migration is done, is it the thing? Can't return default value when it fails after last retry?

Copy link
Collaborator

@mojganii mojganii left a comment

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, 4 unresolved discussions (waiting on @buggmagnet)


ios/PacketTunnel/PacketTunnelProvider/PacketTunnelProvider.swift line 179 at r1 (raw file):

        var hasNotMigratead = true
        repeat {
            migrationManager.migrateSettings(

this code can be shorten like this :

Code snippet:

      while true {
            migrationManager.migrateSettings(
                store: SettingsManager.store,
                migrationCompleted: { [unowned self] migrationResult in
                    switch migrationResult {
                    case .success:
                        providerLogger.debug("Successful migration from PacketTunnel")
                        break
                    case .nothing:
                        providerLogger.debug("Attempted migration from PacketTunnel, but found nothing to do")
                        break
                    case let .failure(error):
                        providerLogger
                            .error(
                                "Failed migration from PacketTunnel: \(error)"
                            )
                        // `next` returns an Optional value, but this iterator is guaranteed to always have a next value
                        if let delay = migrationFailureIterator.next() {
                            providerLogger.error("Retrying migration in \(delay.timeInterval) seconds")
                            // Block the launch of the Packet Tunnel for as long as the settings migration fail.
                            // The process watchdog introduced by iOS 17 will kill this process after 60 seconds.
                            Thread.sleep(forTimeInterval: delay.timeInterval)
                        } else {
                            break
                        }
                    }
                }
            )
        }

@buggmagnet buggmagnet force-pushed the app-hangs-on-launch-after-reboot-ios-909 branch from ee8b38e to 7f3d297 Compare October 29, 2024 10:18
Copy link
Contributor Author

@buggmagnet buggmagnet left a comment

Choose a reason for hiding this comment

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

Reviewable status: 2 of 3 files reviewed, 3 unresolved discussions (waiting on @mojganii and @rablador)


ios/PacketTunnel/PacketTunnelProvider/PacketTunnelProvider.swift line 176 at r1 (raw file):

Previously, rablador (Jon Petersson) wrote…

Although we won't deadlock here, the code could potentially repeat forever. That's still by design then?

Yes


ios/PacketTunnel/PacketTunnelProvider/PacketTunnelProvider.swift line 178 at r1 (raw file):

Previously, mojganii wrote…

this loop tries until the migration is done, is it the thing? Can't return default value when it fails after last retry?

This is the intention, the PacketTunnel either succeeds the migration, or gets killed after 1 minute because of the new watchdog timer introduced in iOS 17


ios/PacketTunnel/PacketTunnelProvider/PacketTunnelProvider.swift line 179 at r1 (raw file):

Previously, mojganii wrote…

this code can be shorten like this :

This suggestion is problematic because it forces the PacketTunnelProvider to sleep in the callback from the MigrationManager effectively blocking the UI Process to perform migration while the sleep is happening.

Copy link
Collaborator

@mojganii mojganii left a comment

Choose a reason for hiding this comment

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

Reviewable status: 2 of 3 files reviewed, 3 unresolved discussions (waiting on @buggmagnet and @rablador)


ios/PacketTunnel/PacketTunnelProvider/PacketTunnelProvider.swift line 179 at r1 (raw file):

Previously, buggmagnet wrote…

This suggestion is problematic because it forces the PacketTunnelProvider to sleep in the callback from the MigrationManager effectively blocking the UI Process to perform migration while the sleep is happening.

My mistake, that's true.

rablador
rablador previously approved these changes Oct 29, 2024
Copy link
Contributor

@rablador rablador left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r2, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @buggmagnet and @mojganii)

Copy link
Collaborator

@pinkisemils pinkisemils left a comment

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @buggmagnet and @mojganii)


ios/MullvadREST/RetryStrategy/RetryStrategy.swift line 86 at r2 (raw file):

            delay: .exponentialBackoff(
                initial: .seconds(5),
                multiplier: UInt64(2),

As discussed on the day, I think there is no tangible difference between attempting to migrate 4 times a minute vs 12 - so we might as well go for the option that has a lower likelihood of getting killed and leave the multiplier at 1.

Copy link
Collaborator

@pinkisemils pinkisemils left a comment

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @buggmagnet and @mojganii)


ios/MullvadREST/RetryStrategy/RetryStrategy.swift line 87 at r2 (raw file):

                initial: .seconds(5),
                multiplier: UInt64(2),
                maxDelay: .minutes(1)

As discussed on the day, I think there is no tangible difference between attempting to migrate 4 times a minute vs 12 - so we might as well go for the option that has a lower likelihood of getting killed and leave the multiplier at 1.

mojganii
mojganii previously approved these changes Oct 30, 2024
Copy link
Collaborator

@mojganii mojganii left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @buggmagnet)

Copy link
Contributor Author

@buggmagnet buggmagnet left a comment

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @mojganii)


ios/MullvadREST/RetryStrategy/RetryStrategy.swift line 86 at r2 (raw file):

Previously, pinkisemils (Emīls Piņķis) wrote…

As discussed on the day, I think there is no tangible difference between attempting to migrate 4 times a minute vs 12 - so we might as well go for the option that has a lower likelihood of getting killed and leave the multiplier at 1.

Done

Copy link
Contributor

@rablador rablador left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 1 of 1 files at r3, all commit messages.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @buggmagnet and @mojganii)

Copy link
Collaborator

@pinkisemils pinkisemils left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 3 files at r1, 1 of 1 files at r2, 1 of 1 files at r3, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @buggmagnet and @mojganii)

@buggmagnet buggmagnet force-pushed the app-hangs-on-launch-after-reboot-ios-909 branch from d42c0ab to 6829313 Compare October 30, 2024 10:15
@buggmagnet buggmagnet merged commit 3bbdf34 into main Oct 30, 2024
10 of 11 checks passed
@buggmagnet buggmagnet deleted the app-hangs-on-launch-after-reboot-ios-909 branch October 30, 2024 10:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
iOS Issues related to iOS
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants