Don't teardown things after sockets have been closed

Before, the call to closeAll() in McsService#connect() would trigger a SocketException (Socket closed) in McsOutputStream and/or McsInputStream. This would send a teardown message causing McsService to take the new connection down right away. In unlucky situations, this could cause small connect/teardown loops. This commit hopes to prevent those.

Change-Id: Id347d598e028bdd1ba2622cd6a5c6b07874335d6
This commit is contained in:
Torsten Grote 2021-10-21 15:16:42 -03:00 committed by Marvin W
parent b9b1ef6246
commit f3c20333b0
3 changed files with 17 additions and 8 deletions

View File

@ -20,7 +20,6 @@ import android.os.Handler;
import android.util.Log;
import com.squareup.wire.Message;
import com.squareup.wire.Wire;
import org.microg.gms.gcm.mcs.Close;
import org.microg.gms.gcm.mcs.DataMessageStanza;
@ -57,7 +56,7 @@ public class McsInputStream extends Thread implements Closeable {
private int streamId = 0;
private long lastMsgTime = 0;
private boolean closed = false;
private volatile boolean closed = false;
public McsInputStream(InputStream is, Handler mainHandler) {
this(is, mainHandler, false);
@ -83,7 +82,11 @@ public class McsInputStream extends Thread implements Closeable {
}
}
} catch (IOException e) {
mainHandler.dispatchMessage(mainHandler.obtainMessage(MSG_INPUT_ERROR, e));
if (closed) {
Log.d(TAG, "We were closed already. Ignoring IOException");
} else {
mainHandler.dispatchMessage(mainHandler.obtainMessage(MSG_INPUT_ERROR, e));
}
}
try {
is.close();
@ -120,7 +123,7 @@ public class McsInputStream extends Thread implements Closeable {
Log.d(TAG, "Reading from MCS version: " + version);
initialized = true;
} catch (IOException e) {
Log.w(TAG, e);
Log.w(TAG, "Error reading version", e);
}
}
}

View File

@ -41,10 +41,10 @@ public class McsOutputStream extends Thread implements Handler.Callback, Closeab
private int version = MCS_VERSION_CODE;
private int streamId = 0;
private Handler mainHandler;
private final Handler mainHandler;
private Handler myHandler;
private boolean closed = false;
private volatile boolean closed = false;
public McsOutputStream(OutputStream os, Handler mainHandler) {
this(os, mainHandler, false);
@ -78,7 +78,11 @@ public class McsOutputStream extends Thread implements Handler.Callback, Closeab
writeInternal((Message) msg.obj, msg.arg1);
mainHandler.dispatchMessage(mainHandler.obtainMessage(MSG_OUTPUT_DONE, msg.arg1, msg.arg2, msg.obj));
} catch (IOException e) {
mainHandler.dispatchMessage(mainHandler.obtainMessage(MSG_OUTPUT_ERROR, e));
if (closed) {
Log.d(TAG, "We were closed already. Ignoring IOException");
} else {
mainHandler.dispatchMessage(mainHandler.obtainMessage(MSG_OUTPUT_ERROR, e));
}
}
return true;
case MSG_TEARDOWN:

View File

@ -441,7 +441,6 @@ public class McsService extends Service implements Handler.Callback {
}
private synchronized void connect() {
wasTornDown = false;
try {
closeAll();
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
@ -452,6 +451,7 @@ public class McsService extends Service implements Handler.Callback {
scheduleReconnect(this);
return;
}
wasTornDown = false;
logd(this, "Starting MCS connection...");
Socket socket = new Socket(SERVICE_HOST, SERVICE_PORT);
@ -744,6 +744,7 @@ public class McsService extends Service implements Handler.Callback {
resetCurrentDelay();
lastIncomingNetworkRealtime = SystemClock.elapsedRealtime();
} catch (Exception e) {
Log.w(TAG, "Exception when handling input: " + message, e);
rootHandler.sendMessage(rootHandler.obtainMessage(MSG_TEARDOWN, e));
}
}
@ -758,6 +759,7 @@ public class McsService extends Service implements Handler.Callback {
}
private static void closeAll() {
logd(null, "Closing all sockets...");
tryClose(inputStream);
tryClose(outputStream);
if (sslSocket != null) {