1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-10 17:49:38 +02:00

SMACK-278: Call socket.close() in XMPPConnection.shutdown() before

closing the reader and writer streams. This basically prevents the
deadlock reported in SMACK-278.

Additionally, we need to tell the PacketReader and PacketWriter that
the socket got closed and that subsequent caused Exceptions can be
ignored. This is done via the socketClosed boolean in XMPP Connection,
that is queried by PacketReader and PacketWriter in case an Exception
is encountered.

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@13390 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Florian Schmaus 2013-01-06 14:02:44 +00:00 committed by flow
parent 3c2eaba840
commit e1bb5a6123
3 changed files with 57 additions and 10 deletions

View file

@ -60,6 +60,10 @@ public class XMPPConnection extends Connection {
String connectionID = null;
private String user = null;
private boolean connected = false;
// socketClosed is used concurrent
// by XMPPConnection, PacketReader, PacketWriter
private volatile boolean socketClosed = false;
/**
* Flag that indicates if the user is currently authenticated with the server.
*/
@ -358,6 +362,10 @@ public class XMPPConnection extends Connection {
return isUsingTLS();
}
public boolean isSocketClosed() {
return socketClosed;
}
public boolean isAuthenticated() {
return authenticated;
}
@ -377,14 +385,20 @@ public class XMPPConnection extends Connection {
*/
protected void shutdown(Presence unavailablePresence) {
// Set presence to offline.
packetWriter.sendPacket(unavailablePresence);
if (packetWriter != null) {
packetWriter.sendPacket(unavailablePresence);
}
this.setWasAuthenticated(authenticated);
authenticated = false;
connected = false;
packetReader.shutdown();
packetWriter.shutdown();
if (packetReader != null) {
packetReader.shutdown();
}
if (packetWriter != null) {
packetWriter.shutdown();
}
// Wait 150 ms for processes to clean-up, then shutdown.
try {
Thread.sleep(150);
@ -393,9 +407,25 @@ public class XMPPConnection extends Connection {
// Ignore.
}
// Set socketClosed to true. This will cause the PacketReader
// and PacketWriter to ingore any Exceptions that are thrown
// because of a read/write from/to a closed stream.
// It is *important* that this is done before socket.close()!
socketClosed = true;
try {
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
// In most cases the close() should be successful, so set
// connected to false here.
connected = false;
// Close down the readers and writers.
if (reader != null) {
try {
// Should already be closed by the previous
// socket.close(). But just in case do it explicitly.
reader.close();
}
catch (Throwable ignore) { /* ignore */ }
@ -403,13 +433,17 @@ public class XMPPConnection extends Connection {
}
if (writer != null) {
try {
// Should already be closed by the previous
// socket.close(). But just in case do it explicitly.
writer.close();
}
catch (Throwable ignore) { /* ignore */ }
writer = null;
}
// Make sure that the socket is really closed
try {
// Does nothing if the socket is already closed
socket.close();
}
catch (Exception e) {
@ -524,6 +558,7 @@ public class XMPPConnection extends Connection {
throw new XMPPException(errorMessage, new XMPPError(
XMPPError.Condition.remote_server_error, errorMessage), ioe);
}
socketClosed = false;
initConnection();
}