Which libgdx backends honor HttpRequest timeout and cancellation?
0 reputation · 10 Mar 2020, 14:21 UTC
0 reputation · 10 Mar 2020, 14:21 UTC
I am evaluating libgdx’s HTTP abstraction for a cross-platform project targeting Desktop, Android, and HTML5. The goal is to enforce a request deadline and abort in-flight requests when the user navigates away, without leaving sockets or XHR handles pending.
libgdx exposes HttpRequest.setTimeout and HttpRequest.setCancel, but whether those calls stop the underlying network operation appears to depend on the backend. Desktop may interrupt only after blocked I/O completes, Android may wait for buffer drains, and HTML5 XHR abort may not invoke the response listener. Documentation does not state whether timeout and cancellation propagate consistently across all supported backends.
Because no built-in callback confirms that the network layer has fully stopped, I need to know which platforms require custom tracking or platform-specific code.
setTimeout reliably trigger HttpResponseListener on Desktop, Android, and HTML5?setCancel(true), which backends guarantee that the underlying connection is closed?35275 reputation · 10 Mar 2020, 22:37 UTC
All three supported libgdx backends – Desktop (LWJGL), Android, and HTML5 (GWT) – use the HttpRequest.setTimeout value to abort a request after the specified interval. In practice:
java.net.HttpURLConnection as both connection and read timeouts. When the interval expires, the connection throws a SocketTimeoutException and the HttpResponseListener receives the callback with statusCode == -1.HttpURLConnection (or the legacy Apache client in very old libgdx releases). An expired timeout triggers the same exception and listener callback.RequestBuilder.setTimeoutMillis. When the timer fires, the browser aborts the XHR and the listener’s onHttpResponse is not invoked – the request simply terminates with status 0.Calling HttpRequest.setCancel(true) initiates a best‑effort abort:
disconnect() (or abort() on the old Apache client) is called. The underlying socket is closed only after the current blocked I/O operation finishes. The listener will still be notified, typically with a timeout‑style exception.RequestBuilder.cancel() aborts the XHR immediately. Because the browser does not fire the listener for an aborted request, you will not see a callback unless you implement your own flag.For most use‑cases the built‑in timeout and cancellation work fine. However, if you need absolute certainty that the network channel is gone when the user navigates away, add a small platform‑specific guard:
HttpRequest and, after calling setCancel(true), poll the connection’s state or use a ScheduledTask to double‑check that no ESTABLISHED sockets remain.onHttpResponse if the callback arrives. This way you can be certain the request has truly finished.HttpRequest request = new HttpRequest(HttpMethod.GET).setUrl("https://example.com/slow");
request.setTimeout(50); // 50 ms
request.setCancel(true); // cancel immediately
request.setListener(new HttpResponseListener() {
public void onHttpResponse(HttpResponse response) {
System.out.println("status: " + response.getStatusCode());
}
public void onHttpRequestFailed(Throwable t) {
System.out.println("failed: " + t.getMessage());
}
});
Gdx.net.sendHttpRequest(request);
Run the snippet on each platform and observe:
SocketTimeoutException or a status of –1.ScheduledTask that sets a flag after 50 ms.To give a more precise recommendation, could you tell me which libgdx version you’re targeting? Older Android releases used the legacy Apache client, which behaves slightly differently for cancellation.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.