Pregunta

I'm using the HTTPClient library and I'm getting an odd issue where the Socket.close() call never returns causing a TimeoutException. This happens once in a while and I can reproduce it about 10% of the time.

I'm seeing this issue on Android 4.2, 4.3 and 4.4.

Here is the Android Bug Ticket I created for this issue:
http://code.google.com/p/android/issues/detail?id=66102

Any ideas why Socket.close would ever hang forever? How I could avoid this? Thanks!

02-17 20:48:31.800: E/AndroidRuntime(12871): FATAL EXCEPTION: FinalizerWatchdogDaemon
02-17 20:48:31.800: E/AndroidRuntime(12871): Process: com.vblast.sample, PID: 12871
02-17 20:48:31.800: E/AndroidRuntime(12871): java.util.concurrent.TimeoutException: org.apache.http.impl.conn.PoolingHttpClientConnectionManager.finalize() timed out after 10 seconds
02-17 20:48:31.800: E/AndroidRuntime(12871):    at libcore.io.Posix.close(Native Method)
02-17 20:48:31.800: E/AndroidRuntime(12871):    at libcore.io.BlockGuardOs.close(BlockGuardOs.java:75)
02-17 20:48:31.800: E/AndroidRuntime(12871):    at libcore.io.IoBridge.closeSocket(IoBridge.java:188)
02-17 20:48:31.800: E/AndroidRuntime(12871):    at java.net.PlainSocketImpl.close(PlainSocketImpl.java:162)
02-17 20:48:31.800: E/AndroidRuntime(12871):    at java.net.Socket.close(Socket.java:317)
02-17 20:48:31.800: E/AndroidRuntime(12871):    at org.apache.http.impl.BHttpConnectionBase.close(BHttpConnectionBase.java:346)
02-17 20:48:31.800: E/AndroidRuntime(12871):    at org.apache.http.impl.conn.LoggingManagedHttpClientConnection.close(LoggingManagedHttpClientConnection.java:83)
02-17 20:48:31.800: E/AndroidRuntime(12871):    at org.apache.http.impl.conn.CPoolEntry.closeConnection(CPoolEntry.java:70)
02-17 20:48:31.800: E/AndroidRuntime(12871):    at org.apache.http.impl.conn.CPoolEntry.close(CPoolEntry.java:96)
02-17 20:48:31.800: E/AndroidRuntime(12871):    at org.apache.http.pool.AbstractConnPool.shutdown(AbstractConnPool.java:127)
02-17 20:48:31.800: E/AndroidRuntime(12871):    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.shutdown(PoolingHttpClientConnectionManager.java:347)
02-17 20:48:31.800: E/AndroidRuntime(12871):    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.finalize(PoolingHttpClientConnectionManager.java:168)
02-17 20:48:31.800: E/AndroidRuntime(12871):    at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:187)
02-17 20:48:31.800: E/AndroidRuntime(12871):    at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:170)
02-17 20:48:31.800: E/AndroidRuntime(12871):    at java.lang.Thread.run(Thread.java:841)
¿Fue útil?

Solución

Turns out I confused using Socket.setSoLinger() as my connection timeout setting causing the long hang thus TimeoutException. :/

This is what Socket.setSoLinger() does:

If the socket is in connection-mode, and the SO_LINGER option is set for the socket with non-zero linger time, and the socket has untransmitted data, then close() shall block for up to the current linger interval until all data is transmitted.

http://pubs.opengroup.org/onlinepubs/009695399/functions/close.html

Otros consejos

The real issue here is that you have leaked the connection, requiring it to close on finalization. You need to check your code to ensure the closes are in finally blocks. You may also have to call HttpURLConnection.disconnect() yourself.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top