When socketOutputStream.flush() doesn't really flush..

Oh, I feel like a n00b sometimes. (I sense a few people nodding understandably…)

The following optimisation deathtrap caused my TCP streams to ditch all outbound-pending-bytes.


      this.socket.setKeepAlive(false);
      this.socket.setSoLinger(true, 0);
      this.socket.setReuseAddress(true);
      this.socket.setTcpNoDelay(true);

//

   for(...)
   {
      this.out.write(...);
      this.out.flush();
   }
   this.out.close();

Appearantly flush() flushes the bytes into the OS buffer, instead of flushing the OS buffer itself. Causing setSoLinger(true, 0) to discard any pending bytes and immediately closing the stream. Oh the fun I had this morning…

dont know if it could help but seems to explain issues that happen with TCP/CLOSE/flush/SO_LINGER
http://blog.netherlabs.nl/articles/2009/01/18/the-ultimate-so_linger-page-or-why-is-my-tcp-not-reliable

Sidenote: as mentionned in this article for my own programs I usually use an integer as a packet header with size of the data that follow so the receiver know that the data received are consistent/complete

EDIT : shouldn’t be ?

this.socket.setSoLinger(true, 10000); 

Thanks, interesting read, but unfortunately doesn’t mention flush()

And I can’t use a packet-length protocol in this case, as I’m dealing with the HTTP protocol. You can do the same with Content-Length or Transfer-Encoding: chunked but I’m the server, not the client, which means I have to safely close the connection myself, instead of relying on the client. I have to close the connection after every request, as I’m handling 300+ hits / sec, and that means I’d be quickly running out of file descriptors (or local ports) if I used Keep-Alive.

for some reason, the parameter is taking seconds, so the above would mean a 3 hour linger :wink:

oups but all data will for sure be sent !

… and serversocket.accept() will throw IOException("Too many open files") :persecutioncomplex:

Keeping the connection alive for 10 seconds already swamped my server and eventually crashed every other app too, as they all need to be able to make new file descriptors to function properly.