prevent race condition when fetching device ids

This commit is contained in:
Daniel Gultsch 2018-10-03 22:03:40 +02:00
parent f608fb349a
commit 23282484d6
2 changed files with 47 additions and 43 deletions

View File

@ -1021,13 +1021,15 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded {
}
if (packet != null) {
mXmppConnectionService.sendIqPacket(account, packet, (account, response) -> {
synchronized (fetchDeviceIdsMap) {
List<OnDeviceIdsFetched> callbacks = fetchDeviceIdsMap.remove(jid);
if (response.getType() == IqPacket.TYPE.RESULT) {
fetchDeviceListStatus.put(jid, true);
Element item = mXmppConnectionService.getIqParser().getItem(response);
Set<Integer> deviceIds = mXmppConnectionService.getIqParser().deviceIds(item);
registerDevices(jid, deviceIds);
final List<OnDeviceIdsFetched> callbacks;
synchronized (fetchDeviceIdsMap) {
callbacks = fetchDeviceIdsMap.remove(jid);
}
if (callbacks != null) {
for (OnDeviceIdsFetched c : callbacks) {
c.fetched(jid, deviceIds);
@ -1039,13 +1041,16 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded {
} else {
fetchDeviceListStatus.put(jid, false);
}
final List<OnDeviceIdsFetched> callbacks;
synchronized (fetchDeviceIdsMap) {
callbacks = fetchDeviceIdsMap.remove(jid);
}
if (callbacks != null) {
for (OnDeviceIdsFetched c : callbacks) {
c.fetched(jid, null);
}
}
}
}
});
}
}
@ -1157,8 +1162,9 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded {
Set<SignalProtocolAddress> addresses = new HashSet<>();
for (Jid jid : getCryptoTargets(conversation)) {
Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Finding devices without session for " + jid);
if (deviceIds.get(jid) != null) {
for (Integer foreignId : this.deviceIds.get(jid)) {
Set<Integer> ids = deviceIds.get(jid);
if (deviceIds.get(jid) != null && !ids.isEmpty()) {
for (Integer foreignId : ids) {
SignalProtocolAddress address = new SignalProtocolAddress(jid.toString(), foreignId);
if (sessions.get(address) == null) {
IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
@ -1181,8 +1187,8 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded {
Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Have no target devices in PEP!");
}
}
if (deviceIds.get(account.getJid().asBareJid()) != null) {
for (Integer ownId : this.deviceIds.get(account.getJid().asBareJid())) {
Set<Integer> ownIds = this.deviceIds.get(account.getJid().asBareJid());
for (Integer ownId : (ownIds != null ? ownIds : new HashSet<Integer>())) {
SignalProtocolAddress address = new SignalProtocolAddress(account.getJid().asBareJid().toString(), ownId);
if (sessions.get(address) == null) {
IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
@ -1200,7 +1206,6 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded {
}
}
}
}
return addresses;
}
@ -1215,12 +1220,7 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded {
}
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": createSessionsIfNeeded() - jids with empty device list: " + jidsWithEmptyDeviceList);
if (jidsWithEmptyDeviceList.size() > 0) {
fetchDeviceIds(jidsWithEmptyDeviceList, new OnMultipleDeviceIdFetched() {
@Override
public void fetched() {
createSessionsIfNeededActual(conversation);
}
});
fetchDeviceIds(jidsWithEmptyDeviceList, () -> createSessionsIfNeededActual(conversation));
return true;
} else {
return createSessionsIfNeededActual(conversation);

View File

@ -78,6 +78,10 @@ public class FingerprintStatus implements Comparable<FingerprintStatus> {
return status;
}
public static FingerprintStatus createActive(Boolean trusted) {
return createActive(trusted != null && trusted);
}
public static FingerprintStatus createActive(boolean trusted) {
final FingerprintStatus status = new FingerprintStatus();
status.trust = trusted ? Trust.TRUSTED : Trust.UNTRUSTED;