2

It was silly of me not to dump the stuck trace while catching the IOException from socket.accept() and shutting down the thread doing the accept... Having fixed this, I still want to understand how to deal with the situation when this call barfs.

My app is a classic socket server accepting hundreds of clients, sometimes thousands. Accepting thread is always up and blocked in accept() call. Once accepted, the separate thread gets launched to do the stuff and so on. Nothing special.

The question is, what should be done when accept() fails? Should this be considered as a permanent failure immediately? Should I retry to get into accept() for some time and try to get through? What's the best practice? And what normally the reasons for the IOException to be thrown?

4
  • Not a real question until you give us the error message/exception you got. 'socket closed', 'out of buffer space', and 'out of file descriptors' come to mind as candidates, with obvious explanations in each case: nothing else. Commented Jul 8, 2012 at 10:07
  • Should I have the stack trace there wouldn't be a question in the first place. That's the reality. Commented Jul 8, 2012 at 10:32
  • Also, I think there will be a continuation to this question - I suspect that accept fails because linux box runs out of file handles. Which will raise another question - should I shutdown the server altogether or keep trying hoping that OS will find me some handles to accept the connections... Commented Jul 8, 2012 at 10:34
  • The reality is that until you have a stack trace to post here there is no real question. You can have all the suspicions you like: they are no substitute for hard data. Commented Jul 8, 2012 at 12:33

2 Answers 2

1
  1. Its the clients responsibility to retry on connection failure. The server should just log the exception and continue back doing "accept". Servers in general, never initiate connections to client.
  2. There are too many reasons for IOException to be thrown, from firewall issues to file-handle-exhaustion issues. The message of the IOException should reveal the cause.
Sign up to request clarification or add additional context in comments.

1 Comment

yes, the clients keep trying to get through, this is not the issue. The question is why would accept fail and who should address the issue - the software? or the infrastructure where server runs. If I were to have the stack trace I would know :) I will know when this happens again. I just wanted to know in general what should be done if working 'acceptor' all of a sudden fails - keep trying to acccept? or just declare the fatal failure and call user attention?
1

The only reasons I can think of for an IOException being thrown in accept() call would be some issue with the port being in use or the host's networking being misconfigured.

You could potentially wait a bit and retry on the assumption that the system's administrator will notice the problem and fix it. But if this happens during startup, it would be a good idea for the application to bail out with a "fatal" error message.

4 Comments

What happens is that server socket runs for few days accepting connection without a problem and then all of a sudden it fails. But you are right, when this happens on the first call to 'accept' the good idea would be impose a fatal failure. Thanks for the tip!
@Dima - what does the exception message say when the accept fails?
as I mentioned, the exception is not logged (which is stupid by itself), the system is in production and I don't have and access to it right now. So I wanted to get an idea in what circumstances it may fail. And even more interesting - how can I reproduce this in development...
@Dima - if you have no logging in production, you've got little hope of reproducing the problem in development. And even if you do (seem to) reproduce it, you can't be sure that you actually have done ... without log messages to compare.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.