libgdx / libgdx/libgdx

NetJavaImpl.HttpClientResponse swallowing exceptions thrown by HttpURLConnection

Open
#4,700 12 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug code quality
Dominant language
Java
Stars
25.4k
Forks
6.5k
Avg merge
1d 16h
Merged PRs (30d)
5

Description

Issue details

HttpClientResponse silently ignores all exceptions thrown by the Connection input stream, including exception thrown if the connection was already closed. After the response content is extracted with getResult (or one of its alternatives), the input stream is closed and subsequent calls to getResult will return an empty byte array, empty stream or empty string, without warning the user.

Possible solutions:

  • Cache response result as byte array. Wrap with string or input stream on demand. Make HttpClientResponse thread-safe and reusable.
  • Rethrow silently ignored exceptions. I think making HttpResponse a single-use object is somewhat OK, as long as the user is able to track the source of the issue.
  • At the very least - add some warning logs for ignored exceptions.
Reproduction

A Kotlin example reproducing the issue - should be trivial to convert to Java:

fun main(args: Array<String>) {
  val net = NetJavaImpl()
  val request = HttpRequest("GET")
  request.url = "http://google.com"
  net.sendHttpRequest(request, object : HttpResponseListener {
    override fun cancelled() = Unit
    override fun failed(t: Throwable?) {
      t?.printStackTrace()
    }
    override fun handleHttpResponse(httpResponse: HttpResponse) {
      println("Content: ${httpResponse.resultAsString}")
      println("Content: ${httpResponse.resultAsString}")
    }
  })
  Thread.sleep(1000L)
}

"Content: " is printed by the second println execution. No exceptions are thrown.

Version of LibGDX

1.9.6, although I'm pretty sure the issue exists in earlier versions.

Please select the affected platforms
  • Android
  • iOS (robovm)
  • iOS (MOE)
  • HTML/GWT
  • Windows
  • Linux
  • MacOS

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at NetJavaImpl.HttpClientResponse and inspect how getResult and its alternatives read and close the Connection input stream. Reproduce the issue with the provided Kotlin example by reading resultAsString twice. Done means a repeated response read no longer silently returns empty content, and any underlying exception is observable.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.