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 android.util.Log;
import com.squareup.wire.Message; import com.squareup.wire.Message;
import com.squareup.wire.Wire;
import org.microg.gms.gcm.mcs.Close; import org.microg.gms.gcm.mcs.Close;
import org.microg.gms.gcm.mcs.DataMessageStanza; import org.microg.gms.gcm.mcs.DataMessageStanza;
@ -57,7 +56,7 @@ public class McsInputStream extends Thread implements Closeable {
private int streamId = 0; private int streamId = 0;
private long lastMsgTime = 0; private long lastMsgTime = 0;
private boolean closed = false; private volatile boolean closed = false;
public McsInputStream(InputStream is, Handler mainHandler) { public McsInputStream(InputStream is, Handler mainHandler) {
this(is, mainHandler, false); this(is, mainHandler, false);
@ -83,7 +82,11 @@ public class McsInputStream extends Thread implements Closeable {
} }
} }
} catch (IOException e) { } 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 { try {
is.close(); is.close();
@ -120,7 +123,7 @@ public class McsInputStream extends Thread implements Closeable {
Log.d(TAG, "Reading from MCS version: " + version); Log.d(TAG, "Reading from MCS version: " + version);
initialized = true; initialized = true;
} catch (IOException e) { } 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 version = MCS_VERSION_CODE;
private int streamId = 0; private int streamId = 0;
private Handler mainHandler; private final Handler mainHandler;
private Handler myHandler; private Handler myHandler;
private boolean closed = false; private volatile boolean closed = false;
public McsOutputStream(OutputStream os, Handler mainHandler) { public McsOutputStream(OutputStream os, Handler mainHandler) {
this(os, mainHandler, false); this(os, mainHandler, false);
@ -78,7 +78,11 @@ public class McsOutputStream extends Thread implements Handler.Callback, Closeab
writeInternal((Message) msg.obj, msg.arg1); writeInternal((Message) msg.obj, msg.arg1);
mainHandler.dispatchMessage(mainHandler.obtainMessage(MSG_OUTPUT_DONE, msg.arg1, msg.arg2, msg.obj)); mainHandler.dispatchMessage(mainHandler.obtainMessage(MSG_OUTPUT_DONE, msg.arg1, msg.arg2, msg.obj));
} catch (IOException e) { } 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; return true;
case MSG_TEARDOWN: case MSG_TEARDOWN:

View File

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