12 Replies Latest reply: Jun 27, 2013 5:35 PM by Hardev RSS

    java.net.SocketException: socket closed

    846089
      Hi,

      I'm using JDK 1.6.0_12 on windows platform, and sometime I get exception in 3rd party library (JAIN-SIP)

      java.net.SocketException: socket closed
           at java.net.PlainDatagramSocketImpl.receive0(Native Method)
           at java.net.PlainDatagramSocketImpl.receive(Unknown Source)
           at java.net.DatagramSocket.receive(Unknown Source)
           at gov.nist.javax.sip.stack.UDPMessageProcessor.run(Unknown Source)
           at java.lang.Thread.run(Unknown Source)

      but we do not know who and when close it...

      Is there are way to log native code to file ? and understand when and why it become closed...
      IMHO this looks like already fixed bug http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4361783

      Related bug on mobicents tracking system is http://code.google.com/p/mobicents/issues/detail?id=1760
        • 1. Re: java.net.SocketException: socket closed
          802316
          This bug was fixed 10 years ago, so I doubt its the same bug. If you think its a bug, can you try Java 6 udpate 24 instead of update 12?
          • 2. Re: java.net.SocketException: socket closed
            846089
            OK, I'll try, but it's not easy to reproduce and verify.
            • 3. Re: java.net.SocketException: socket closed
              Hardev
              I have also seen the same issue, in my case I have Java 5 running on windows 2008 server. It happens occasionally (twice a month) and I was wondering if a solution was found for this.

              Thanks.
              • 4. Re: java.net.SocketException: socket closed
                EJP
                Running the SIP stack? or something else.

                If the SIP stack, it's a bug in the SIP stack.

                If something else, it's a bug in your code. The error message you have tried to use a Socket after you have closed it. NB not after the peer has closed it, that produces EOS indications or connection resets.
                • 5. Re: java.net.SocketException: socket closed
                  gimbal2
                  EJP wrote:
                  Running the SIP stack? or something else.

                  If the SIP stack, it's a bug in the SIP stack.

                  If something else, it's a bug in your code. The error message you have tried to use a Socket after you have closed it. NB not after the peer has closed it, that produces EOS indications or connection resets.
                  He, in true Friday "I don't want to work" fashion that gives me an idea for a wiki website: javaexceptions.com. Look up under which circumstances you could get SPECIFIC exceptions and exception messages :) Can save you from repeating yourself at least a few hundred times a year anyway, I don't know how often I've seen you post exactly this...

                  Pardon my intrusion, carry on.
                  • 6. Re: java.net.SocketException: socket closed
                    EJP
                    It should really be a separate exception with its own Javadoc. They got it right in NIO with ClosedChannelException.
                    • 7. Re: java.net.SocketException: socket closed
                      gimbal2
                      EJP wrote:
                      It should really be a separate exception with its own Javadoc. They got it right in NIO with ClosedChannelException.
                      Hm, that is a good point. Using specific exceptions for the sake of (code) documentation. Never thought of it that way, until now the only real argument I had that held any water outside of a text book was to be able to catch and deal with specific exceptions, which is useful less often than you'd think.
                      • 8. Re: java.net.SocketException: socket closed
                        Hardev
                        I have a Datagram socket which blocks to receive UDP data in one thread and a pool of other threads which can send UDP data using this socket. I have made sure the send is synchronized. I have no explicit calls to close the socket. My socket send is in a try catch block to capture exceptions during send, my receive is also in a try catch block and it is in this block that I get the socket closed exception. I don't see any exceptions during send. I modified my code so that after the socket closed exception I would exit the thread that was running the receive socket and re-initialise the socket to listen on the same port. I tried to emulate the problem by explicitly closing the socket from a different thread, I got the socket closed exception in the receive thread and I was able to re-intialise the socket and the process kept on running as normal. After I had deployed this I got a "real" socket closed exception and the code to re-initialise failed with a bind exception, this tells me that the socket hasn't been released when this exception occurs hence the bind failure. This is where I am at and looking for answers from others who may have experienced the same issue.
                        • 9. Re: java.net.SocketException: socket closed
                          EJP
                          Hmm, bizarre. Very good debugging, my compliments. Can't offer any suggestion except to double check all that synchronization. Can you post some code?
                          • 10. Re: java.net.SocketException: socket closed
                            Hardev
                            Just an update to what I feel is happening. Problem with this issue is that it happens so rarely, anyway I had added more debug in my code to look for the IOException and check the status of the socket. When it finally happend I noted that the socket in fact was not closed. This leads me to believe that we are sending a UDP packet to a host on a port that is not been listened to. In this case the host may be sending back an ICMP packet which gets interpreted as an IOException by the Java receiver. To test my theory I sent a UDP packet to the host/port that caused it BUT I didn't get back an ICMP packet, that could be because the router is not passing it on due to NAT, so I am still not 100% sure. I have now got wireshark running in the hope it happens again and maybe get to the bottom of this.
                            • 11. Re: java.net.SocketException: socket closed
                              EJP
                              You will only get an ICMP error on a DatagramSocket if it is 'connected', i.e. if you have called DatagramSocket.connect().
                              • 12. Re: java.net.SocketException: socket closed
                                Hardev

                                I think I have solved this, I set up my server to start wireshark for 5 mins if I ever got this exception, it occurred a few days back and I was able to correlate my logs with the wireshark capture. My logs showed this :

                                 

                                ERROR 03:16:19.184 - SipServer; BidUdpTransport:IOException  [ java.net.SocketEx

                                ception ] socket closedjava.net.SocketException: socket closed  -----------------------------------------------> This caused the wireshark to trigger

                                ERROR 03:16:19.745 - SipServer; BidUdpTransport:IOException  [ java.net.SocketEx

                                ception ] socket closedjava.net.SocketException: socket closed -------------------------------------------------> Packet 1 below

                                ERROR 03:16:20.806 - SipServer; BidUdpTransport:IOException  [ java.net.SocketEx

                                ception ] socket closedjava.net.SocketException: socket closed -------------------------------------------------> Packet 4 below

                                ERROR 03:17:08.308 - SipServer; BidUdpTransport:IOException  [ java.net.SocketEx

                                ception ] socket closedjava.net.SocketException: socket closed -------------------------------------------------> Packet 812 below

                                ERROR 03:17:08.823 - SipServer; BidUdpTransport:IOException  [ java.net.SocketEx

                                ception ] socket closedjava.net.SocketException: socket closed -------------------------------------------------> Packet 815 below

                                ERROR 03:17:09.931 - SipServer; BidUdpTransport:IOException  [ java.net.SocketEx

                                ception ] socket closedjava.net.SocketException: socket closed -------------------------------------------------> Packet 825 below

                                 

                                and corresponding entries in wireshark:

                                 

                                No.Time                Source         Destination    Protocol Length Info
                                12013-06-23 03:16:19.735195  192.168.1.2    63.218.164.62ICMP        70Time-to-live exceeded (Time to live exceeded in transit)

                                 

                                 

                                No.Time                Source         Destination    Protocol Length Info
                                42013-06-23 03:16:20.796186  192.168.1.2    63.218.164.62ICMP        70Time-to-live exceeded (Time to live exceeded in transit)

                                 

                                 

                                No.Time                Source         Destination    Protocol LengthInfo
                                8122013-06-23 03:17:08.304685  192.168.1.2    67.17.199.177ICMP        70Time-to-live exceeded (Time to live exceeded in transit)

                                 

                                 

                                No.Time                Source         Destination    Protocol LengthInfo
                                8152013-06-23 03:17:08.819074  192.168.1.2    67.17.199.177ICMP        70Time-to-live exceeded (Time to live exceeded in transit)

                                 

                                 

                                No.Time                Source         Destination    Protocol LengthInfo
                                8252013-06-23 03:17:09.926770  192.168.1.2    67.17.199.177ICMP        70Time-to-live exceeded (Time to live exceeded in transit)

                                 

                                 

                                 

                                So it is related to receiving an ICMP packet which has been received in response to a UDP packet that was sent out. Also note that the UDP socket was not "connected", I haven't called the connect() method on the UDP socket.

                                To work around this I check the state of the socket when I get the exception and if it is not closed I carry on as normal. So far I have seen these exceptions and choosing to ignore has shown no undesired side effects.