diff --git a/lib/tunnelbroker/use-peer-to-peer-message-handler.js b/lib/tunnelbroker/use-peer-to-peer-message-handler.js --- a/lib/tunnelbroker/use-peer-to-peer-message-handler.js +++ b/lib/tunnelbroker/use-peer-to-peer-message-handler.js @@ -260,31 +260,56 @@ resultDescription = 'Race condition while creating session with ' + `${senderDeviceID}, session version: ${sessionVersion}, ` + - `this device has a higher deviceID and the session will be kept`; + 'this device has a higher deviceID and the session will be ' + + 'kept'; } else { resultDescription = 'Race condition while creating session with ' + `${senderDeviceID}, session version: ${sessionVersion}, ` + - `this device has a lower deviceID and the session will overwritten`; - - const result = await olmAPI.contentInboundSessionCreator( - deviceKeys.identityKeysBlob.primaryIdentityPublicKeys, - encryptedData, - sessionVersion, - true, - ); + 'this device has a lower deviceID and the session will be ' + + 'overwritten'; + + let result; + try { + result = await olmAPI.contentInboundSessionCreator( + deviceKeys.identityKeysBlob.primaryIdentityPublicKeys, + encryptedData, + sessionVersion, + true, + ); + } catch (e2) { + const error2Message = getMessageForException(e2) ?? ''; + resultDescription = + 'Race condition while creating session with ' + + `${senderDeviceID}, session version: ${sessionVersion}, ` + + 'this device has a lower deviceID but overriding the' + + ` session failed ${error2Message}`; + throw e2; + } resultDescription = 'Overwrite session with device ' + `${senderDeviceID}: ${result}, ` + - `session version: ${sessionVersion}, starting resending messages`; - - await resendPeerToPeerMessages(senderDeviceID); + `session version: ${sessionVersion}, starting resending ` + + 'messages'; + + try { + await resendPeerToPeerMessages(senderDeviceID); + } catch (e2) { + const error2Message = getMessageForException(e2) ?? ''; + resultDescription = + 'Race condition while creating session with ' + + `${senderDeviceID}, session version: ${sessionVersion}, ` + + 'this device has a lower deviceID but resending the' + + ` messages failed ${error2Message}`; + throw e2; + } resultDescription = 'Overwrite session with device ' + `${senderDeviceID}: ${result}, ` + - `session version: ${sessionVersion}, finished resending messages`; + `session version: ${sessionVersion}, finished resending ` + + 'messages'; } } else { resultDescription = diff --git a/native/cpp/CommonCpp/CryptoTools/CryptoModule.cpp b/native/cpp/CommonCpp/CryptoTools/CryptoModule.cpp --- a/native/cpp/CommonCpp/CryptoTools/CryptoModule.cpp +++ b/native/cpp/CommonCpp/CryptoTools/CryptoModule.cpp @@ -311,9 +311,9 @@ if (this->hasSessionFor(targetDeviceId)) { std::shared_ptr existingSession = getSessionByDeviceId(targetDeviceId); - if (existingSession->getVersion() > sessionVersion) { + if (!overwrite && existingSession->getVersion() > sessionVersion) { throw std::runtime_error{"OLM_SESSION_ALREADY_CREATED"}; - } else if (existingSession->getVersion() == sessionVersion) { + } else if (!overwrite && existingSession->getVersion() == sessionVersion) { throw std::runtime_error{"OLM_SESSION_CREATION_RACE_CONDITION"}; }