okhttp: IllegalStateException when reading data from a connection closed by another thread

The log shows my REST reader getting an IllegalStateException when trying to read from an HTTP connection that has been closed (not by me). I think it should throw an IOException instead. This is with the okhttp 2.0 master branch.

java.lang.IllegalStateException: closed
       at okio.RealBufferedSink.flush(SourceFile:152)
       at com.squareup.okhttp.internal.http.HttpEngine.readResponse(SourceFile:570)
       at com.squareup.okhttp.internal.http.HttpURLConnectionImpl.execute(SourceFile:384)
       at com.squareup.okhttp.internal.http.HttpURLConnectionImpl.getResponse(SourceFile:331)
       at com.squareup.okhttp.internal.http.HttpURLConnectionImpl.getHeaderFields(SourceFile:178)
       at com.squareup.okhttp.internal.http.DelegatingHttpsURLConnection.getHeaderFields(SourceFile:178)
       at com.squareup.okhttp.internal.http.HttpsURLConnectionImpl.getHeaderFields(SourceFile:1)
       at com.levelup.touiteur.DBCookieMaster.setCookieResponse(SourceFile:460)
       at com.levelup.http.AbstractHttpRequest.setResponse(SourceFile:159)
       at com.levelup.http.twitter.HttpTwitterPost.setResponse(SourceFile:66)
       at com.levelup.http.HttpClient.getQueryResponse(SourceFile:165)
       at com.levelup.http.HttpClient.getInputStream(SourceFile:187)
       at com.levelup.http.HttpClient.parseRequest(SourceFile:274)
       at com.levelup.http.HttpClient.getStringResponse(SourceFile:312)
       at com.levelup.http.twitter.TwitterOAuth.getRequestTokener(SourceFile:165)
       at com.levelup.http.twitter.TwitterOAuth.queryJSONObject(SourceFile:72)
       at com.levelup.http.twitter.TwitterOAuth.queryJSONObjectOnly(SourceFile:68)
       at com.plume.twitter.TwitterClient.queryJSONObjectOnly(SourceFile:641)
       at com.plume.twitter.TwitterClient$ProfileUpdateBuilder.updateProfile(SourceFile:1070)
       at com.levelup.touiteur.ProfileTwitter$2$1.execute(SourceFile:554)
       at com.levelup.touiteur.ActivityTouiteur$BusyWorkingThread.run(SourceFile:595)```

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 18 (11 by maintainers)

Commits related to this issue

Most upvoted comments

Hello, OkHttp 2.2.0

When i use

System.out.println(response.body().string());
String ceyson = response.body().string();

there is exception like this (for String ceyson = response.body().string(); )

W/System.err﹕ java.lang.IllegalStateException: closed
W/System.err﹕ at com.squareup.okhttp.internal.http.HttpConnection$FixedLengthSource.read(HttpConnection.java:405)
W/System.err﹕ at okio.Buffer.writeAll(Buffer.java:705)
W/System.err﹕ at okio.RealBufferedSource.readByteArray(RealBufferedSource.java:92)
W/System.err﹕ at com.squareup.okhttp.ResponseBody.bytes(ResponseBody.java:57)
W/System.err﹕ at com.squareup.okhttp.ResponseBody.string(ResponseBody.java:83)

But when i use only String ceyson = response.body().string(); it is ok