From 71c4c411b2b235b8eae210facba8ba7a371fd076 Mon Sep 17 00:00:00 2001 From: TebbeUbben Date: Sat, 2 Feb 2019 11:41:16 +0100 Subject: [PATCH] Fix race condition for bolus cancel --- .../PumpInsightLocal/LocalInsightPlugin.java | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsightLocal/LocalInsightPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsightLocal/LocalInsightPlugin.java index 32270999ea..a928d56504 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsightLocal/LocalInsightPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsightLocal/LocalInsightPlugin.java @@ -152,6 +152,8 @@ public class LocalInsightPlugin extends PluginBase implements PumpInterface, Con } }; + private final Object $bolusLock = new Object[0]; + private boolean bolusInProgress; private int bolusID = -1; private List profileBlocks; private boolean limitsFetched; @@ -498,12 +500,14 @@ public class LocalInsightPlugin extends PluginBase implements PumpInterface, Con PumpEnactResult result = new PumpEnactResult(); if (detailedBolusInfo.insulin > 0) { try { - DeliverBolusMessage bolusMessage = new DeliverBolusMessage(); - bolusMessage.setBolusType(BolusType.STANDARD); - bolusMessage.setDuration(0); - bolusMessage.setExtendedAmount(0); - bolusMessage.setImmediateAmount(detailedBolusInfo.insulin); - bolusID = connectionService.requestMessage(bolusMessage).await().getBolusId(); + synchronized ($bolusLock) { + DeliverBolusMessage bolusMessage = new DeliverBolusMessage(); + bolusMessage.setBolusType(BolusType.STANDARD); + bolusMessage.setDuration(0); + bolusMessage.setExtendedAmount(0); + bolusMessage.setImmediateAmount(detailedBolusInfo.insulin); + bolusID = connectionService.requestMessage(bolusMessage).await().getBolusId(); + } result.success = true; result.enacted = true; Treatment t = new Treatment(); @@ -562,16 +566,18 @@ public class LocalInsightPlugin extends PluginBase implements PumpInterface, Con @Override public void stopBolusDelivering() { new Thread(() -> { - try { - alertService.ignore(AlertType.WARNING_38); - CancelBolusMessage cancelBolusMessage = new CancelBolusMessage(); - cancelBolusMessage.setBolusID(bolusID); - connectionService.requestMessage(cancelBolusMessage).await(); - confirmAlert(AlertType.WARNING_38); - } catch (InsightException e) { - log.info("Exception while canceling bolus: " + e.getClass().getCanonicalName()); - } catch (Exception e) { - log.error("Exception while canceling bolus", e); + synchronized ($bolusLock) { + try { + alertService.ignore(AlertType.WARNING_38); + CancelBolusMessage cancelBolusMessage = new CancelBolusMessage(); + cancelBolusMessage.setBolusID(bolusID); + connectionService.requestMessage(cancelBolusMessage).await(); + confirmAlert(AlertType.WARNING_38); + } catch (InsightException e) { + log.info("Exception while canceling bolus: " + e.getClass().getCanonicalName()); + } catch (Exception e) { + log.error("Exception while canceling bolus", e); + } } }).start(); }