Skip navigation
5078 Views 18 Replies Latest reply: Nov 7, 2011 1:30 AM by Flow RSS
sgargan Bronze 1 posts since
Sep 6, 2008
Currently Being Moderated

Sep 6, 2008 7:46 PM

Dedlock during Smack disconnect

 

 

Hi,

I've uncovered subtle and intermittent deadlock scenario that can occur (in the 3.0.4 release) when closing a XMPP connection. The scenario occurs as follows, Calling disconnect attempts to close the connections reader, (line 528) which attempts to acquire the readers lock. This reader is also held by the MXParser, which at the time of the close is blocked reading from the socket. This blocked read is holding the reader lock and thus deadlocks the close call. There is an attempt to shutdown the PacketReader (line 570), but as shutdown waits for the current operations to finish, the blocked read is not interrupted and the call to close will hang.

 

It could easily be mitigated by calling shutdownNow on the packet reader, (interrupting the read and allowing the reader to close) or if this is not desirable, calling shutdown, and starting a timer to force the shutdown (via shutdown now) if the reader is still open after a few seconds.

 

Best,

 

Stephen

  • Zhenlei Cai Bronze 6 posts since
    Nov 30, 2008
    Currently Being Moderated
    Jan 16, 2009 10:31 PM (in response to sgargan)
    Re: Dedlock during Smack disconnect

    I found the exactly same problem happening. This is not fixed in 3.1.0 beta. It looks to me in the disconnect() method one should first stop/destroy the packet reader thread (the one runs parsePackets()) to release its lock on the java.io.InputStreamReader. I hope this get fixed in 3.1.0 RC1.  Here is the thread dump: 

     

     

     

     

         [java] Full thread dump Java HotSpot(TM) 64-Bit Server VM (1.5.0_07-b03 mixed mode):

         [java]

         [java] "Smack Packet Reader (12215)" daemon prio=1 tid=0x00002aaaf89a2f30 nid=0x3be9 runnable [0x0000000048ae0000..0x0000000048ae0d10]

         [java]     at java.net.SocketInputStream.socketRead0(Native Method)

         [java]     at java.net.SocketInputStream.read(SocketInputStream.java:129)

         [java]     at sun.nio.cs.StreamDecoder$CharsetSD.readBytes(StreamDecoder.java:411)

         [java]     at sun.nio.cs.StreamDecoder$CharsetSD.implRead(StreamDecoder.java:453)

         [java]     at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:183)

         [java]     - locked <0x00002aaae184d920> (a java.io.InputStreamReader)

         [java]     at java.io.InputStreamReader.read(InputStreamReader.java:167)

         [java]     at java.io.BufferedReader.fill(BufferedReader.java:136)

         [java]     at java.io.BufferedReader.read1(BufferedReader.java:187)

         [java]     at java.io.BufferedReader.read(BufferedReader.java:261)

         [java]     - locked <0x00002aaae184d920> (a java.io.InputStreamReader)

         [java]     at org.xmlpull.mxp1.MXParser.fillBuf(MXParser.java:2972)

         [java]     at org.xmlpull.mxp1.MXParser.more(MXParser.java:3026)

         [java]     at org.xmlpull.mxp1.MXParser.nextImpl(MXParser.java:1144)

         [java]     at org.xmlpull.mxp1.MXParser.next(MXParser.java:1093)

         [java]     at org.jivesoftware.smack.PacketReader.parsePackets(PacketReader.java:368)

         [java]     at org.jivesoftware.smack.PacketReader.access$000(PacketReader.java:44)

         [java]     at org.jivesoftware.smack.PacketReader$1.run(PacketReader.java:76)

         [java]



     

     

         [java] "main" prio=1 tid=0x000000004011bb30 nid=0x72fa waiting for monitor entry [0x00007fff3fa24000..0x00007fff3fa24b10]

         [java]     at java.io.BufferedReader.close(BufferedReader.java:500)

         [java]     - waiting to lock <0x00002aaae184d920> (a java.io.InputStreamReader)

         [java]     at org.jivesoftware.smack.XMPPConnection.shutdown(XMPPConnection.java:583)

         [java]     at org.jivesoftware.smack.XMPPConnection.disconnect(XMPPConnection.java:643)

         [java]     at org.jivesoftware.smack.XMPPConnection.disconnect(XMPPConnection.java:618)

         [java]     at com.gaocan.im.server.ImConnectionPool.closeConnection(ImConnectionPool.java:114 )



    • Zhenlei Cai Bronze 6 posts since
      Nov 30, 2008
      Currently Being Moderated
      Jan 17, 2009 12:00 AM (in response to Zhenlei Cai)
      Re: Dedlock during Smack disconnect

      so I am trying this hack, so far it seems to work:

       

      add "

       

       

              try {

                  socket.shutdownInput();

              } catch (IOException e) {

                  ;

              }

      "



       

      in front of  the

       

              packetReader.shutdown(); call in

       

      XMPPConnection.shutdown() method.  With this hack then reader is forcefully closed, you will see these harmless messages:

       

       

       

           [java] java.io.EOFException: no more data available - expected end tag </stream:stream> to close start tag <stream:stream> from line 1, parser stopped on END_TAG seen ...onflict xmlns=\'urn:ietf:params:xml:ns:xmpp-stanzas\'/></error></iq>... @1:690

           [java]     at org.xmlpull.mxp1.MXParser.fillBuf(MXParser.java:3015)

           [java]     at org.xmlpull.mxp1.MXParser.more(MXParser.java:3026)

           [java]     at org.xmlpull.mxp1.MXParser.nextImpl(MXParser.java:1144)

           [java]     at org.xmlpull.mxp1.MXParser.next(MXParser.java:1093)

           [java]     at org.jivesoftware.smack.PacketReader.parsePackets(PacketReader.java:368)

           [java]     at org.jivesoftware.smack.PacketReader.access$000(PacketReader.java:44)

           [java]     at org.jivesoftware.smack.PacketReader$1.run(PacketReader.java:76)



      • TimDev Bronze 9 posts since
        May 11, 2009
        Currently Being Moderated
        Jun 29, 2009 7:43 AM (in response to Zhenlei Cai)
        Re: Dedlock during Smack disconnect

        Is this the best solution anyone has found for this issue?

        I pulled down the latest source from the subversion repository, but the problem still seems to appear with that code.

        I tried making the modifications to the source described above, and found I needed to force the socket to close before calling  packetReader.shutdown() to completely avoid the problem.

         

        try {
                 socket.shutdownInput();
                 socket.close();
              } catch (IOException e) {
                 ;
              }

         

        Thanks for any information.

  • pcmuc Bronze 2 posts since
    Sep 22, 2009
    Currently Being Moderated
    Sep 22, 2009 4:33 AM (in response to sgargan)
    Re: Dedlock during Smack disconnect

    Hi,

     

    I'm having the same problem with disconnect().

    It is not fixed in version 3.1.0 and I'm wondering if anybody filed a bug or contributed the solution for this issue. Otherwise everybody having this problem will have to fix it in their own copy of smack...

     

    Best regards,

    Peter

More Like This

  • Retrieving data ...

Bookmarked By (1)

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points