我使用HttpClient 4.1.2运行Java6。我一直在使用这样的代码来重新加载番石榴缓存。它工作了近一年,有一天,我的缓存停止可靠地重新加载。
现在,我已经将问题缩小到了EntityUtils.toString()**,期间的死锁,但我不知道为什么会发生这种情况,也不知道如何避免。
HttpEntity entity = null;
try {
entity = PooledHttpClient.getHttpEntity(new HttpGet(location));
// PROBLEM HERE: Deadlock occurs rarely in EntityUtils.toString()
final String resource = EntityUtils.toString(entity);
return resource;
} catch (Exception e) {
// Something went wrong, log the error or retry
} finally {
EntityUtils.consume(entity);
}我已经添加了调试,并看到缓存在此方法卡住一次后停止重新加载。下面是线程转储的堆栈跟踪:
"pool-6-thread-172" prio=3 tid=0x0000000017cc4800 nid=0x862 runnable [0xfffffd7fdf1f0000]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at org.apache.http.impl.io.AbstractSessionInputBuffer.read(AbstractSessionInputBuffer.java:187)
at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:176)
at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:138)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
- locked <0x0000000735ec3cc0> (a java.io.InputStreamReader)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at java.io.Reader.read(Reader.java:123)
at org.apache.http.util.EntityUtils.toString(EntityUtils.java:199)
at org.apache.http.util.EntityUtils.toString(EntityUtils.java:221)
at com.something.Resource.getResource(Resource.java:216)
...有什么想法吗?
发布于 2014-12-11 04:17:32
我终于找到了问题所在,关键是堆栈跟踪。回想起来,这是很明显的,但当时我不明白。代码基本上停留在InputStreamReader.read()上,原因是没有套接字超时集(至少对我来说是这样)。
我有过这样的事情:HttpConnectionParams.setConnectionTimeout(params, 10000);
然而,我错过了套接字的关键第二次超时:HttpConnectionParams.setSoTimeout(params, 30000);
如果没有第二次超时,如果连接是建立的,但在那之后有一个问题,我将无限期地等待。我在这里非常困惑,因为这些代码在生产中运行了一年多,没有一个问题,然后有一天,我开始遇到每小时或每天的问题(从Heroku获取文件、一些内容)。仍然不知道为什么,因为我添加了套接字超时,它是一帆风顺的!
希望这能帮上忙!
https://stackoverflow.com/questions/25425551
复制相似问题