What happens to an instance of ServerSocket blocked inside accept(), when I drop all references to i

Posted by Hanno Fietz on Stack Overflow See other posts from Stack Overflow or by Hanno Fietz
Published on 2010-04-01T19:19:47Z Indexed on 2010/04/01 19:23 UTC
Read the original article Hit count: 204

Filed under:
|
|

In a multithreaded Java application, I just tracked down a strange-looking bug, realizing that what seemed to be happening was this:

  • one of my objects was storing a reference to an instance of ServerSocket
  • on startup, one thread would, in its main loop in run(), call accept() on the socket
  • while the socket was still waiting for a connection, another thread would try to restart the component
  • under some conditions, the restart process missed the cleanup sequence before it reached the initialization sequence
  • as a result, the reference to the socket was overwritten with a new instance, which then wasn't able to bind() anymore
  • the socket which was blocking inside the accept() wasn't accessible anymore, leaving a complete shutdown and restart of the application as the only way to get rid of it.

Which leaves me wondering: with no references left to the ServerSocket instance, what would free the socket for a new connection? At what point would the ServerSocket become garbage collected? In general, what are good practices I can follow to avoid this type of bug?

© Stack Overflow or respective owner

Related posts about java

Related posts about multithreading