openvpn_flutter
openvpn_flutter copied to clipboard
`connect` method never `return` anything
π 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
connectmethod 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
connectmethod 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
connectmethod could await until a stage update event is received (e.g.,connected,disconnected, orerror). - Consider returning a
Futurethat 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
Futurebased on actual connection state. -
A better approach would be to complete the
Futurewhen:- β
connectedstage is received β resolve success. - β
erroror timeout occurs β return error/failure.
- β
-
This would give developers a reliable way to handle both success and failure cases programmatically.