Skip to content

Commit

Permalink
host not updated when switching hosts, race condition on logout
Browse files Browse the repository at this point in the history
  • Loading branch information
or-else committed Aug 13, 2022
1 parent 8e0d5ab commit 6321fe7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:hint="@string/password_hint"
app:passwordToggleEnabled="true">
app:endIconMode="password_toggle">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
34 changes: 24 additions & 10 deletions tinodesdk/src/main/java/co/tinode/tinodesdk/Tinode.java
Original file line number Diff line number Diff line change
Expand Up @@ -428,11 +428,13 @@ protected PromisedReply<ServerMessage> connect(@NotNull URI serverURI, boolean b
synchronized (mConnLock) {
boolean sameHost = serverURI.equals(mServerURI);
// Connection already exists and connected.
if (mConnection != null && mConnection.isConnected()) {
// If the connection is live and the server address has not changed, return a resolved promise.
if (sameHost) {
if (mConnection != null) {
if (mConnection.isConnected() && sameHost) {
// If the connection is live and the server address has not changed, return a resolved promise.
return new PromisedReply<>((ServerMessage) null);
} else {
}

if (!sameHost) {
// Clear auto-login because saved credentials won't work with the new server.
setAutoLogin(null, null);
// Stop exponential backoff timer if it's running.
Expand Down Expand Up @@ -469,6 +471,7 @@ protected PromisedReply<ServerMessage> connect(@NotNull URI serverURI, boolean b
*/
public PromisedReply<ServerMessage> connect(@Nullable String hostName, boolean tls, boolean background) {
URI connectTo = mServerURI;
Log.d(TAG, "Connecting to " + hostName + " (" + (tls ? "HTTPS" : "http") + ")");
if (hostName != null) {
try {
connectTo = createWebsocketURI(hostName, tls);
Expand All @@ -477,6 +480,7 @@ public PromisedReply<ServerMessage> connect(@Nullable String hostName, boolean t
}
}

Log.d(TAG, "Connection URI=" + connectTo.toString());
return connect(connectTo, background);
}

Expand Down Expand Up @@ -1478,18 +1482,22 @@ public void setAutoLoginToken(String token) {
* Log out current user.
*/
public void logout() {
mMyUid = null;
mServerParams = null;
setAutoLoginToken(null);

if (mStore != null) {
// Clear token here, because of logout setDeviceToken will not be able to clear it.
mStore.saveDeviceToken(null);
mStore.logout();
}

// Best effort to clear device token on logout.
// The app logs out even if the token request has failed.
setDeviceToken(NULL_VALUE).thenFinally(new PromisedReply.FinalListener() {
@Override
public void onFinally() {
disconnect(false);
mMyUid = null;
mServerParams = null;

if (mStore != null) {
mStore.logout();
}
}
});
}
Expand Down Expand Up @@ -2536,6 +2544,12 @@ protected void onMessage(Connection conn, String message) {
@Override
protected void onDisconnect(Connection conn, boolean byServer, int code, String reason) {
handleDisconnect(byServer, -code, reason);
// Promises may have already been rejected if onError was called first.
try {
rejectPromises(new ServerResponseException(503, "disconnected"));
} catch (Exception ignored) {
// Don't throw an exception as no one can catch it.
}
}

@Override
Expand Down

0 comments on commit 6321fe7

Please sign in to comment.