-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Description
In the current code (1.5.0 master and 1.5.1dev as of this date) the start of boluses is confirmed with the pump, and in general this allows that insulin to be tracked correctly (for predictions, temp basals, and subsequent boluses) until a full pull of history from the pump occurs. Most of the time this works as designed.
However, there are circumstances where I have observed this to fail for a short window. What happens is that I issue a bolus, I get the spinning cursor waiting for the bolus to be confirmed, and then the forecast updates to reflect that bolus insulin. However, sometimes during the next few minutes the forecast will then flip back to being very high, and the corresponding recommended temp basal (or recommended bolus if I go to the bolus screen) indicates that Loop has "forgotten" that there is a bolus being delivered (or recently completed).
If someone would like to try to reproduce this, it appears to me that one thing that can trigger this is the arrival of a new BG value shortly after the bolus is initiated. So to reproduce, wait until you need to bolus, then look at the timestamp on the BG value and wait until it is 3 or 4 minutes old. Then enter carbs as needed and deliver a bolus. Then watch the forecast in Loop and see what happens when the new BG arrives a minute or two later. Often the forecast will then flip back up to a predicted very high value. It only stays up briefly, and then soon comes back down again once the bolus is fetched from pump history. But there is a risk here - on Facebook someone reported delivering a second bolus during that window (due to doing two carb entries with different absorption times for different parts of a meal) and thus accidentally delivering too much insulin.
Looking at the code, I have a guess about what may be happening. When a bolus request is issued to the pump, function addRequestedBolus is called, which sets lastRequestedBolus. That variable is then used here in function getPendingInsulin to track that insulin while waiting for bolus confirmation. Then when Loop receives confirmation that the bolus started without error, the function addConfirmedBolus is called. This clears lastRequestedBolus, and calls addPendingPumpEvent in DoseStore. This adds the event to the current doseStore, which allows it to be used in predictions. All good so far.
According to this comment, pending events in the doseStore are cleared when either addPumpEvents or addReservoirValue are called. That seems like it should be fine, since either the history fetch should show that bolus, or the reservoir level should indicate that it has been delivered. But my guess is that this is where the glitch is happening. I don't know when the pump records a completed bolus to its internal history, but it may be that if the history fetch occurs while the bolus is still occurring, it could miss the current bolus, but still clear the pending bolus record from the doseStore. If this is true, a possible solution could be to check the pump status and not pull history if the pump is currently bolusing. Similarly, I don't know when mySentry packets are sent, but if the pump sends a packet with a reservoir value before a bolus is complete, possibly addReservoirValue could get a value that doesn't fully reflect the bolus delivery, but in the process might still clear the pending event from the doseStore, again leading to a temporarily-wrong amount of pending insulin.
Again, this is a narrow risk window, but still I think it would be good to close it if possible. I'll continue to look into some of the ideas I posed above, but I would be interested to hear thoughts from others. If the second comment about mySentry packets and reservoir levels is correct, this should never happen on x22 pumps, so any testing there would be useful.