We have an algorithm trading stocks live on Interactive Brokers. 

When there is an open position, it creates stop and limit orders for stop loss and take profit. 

If stop order receives PartialFill event, limit order is reduced appropriately, on Fill event, it is closed; and vise-versa, of course.

However, I have faced this strange behavior that sometimes order is not being canceled, when it should, here is what I can see in logs (order #2 is stop, order #3 is limit, some unneccessary lines removed):

# added stop and limit orders
2022-06-30 13:37:31 New Order Event: Time: 06/30/2022 13:37:31 OrderID: 2 EventID: 1 Symbol: ASPN Status: Submitted Quantity: -1264 StopPrice: 10.25 Message: Interactive Brokers Order Event
2022-06-30 13:37:31 New Order Event: Time: 06/30/2022 13:37:31 OrderID: 3 EventID: 1 Symbol: ASPN Status: Submitted Quantity: -1264 LimitPrice: 10.62 Message: Interactive Brokers Order Event

# partial and eventually complete fill of stop order
2022-06-30 13:38:32 New Order Event: Time: 06/30/2022 13:38:32 OrderID: 2 EventID: 4 Symbol: ASPN Status: PartiallyFilled Quantity: -1264 FillQuantity: -100 FillPrice: 10.2738 USD StopPrice: 10.27 OrderFee: 0.406784 USD Message: Interactive Brokers Order Fill Event - 1164 remaining
2022-06-30 13:38:32 New Order Event: Time: 06/30/2022 13:38:32 OrderID: 2 EventID: 5 Symbol: ASPN Status: PartiallyFilled Quantity: -1264 FillQuantity: -100 FillPrice: 10.27 USD StopPrice: 10.27 OrderFee: 0.406776 USD Message: Interactive Brokers Order Fill Event - 1064 remaining
2022-06-30 13:38:32 New Order Event: Time: 06/30/2022 13:38:32 OrderID: 2 EventID: 6 Symbol: ASPN Status: PartiallyFilled Quantity: -1264 FillQuantity: -700 FillPrice: 10.27 USD StopPrice: 10.27 OrderFee: 4.947429 USD Message: Interactive Brokers Order Fill Event - 364 remaining
2022-06-30 13:38:32 New Order Event: Time: 06/30/2022 13:38:32 OrderID: 2 EventID: 7 Symbol: ASPN Status: PartiallyFilled Quantity: -1264 FillQuantity: -100 FillPrice: 10.27 USD StopPrice: 10.27 OrderFee: 0.706776 USD Message: Interactive Brokers Order Fill Event - 264 remaining
2022-06-30 13:38:32 New Order Event: Time: 06/30/2022 13:38:32 OrderID: 2 EventID: 8 Symbol: ASPN Status: Filled Quantity: -1264 FillQuantity: -264 FillPrice: 10.27 USD StopPrice: 10.27 OrderFee: 1.865887 USD Message: Interactive Brokers Order Fill Event

# cancelling limit order
2022-06-30 13:38:32 New Order Event: Time: 06/30/2022 13:38:32 OrderID: 3 EventID: 2 Symbol: ASPN Status: CancelPending Quantity: -1164 LimitPrice: 10.62

# trying to update limit order on Partial fills
2022-06-30 13:38:32 New Order Event: Time: 06/30/2022 13:38:32 OrderID: 3 EventID: 3 Symbol: ASPN Status: UpdateSubmitted Quantity: -564 LimitPrice: 10.62 Message: Interactive Brokers Order Event

# this is my code logging partial fills
2022-06-30 13:38:32 Partial fill for stop loss: reducing qty by 100.0
2022-06-30 13:38:32 Partial fill for stop loss: reducing qty by 100.0
2022-06-30 13:38:32 Partial fill for stop loss: reducing qty by 700.0
2022-06-30 13:38:32 Partial fill for stop loss: reducing qty by 100.0

# Status=New??? Why would would it create a new order now?
2022-06-30 13:38:32 New Order Event: Time: 06/30/2022 13:38:32 OrderID: 3 EventID: 4 Symbol: ASPN Status: New Quantity: -564 LimitPrice: 10.62 Message: Interactive Brokers Order Event

2022-06-30 13:38:32 New Order Event: Time: 06/30/2022 13:38:32 OrderID: 3 EventID: 5 Symbol: ASPN Status: UpdateSubmitted Quantity: -564 LimitPrice: 10.62 Message: Interactive Brokers Order Event

So in my understanding, when PartialFill event comes later that Filled event (which is understandable in async environment), my code first cancels order and then tries to update its quantity. I would expect in this situation for order to be left canceled, but instead it has recreated a new order. It doesn't seem right to me.

Sorry, I cannot share the full code, but it is pretty straightforward and was working as described above for quite some time without issues.