sdk
sdk copied to clipboard
Unreliable network connection timeout parameters
Although SecureSocket.connect() has set a timeout parameter of 500ms, I just encountered a situation where the timeout exception is not thrown even after exceeding 500ms, and the reason is unknown. Although the connection timeout was caused by a server failure, SecureSocket.connect() should not be interpreted literally as not throwing timeout exceptions.
try{
await SecureSocket.connect(remoteIP, remotePort,
timeout: Duration(milliseconds: 500), onBadCertificate: (X509Certificate){
return true;
});
}catch(e){
// Timeout exceptions that may not be triggered at times
}
So I can only add a Future's own timeout to solve it:
try{
await SecureSocket.connect(remoteIP, remotePort,
timeout: Duration(milliseconds: 500), onBadCertificate: (X509Certificate){
return true;
}).timeout(Duration(milliseconds: 500));
} on TimeoutException catch (e) {
// Actual triggered timeout
}catch(e){
// Timeout exceptions that may not be triggered at times
}
Summary: The SecureSocket.connect() method with a specified timeout parameter does not consistently throw a timeout exception when the connection exceeds the set duration, leading to unreliable network connection timeouts. This issue requires investigation to determine why the timeout parameter is not being enforced as expected.
//cc @brianquinlan
We might not apply the timeout early enough:
static Future<_NativeSocket> connect(dynamic host, int port,
dynamic sourceAddress, int sourcePort, Duration? timeout) {
return startConnect(host, port, sourceAddress, sourcePort)
.then((ConnectionTask<_NativeSocket> task) {
Future<_NativeSocket> socketFuture = task.socket;
if (timeout != null) {
socketFuture = socketFuture.timeout(timeout, onTimeout: () {
task.cancel();
throw createError(
null, "Connection timed out, host: ${host}, port: ${port}");
});
}
return socketFuture;
});
}
close() also encountered the same problem. Suggest conducting a unified investigation to identify potential issues with interfaces with timeouts.