@@ -486,6 +486,12 @@ class TrezorRepo @Inject constructor(
486486 fun hasKnownDevices (): Boolean = _state .value.knownDevices.isNotEmpty()
487487
488488 suspend fun autoReconnect (walletIndex : Int = 0): Result <TrezorFeatures > = withContext(ioDispatcher) {
489+ if (isConnectInProgress()) {
490+ // A live handshake looks like a stale session (transport connected,
491+ // features pending), so resetting here would drop the session the
492+ // user is entering their PIN or pairing code into.
493+ return @withContext Result .failure(AppError (" Connect already in progress" ))
494+ }
489495 val knownDevices = _state .value.knownDevices.ifEmpty { loadKnownDevices() }
490496 if (knownDevices.isEmpty()) {
491497 return @withContext Result .failure(AppError (" No known devices" ))
@@ -689,14 +695,23 @@ class TrezorRepo @Inject constructor(
689695 */
690696 private suspend fun retryAutoReconnect () {
691697 repeat(TRANSPORT_RESTORED_MAX_ATTEMPTS ) { attempt ->
692- val current = _state .value
693- if (current.connected != null || current.isConnecting || current.isAutoReconnecting) return
698+ if (_state .value.connected != null || isConnectInProgress()) return
694699 delay(TRANSPORT_RESTORED_RECONNECT_DELAY * (attempt + 1 ))
700+ // A connect may have started while this attempt was waiting.
701+ if (_state .value.connected != null || isConnectInProgress()) return
695702 Logger .info(" Attempting auto-reconnect after transport restored, attempt '${attempt + 1 } '" , context = TAG )
696703 if (autoReconnect().isSuccess) return
697704 }
698705 }
699706
707+ private fun isConnectInProgress (): Boolean = run {
708+ val current = _state .value
709+ current.isConnecting ||
710+ current.isAutoReconnecting ||
711+ needsPinEntry.value ||
712+ needsPairingCode.value
713+ }
714+
700715 private suspend fun addOrUpdateKnownDevice (deviceInfo : TrezorDeviceInfo , features : TrezorFeatures ) {
701716 val existing = _state .value.knownDevices
702717 val previous = existing.find { it.id == deviceInfo.id }
0 commit comments