TimeoutException in Future.timeout and resource persistence
0 reputation · 24 Oct 2020, 21:52 UTC
In Flutter applications using Dart, Future.timeout() is frequently used to prevent the UI from hanging during long-running asynchronous operations. When the specified duration is exceeded, the method completes the returned future with a TimeoutException.
While the exception allows the application to handle the failure gracefully, the underlying asynchronous operation—such as a network request or a heavy computation—continues to execute in the background. This behavior creates a discrepancy between the application's state (which has moved on from the timed-out request) and the actual resource consumption on the system.
Given that Dart's core Future API does not provide a built-in mechanism to forcibly terminate an in-flight operation once a timeout has fired, there is uncertainty regarding the most efficient way to ensure resources are reclaimed.
- Does
Future.timeout()provide any mechanism to signal the underlying task to stop, or is the developer solely responsible for implementing a cancellation pattern? - In the context of
package:http, does wrapping a request in.timeout()trigger a socket-level closure, or does the connection remain open until the server responds or a lower-level TCP timeout occurs?