ReconnectionManager.reconnect() can throw NotConnectedException

If you disable automatic reconnection while Smack is already attempting to reconnect, it can cause the reconnect runnable to hit a NotConnectedException. This happens in the following code:

if (isReconnectionPossible(connection)) {
   try {
  connection.connect();
  } catch (SmackException.AlreadyConnectedException e) {
  LOGGER.log(Level.FINER, "Connection was already connected on reconnection attempt", e);
  }
  }
   // TODO Starting with Smack 4.2, connect() will no
  // longer login automatically. So change this and the
  // previous lines to connection.connect().login() in the
  // 4.2, or any later, branch.
   if (!connection.isAuthenticated()) {
  connection.login();
  }
   // Successfully reconnected.
   attempts = 0;

isReconnectionPossible(connection) returns false because automatic reconnection is disabled, so connect() is not called. But then connection.login() is called on the unconnected connection, causing the exception.

I think that if automatic reconnection is disabled Smack shouldn’t try to connect or log in during the reconnect code.

The full stack trace is:

09-07 11:29:44.914 29325-29449/<app_name> D/SMACK: Reconnection failed due to an exception (XMPPTCPConnection[<user@domain>] (0))
org.jivesoftware.smack.SmackException$NotConnectedException: Client is not, or no longer, connected. Did you call connect() before login()?
at org.jivesoftware.smack.AbstractXMPPConnection.throwNotConnectedExceptionIfAppro priate(AbstractXMPPConnection.java:643)
at org.jivesoftware.smack.AbstractXMPPConnection.login(AbstractXMPPConnection.java :486)
at org.jivesoftware.smack.AbstractXMPPConnection.login(AbstractXMPPConnection.java :448)
at org.jivesoftware.smack.ReconnectionManager$2.run(ReconnectionManager.java:254)
at java.lang.Thread.run(Thread.java:761)

You are right. Thanks for pointing this out. As far as I can tell this is not the only flaw in ReconnectionManager, its code could need a little bit overhaul. Possibly that this would went into the 4.2.2 release.

Created SMACK-778 to track this issue.