From d5647ce0212c721a869285483b96e1d1dd85f5d9 Mon Sep 17 00:00:00 2001 From: jbr7rr <> Date: Mon, 22 May 2023 20:05:52 +0200 Subject: [PATCH] Fix race condition on dash disconnect --- .../dash/driver/comm/session/Connection.kt | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/pump/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Connection.kt b/pump/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Connection.kt index 943a9524bc..177f8ed552 100644 --- a/pump/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Connection.kt +++ b/pump/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Connection.kt @@ -96,7 +96,6 @@ class Connection( connectionWaitCond.timeoutMs = newTimeout } podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.CONNECTED - _connectionWaitCond = null val discoverer = ServiceDiscoverer(aapsLogger, gatt, bleCommCallbacks, this) val discovered = discoverer.discoverServices(connectionWaitCond) @@ -122,21 +121,24 @@ class Connection( cmdBleIO.hello() cmdBleIO.readyToRead() dataBleIO.readyToRead() + _connectionWaitCond = null } @Synchronized fun disconnect(closeGatt: Boolean) { aapsLogger.debug(LTag.PUMPBTCOMM, "Disconnecting closeGatt=$closeGatt") - podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.DISCONNECTED - if (closeGatt) { - gattConnection?.close() - gattConnection = null - } else { + if (closeGatt == false && gattConnection != null) { + // Disconnect first, then close gatt gattConnection?.disconnect() + } else { + // Call with closeGatt=true only when ble is already disconnected or there is no connection + gattConnection?.close() + bleCommCallbacks.resetConnection() + gattConnection = null + session = null + msgIO = null + podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.DISCONNECTED } - bleCommCallbacks.resetConnection() - session = null - msgIO = null } private fun waitForConnection(connectionWaitCond: ConnectionWaitCondition): ConnectionState {