TimeoutException in Future.timeout and resource persistence
0 reputation · 24 Oct 2020, 21:52 UTC
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.
Future.timeout() provide any mechanism to signal the underlying task to stop, or is the developer solely responsible for implementing a cancellation pattern?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?34775 reputation · 24 Oct 2020, 22:06 UTC
1. Does Future.timeout() signal the underlying task to stop? No. Future.timeout() only completes the returned future with a TimeoutException. The original asynchronous operation continues executing unchanged. Dart’s core Future API has no built‑in cancellation signal; the developer must implement an explicit cancellation pattern.
2. In package:http, does .timeout() close the socket? No. Wrapping an http.Client request in .timeout() does not terminate the TCP connection. The socket stays open until the server responds or the operating system’s TCP keep‑alive / retransmission timeouts fire.
Future.timeout is a combinator that races the original future against a timer. When the timer wins, it completes the new future with an exception but leaves the original future unattached.http package’s Request and Response objects do not expose a cancellation hook. Closing the http.Client (via client.close()) is the only way to forcibly abort in‑flight connections, but doing so affects every request sharing that client.CancelableOperation from package:async for computations or custom streams. Call cancel() after your timeout to stop the work and receive a CancelledError.http.Client per logical scope (e.g., per screen or repository). When a timeout occurs, call client.close() to abort all its pending sockets.CancelableOperation that calls request.abort() (available on http.Request via IOClient) instead of closing the whole client.HttpClient connection timeout via IOClient(HttpClient()..connectionTimeout = Duration(seconds: 5)) so the OS drops idle sockets even if application‑level timeout fires.Are you using a single shared http.Client instance across the app, or creating short‑lived clients per feature? This determines whether client.close() is safe or whether you need per‑request abort logic.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.