From ba5529e86893b469d11fc111a2f8d7dedef934ed Mon Sep 17 00:00:00 2001 From: Andrei Vereha Date: Fri, 23 Jul 2021 19:30:25 +0200 Subject: [PATCH] fix race --- .../omnipod/dash/OmnipodDashPumpPlugin.kt | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt index 0304e2be93..4856f351b9 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt @@ -216,17 +216,29 @@ class OmnipodDashPumpPlugin @Inject constructor( override fun connect(reason: String) { aapsLogger.info(LTag.PUMP, "connect reason=$reason") podStateManager.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.CONNECTING + + synchronized(this) { + stopConnecting?.let { + aapsLogger.warn(LTag.PUMP, "Already connecting: $stopConnecting") + return + } + val stop = CountDownLatch(1) + stopConnecting = stop + } + thread( start = true, name = "ConnectionThread", ) { try { - val stop = CountDownLatch(1) - stopConnecting = stop - val error = omnipodManager.connect(stop).ignoreElements().blockingGet() - aapsLogger.info(LTag.PUMPCOMM, "connect error=$error") + stopConnecting?.let{ + val error = omnipodManager.connect(it).ignoreElements().blockingGet() + aapsLogger.info(LTag.PUMPCOMM, "connect error=${error}") + } } finally { - stopConnecting = null + synchronized(this) { + stopConnecting = null + } } } }