Hang on socket close after connection abort
Hi all,
i have a rather nasty problem here and the situation is pretty critical so please help wit anything that comes to your mind.
There is a thread with a server socket which accepts a connection and spawns another thread with the created client socket. The connection works for some time without problems. Then a IOException is thrown when writing to the client socket, either "Connection reset" or "Connection timed out". To handle the broken connection, the client socket should then be closed by the thread but hangs in the close method forever.
So the question is how can a "Connection reset" or "Connection timed out" be reproduced when sending data? If this is possible maybe the hang on close can be reproduced.
[747 byte] By [
sunnypetea] at [2007-11-27 9:02:45]

> Connection timed out is a client-side condition.
>
> You can provoke a connection reset by closing the
> side that is reading. The sender will get a
> connection reset.
> I've never seen a thread block closing a socket. Is
> it the same thread doing the close that got the
> IOException?
Yes, it is the same. So you dont think this is possible? The logfiles only tell me that the Thread enters the method which closes the socket, but never leaves it, so i guessed that it hangs in the close().
thanks,
sunnypete
Closing a socket is asynchronous. The only ways it can block are:
(a) you are closing the output stream (which you should) and it hasn't been flushed, and the receiver has stopped reading, and his socket receive buffer is full, and your socket send buffer is full, so the implicit flush() will block until the receiver reads enough to create enough space in all these buffers for the flush() to succeed.
(b) there is a lot of pending data in your socket send buffer and you have set a large positive linger timeout, which you shouldn't do
(c) a synchronization block due to multiple threads.
I suspect that in your case the close() is throwing an exception and bypassing your method-leaving trace. Put it in a finally() block.
ejpa at 2007-7-12 21:33:48 >

> Closing a socket is asynchronous. The only ways it
> can block are:
>
> (a) you are closing the output stream (which you
> should) and it hasn't been flushed, and the receiver
> has stopped reading, and his socket receive buffer is
> full, and your socket send buffer is full, so the
> implicit flush() will block until the receiver reads
> enough to create enough space in all these buffers
> for the flush() to succeed.
Yes, thats it, thank you. One thread hangs in the write() because the TCP buffers are full and the other threads hang in the close() of the same socket. Basically i can never rely on the receiver to be alive and read everything i send to him, so how can a write timeout be implemented?
How can i get the thread out of write()?
br,
Pete
There is no write timeout for a socket, and unfortuntately Sun defined all the java.io Streams write methods to loop until everything is written and return void, rather than return the actual write-count like the underlying OS APIs always do, and as the NIO Channel write operations do.
If you really have this problem you really have to use NIO SocketChannels.
ejpa at 2007-7-12 21:33:48 >
