{"record":{"id":"39ac96523503d103","repo":"QuantConnect/Lean","slug":"unexpected-cancel-event-orderevent","errorCode":null,"errorMessage":"Unexpected cancel event: {orderEvent}","messagePattern":"Unexpected cancel event: (.+?)","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/DuplicateOptionAssignmentRegressionAlgorithm.cs","lineNumber":119,"sourceCode":"                else\n                {\n                    throw new RegressionTestException($\"Unexpected order fill event: {orderEvent}\");\n                }\n            }\n            else if (orderEvent.Status == OrderStatus.CancelPending)\n            {\n                // We receive the delisting warning before the order cancel is requested\n                if (!_optionSold || !_optionAssigned || !_stockAssigned || !_optionDelistedWarningReceived)\n                {\n                    throw new RegressionTestException($\"Unexpected cancel pending event: {orderEvent}\");\n                }\n            }\n            else if (orderEvent.Status == OrderStatus.Canceled)\n            {\n                // The delisted event is received before the order is canceled\n                if (!_optionSold || !_optionAssigned || !_stockAssigned || !_optionDelistedWarningReceived || !_optionDelisted)\n                {\n                    throw new RegressionTestException($\"Unexpected cancel event: {orderEvent}\");\n                }\n\n                _orderCanceled = true;\n            }\n        }\n\n        public override void OnDelistings(Delistings delistings)\n        {\n            if (!delistings.TryGetValue(_option, out var delisting))\n            {\n                throw new RegressionTestException($\"Unexpected delisting events\");\n            }\n\n            if (delisting.Type == DelistingType.Warning)\n            {\n                if (!_optionSold || !_optionAssigned || !_stockAssigned || _optionDelistedWarningReceived)\n                {\n                    throw new RegressionTestException($\"Unexpected delisting warning event: {delisting}\");","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/DuplicateOptionAssignmentRegressionAlgorithm.cs#L101-L137","documentation":"When an OrderEvent with Status == Canceled arrives, the regression requires the complete prior sequence: _optionSold, _optionAssigned, _stockAssigned, _optionDelistedWarningReceived, and _optionDelisted must all be true. The delisted event must precede the final cancellation confirmation. A failure means the order was canceled before the full delisting/exercise sequence completed.","triggerScenarios":"The Canceled event arrives before the option is fully delisted (_optionDelisted false), or before assignment/delivery completed — e.g., the order was canceled by a different mechanism (timeout, margin) rather than delisting.","commonSituations":"A Lean change in the ordering of delisting events vs order cancellation, the cancel firing before the Delisted (as opposed to Warning) delisting type is processed, or an early cancel from another source.","solutions":["Log the timestamped sequence of all OrderEvents and OnDelistings calls to find which flag is still false at Canceled.","Verify the DelistingType.Delisted event is processed before the Canceled confirmation.","Check for a Lean regression in delisting-vs-cancel ordering.","If the new ordering is correct, relax the gating to require only the flags that must precede cancellation."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"else if (orderEvent.Status == OrderStatus.Canceled)\n{\n    var fullyReady = _optionSold && _optionAssigned && _stockAssigned\n        && _optionDelistedWarningReceived && _optionDelisted;\n    if (!fullyReady)\n    {\n        Debug($\"Canceled before full delisting: delisted={_optionDelisted} warn={_optionDelistedWarningReceived}\");\n        throw new RegressionTestException($\"Unexpected cancel event: {orderEvent}\");\n    }\n    _orderCanceled = true;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Log the full event sequence to find which flag is false at Canceled.","Verify the DelistingType.Delisted event is processed before the Canceled confirmation.","Relax gating only after confirming the new ordering is intentional."],"tags":["quantconnect","options","order-cancellation","delisting","state-machine","regression-test"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}