Skip to content

VPNConnector fails to initialize when the persisted connection uses a protocol that is no longer valid #33

Open
Miixms99/python-proton-vpn-api-core
#1

Description

@indep-arg

VPNConnector._get_current_connection() does:

if protocol := self._registry.get(persisted_parameters.protocol):

which expects None when there's no valid implementation, as the Registry.get() docstring says. But Registry.get() raises RuntimeError instead (proton/vpn/core/registry.py:116), so VPNConnector.get() / initialize_state() blows up and the client can't create the connector at all.

This happens whenever connection_persistence.json points to a protocol whose validate() returns False on the next start. For example, a protun-* connection (these are now forced for free users) and then running the app where protun isn't valid (snap/flatpak, older NM, missing platform module). The file stays there, so every start fails the same way.

Steps to reproduce

  1. Connect with protun-smart and kill the app, so connection_persistence.json stays.
  2. Start the app in an environment where ProTun.validate() returns False.

With the real registry and NetworkManager:

registered: ['openvpn-udp', 'openvpn-tcp', 'wireguard', 'protun-udp', 'protun-tcp', 'protun-tls', 'protun-smart']
valid:      ['wireguard', 'openvpn-udp', 'openvpn-tcp']
initialize_state -> RuntimeError: No valid implementation found for protun-smart

Expected

Initial state is Disconnected, as for any persisted connection that can't be restored.

Possible fix

Either make Registry.get() return None as documented, or catch the RuntimeError in _get_current_connection().

Found on master @ 1b6dcda (proton-vpn-api-core 5.8.7), Ubuntu 24.04, Python 3.12, NetworkManager 1.46.0.

Test (fails on current master)
import tempfile
from unittest.mock import Mock

import pytest

from proton.vpn.connection import states
from proton.vpn.connection.interfaces import ProtocolPorts, VPNServer
from proton.vpn.connection.persistence import ConnectionParameters, ConnectionPersistence
from proton.vpn.core.registry import Registry
from proton.vpn.core.vpnconnector import VPNConnector


@pytest.mark.asyncio
async def test_initial_state_is_disconnected_when_persisted_protocol_is_not_available():
    persistence = ConnectionPersistence(tempfile.mkdtemp())
    persistence.save(ConnectionParameters(
        connection_id="f3c1e0a0-0000-0000-0000-000000000000",
        backend="linuxnetworkmanager",
        protocol="protun-smart",
        server=VPNServer(
            server_ip="1.1.1.1",
            openvpn_ports=ProtocolPorts([1194], [443], []),
            wireguard_ports=ProtocolPorts([51820], [443], []),
            domain="node.protonvpn.net", x25519pk="key",
            server_id="id", server_name="CH#1", has_ipv6_support=False
        )
    ))

    unavailable = Mock()
    unavailable.get_key.return_value = "protun-smart"
    unavailable.validate.return_value = False  # e.g. running inside flatpak/snap
    registry = Registry()
    registry.register(unavailable)

    session_holder = Mock()
    session_holder.session.logged_in = True

    connector = VPNConnector(
        session_holder, Mock(), Mock(), registry,
        connection_persistence=persistence,
        publisher=Mock(), port_forward_file_handler=Mock()
    )

    state = await connector._get_initial_state()

    assert isinstance(state, states.Disconnected)

Activity

  1. Miixms99 commented on Nov 16, 2026

    @Miixms99

    I've opened a fix for this in #43.

    Problem

    On startup, VPNConnector.initialize_state() calls _get_current_connection(), which restores the last connection from connection_persistence.json. That method uses if protocol := self._registry.get(...), so it expects None when the persisted protocol has no valid implementation. But Registry.get() in proton/vpn/core/registry.py raises RuntimeError instead. The result: if the persisted protocol is no longer registered, or is registered but its validate() now returns false (for example a leftover protun-smart connection in a snap/flatpak build or on an older NetworkManager), the whole connector crashes during initialisation instead of starting as Disconnected.

    Reproduce

    1. Leave connection_persistence.json pointing at a protocol such as protun-smart.
    2. Start the app in an environment where that protocol is not registered, or where its validate() returns false.
    3. Startup fails with RuntimeError: No valid implementation found for protun-smart instead of reaching the Disconnected state.

    Fix

    #43 catches that RuntimeError in _get_current_connection(), logs a warning naming the protocol, and returns None, so startup continues as Disconnected. This matches how a missing or corrupt persistence file is already handled, and the file is left on disk, as on the corrupt-file path. Registry.get() itself is unchanged, because connect() and the protun default selection rely on it raising.

    Checks

    New regression test tests/python/core/test_vpnconnector_persistence.py, with stub Registry classes (no real VPN is opened), covering five paths:

    • persisted protocol not registered
    • persisted protocol registered but validate() returns false
    • persistence file missing
    • persistence file corrupt
    • valid persisted wireguard connection (still restored, no warning)

    Results on Linux, Python 3.12.3, pytest 9.2.0:

    • on current stable: 2 failed, 3 passed; both failures are RuntimeError: No valid implementation found for protun-smart
    • with the fix: 5 passed
    • full suite: 637 passed, 1 failed. The one failure is test_modules_exist (No module named proton.vpn.platform.protun), because the protun module can't be built outside Proton's tree; it fails on stable too and is unrelated to this change
    • flake8 on the changed files is clean, and pylint shows no new messages compared with stable

    Full commands and output are in the PR description. AI assistance was used to write the change and run the tests; this is disclosed in the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions