{"record":{"id":"795b4eac1c8006c7","repo":"dotnet/reactive","slug":"the-asynchronous-operation-failed-with-a-null-error-code-795b4e","errorCode":null,"errorMessage":"The asynchronous operation failed with a null error code.","messagePattern":"The asynchronous operation failed with a null error code\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive.WindowsRuntime/AsyncInfoToObservableBridge.cs","lineNumber":49,"sourceCode":"                progress?.Report(p);\n            });\n\n            Done(info, info.Status, true);\n        }\n\n        private void Done(IAsyncInfo info, AsyncStatus status, bool initial)\n        {\n            var error = default(Exception);\n            var result = default(TResult);\n\n            //\n            // Initial interactions with the IAsyncInfo object. Those could fail, which indicates\n            // a rogue implementation. Failure is just propagated out.\n            //\n            switch (status)\n            {\n                case AsyncStatus.Error:\n                    error = info.ErrorCode ?? throw new InvalidOperationException(\"The asynchronous operation failed with a null error code.\");\n                    break;\n                case AsyncStatus.Canceled:\n                    error = new OperationCanceledException();\n                    break;\n                case AsyncStatus.Completed:\n                    if (_getResult != null)\n                    {\n                        result = _getResult(info);\n                    }\n\n                    break;\n                default:\n                    if (!initial)\n                    {\n                        throw new InvalidOperationException(\"The asynchronous operation completed unexpectedly.\");\n                    }\n\n                    _onCompleted(info, (iai, s) => Done(iai, s, false));","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive.WindowsRuntime/AsyncInfoToObservableBridge.cs#L31-L67","documentation":"AsyncInfoToObservableBridge.Done inspects the IAsyncInfo status after the operation finishes. When status is AsyncStatus.Error, it reads info.ErrorCode to propagate the failure; if ErrorCode is also null the implementation is considered rogue and the bridge throws InvalidOperationException instead of propagating a meaningless null exception.","triggerScenarios":"An IAsyncInfo implementation reports AsyncStatus.Error but its ErrorCode property returns null (a non-conformant WinRT async implementation), observed during Done processing.","commonSituations":"Custom or third-party WinRT async implementations that fail to set ErrorCode when transitioning to the Error state; interop with hand-rolled IAsyncOperation wrappers.","solutions":["Fix the IAsyncInfo implementation so ErrorCode is always set when status becomes Error.","Wrap the observable subscription in try-catch for InvalidOperationException and substitute a generic exception.","Prefer standard WinRT async sources (IAsyncAction/Operation produced by the OS or Task.AsAsyncAction) that always populate ErrorCode."],"exampleFix":"// before (non-conformant impl)\nstatus = AsyncStatus.Error; // ErrorCode left null\n// after\nstatus = AsyncStatus.Error;\nErrorCode = new InvalidOperationException(\"actual failure\");","handlingStrategy":"try-catch","validationCode":"if (info.Status == AsyncStatus.Error && info.ErrorCode == null)\n    throw new InvalidOperationException(\"Rogue async implementation: Error with null ErrorCode\");","typeGuard":"bool HasErrorCode(IAsyncInfo info) => info.Status != AsyncStatus.Error || info.ErrorCode != null;","tryCatchPattern":"try { await obs; }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"null error code\"))\n{ /* treat as non-conformant source; substitute generic error */ }","preventionTips":["Use standard WinRT async operations rather than hand-rolled IAsyncInfo implementations","Always set ErrorCode in custom implementations before flipping status to Error","Validate third-party async wrappers in tests for Error-state conformance"],"tags":["invalid-state","winrt","async","reactive-extensions"],"backgroundTag":"internal-invariant-violation","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}