openvpn_flutter icon indicating copy to clipboard operation
openvpn_flutter copied to clipboard

`connect` method never `return` anything

Open shafiquecbl opened this issue 3 months ago β€’ 0 comments

πŸ› Bug Report: connect method never returns

I encountered an issue with the connect method in openvpn_flutter.

Example usage:

@override
Future<void> connect(VpnServer server, {List<String>? bypassPackages}) async {
  if (server.openvpn == null ||
      server.openvpn?.config == null ||
      server.openvpn?.username == null ||
      server.openvpn?.password == null) {
    throw Exception('OpenVPN configuration is missing');
  }

  await _ensureInitialized();

  return await _openVPN.connect(
    server.openvpn!.config,
    server.name,
    username: server.openvpn!.username,
    password: server.openvpn!.password,
    bypassPackages: bypassPackages,
  );
}

And in a higher-level service:

Future<void> connect(Protocol protocol, VpnServer server) async {
  final vpnProtocol = _protocols[protocol]; // openvpn or wireguard for now (others in future)
  if (vpnProtocol == null) throw Exception('Invalid protocol');

  try {
    await vpnProtocol.connect(server); // connect first
    _startStageStream(protocol);   // then start listening to stage changes
    return await saveSelectedProtocol(protocol); // save selected protocol
  } catch (e) {
    _startStageStream(protocol);
    throw Exception('Failed to connect protocol $protocol');
  }
}

❌ Problem:

  • The connect method never returns, even after the VPN successfully connects.
  • Because of this, I am unable to perform any further actions after connection.
  • If the server has issues and does not respond, the app gets stuck at the "connecting" stage, since the method never completes or throws an error.

βœ… Expected behavior:

  • The connect method should return once the VPN connection attempt finishes (either success or failure).
  • In case of server issues, it should throw an error or return a failure state, so that the app can handle it gracefully.

πŸ” Actual behavior:

  • Method call hangs indefinitely.
  • No way to detect if VPN connected successfully or failed due to server issues.

πŸ’‘ Possible Fix / Suggestions:

  • The connect method could await until a stage update event is received (e.g., connected, disconnected, or error).
  • Consider returning a Future that completes once a definitive state is reached instead of leaving the method hanging.
  • In case of an error or timeout, return an appropriate error so that apps don’t get stuck on "connecting".

πŸ› οΈ Temporary Fix I Used:

Instead of calling:

vpnHelper.startVPN(config, username, password, name, bypassPackages);

I modified it like this:

try {
    vpnHelper.startVPN(config, username, password, name, bypassPackages);
    // Resolve success after starting VPN
    result.success(true); // Indicates VPN start attempt succeeded
} catch (Exception e) {
    result.error("-3", "Failed to start VPN: " + e.getMessage(), "");
}

This way, the method at least returns immediately with success/failure, preventing the app from getting stuck.


πŸš€ Next Steps:

  • Ideally, the method should not just return immediately after starting VPN, but complete its Future based on actual connection state.

  • A better approach would be to complete the Future when:

    • βœ… connected stage is received β†’ resolve success.
    • ❌ error or timeout occurs β†’ return error/failure.
  • This would give developers a reliable way to handle both success and failure cases programmatically.

shafiquecbl avatar Sep 04 '25 06:09 shafiquecbl