{"record":{"id":"58c7ac036e6ec392","repo":"dotnet/reactive","slug":"statemachine","errorCode":null,"errorMessage":"stateMachine","messagePattern":"stateMachine","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Runtime/CompilerServices/TaskObservableMethodBuilder.cs","lineNumber":54,"sourceCode":"#pragma warning disable CA1000 // (Do not declare static members on generic types.) Async method builders are required to define a static Create method, and are require to be generic when the async type produces a result.\n        public static TaskObservableMethodBuilder<T> Create() => default;\n#pragma warning restore CA1000 // Do not declare static members on generic types\n\n        /// <summary>\n        /// Begins running the builder with the associated state machine.\n        /// </summary>\n        /// <typeparam name=\"TStateMachine\">The type of the state machine.</typeparam>\n        /// <param name=\"stateMachine\">The state machine instance, passed by reference.</param>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"stateMachine\"/> is <c>null</c>.</exception>\n#pragma warning disable CA1045 // (Avoid ref.) Required because this is an async method builder\n#pragma warning disable IDE0251 // (Make readonly.) Not part of the standard method builder pattern.\n        public void Start<TStateMachine>(ref TStateMachine stateMachine)\n#pragma warning restore CA1045, IDE0251\n            where TStateMachine : IAsyncStateMachine\n        {\n            if (stateMachine == null)\n            {\n                throw new ArgumentNullException(nameof(stateMachine));\n            }\n\n            stateMachine.MoveNext();\n        }\n\n        /// <summary>\n        /// Associates the builder with the specified state machine.\n        /// </summary>\n        /// <param name=\"stateMachine\">The state machine instance to associate with the builder.</param>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"stateMachine\"/> is <c>null</c>.</exception>\n        /// <exception cref=\"InvalidOperationException\">The state machine was previously set.</exception>\n        public void SetStateMachine(IAsyncStateMachine stateMachine)\n        {\n            if (_stateMachine != null)\n            {\n                throw new InvalidOperationException();\n            }\n","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Runtime/CompilerServices/TaskObservableMethodBuilder.cs#L36-L72","documentation":"AsyncTaskMethodBuilder-style Start<TStateMachine> in the custom TaskObservableMethodBuilder throws ArgumentNullException when the state machine box is null. The compiler-generated async state machine is passed by ref to Start, which calls MoveNext to begin execution; a null machine cannot run. This builder is used by async methods returning IObservable-based tasks, and null here almost always means the async method builder infrastructure was invoked incorrectly.","triggerScenarios":"Calling builder.Start<TStateMachine>(ref machine) manually with an uninitialized (null) state machine, or custom/rewritten async tooling (e.g. post-processing IL rewriters, custom awaiter libraries) that fails to construct the state machine struct before calling Start.","commonSituations":"Custom async-method-builder experiments targeting Rx observables; IL-weaving or AOP frameworks (profilers, mocking tools) that rewrite async state machines; debugging stepping into builder internals after a broken build.","solutions":["Do not call Start manually — let the C# compiler construct and start the state machine for async methods using this builder.","If you must call it, construct the TStateMachine struct (never pass null/default-of-reference) before Start.","Check that IL-rewriting tooling (profiler, weaver, mocking library) supports this async method builder and is up to date.","Rebuild/reinstall System.Reactive so compiler-generated builder bindings match the library version."],"exampleFix":"// before\nTaskObservableMethodBuilder<int> b = default;\nMyStateMachine machine = null; // invalid\nb.Start(ref machine);\n// after\nvar b = TaskObservableMethodBuilder<int>.Create();\nvar machine = new MyStateMachine { Builder = b };\nb.Start(ref machine); // compiler does this automatically for async methods","handlingStrategy":"try-catch","validationCode":"if (stateMachine == null) throw new ArgumentNullException(nameof(stateMachine)); // guard before calling builder.Start","typeGuard":"bool CanStart<TStateMachine>(TStateMachine m) where TStateMachine : class, IAsyncStateMachine => m != null;","tryCatchPattern":"try { builder.Start(ref stateMachine); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"stateMachine\") { /* state machine was not constructed; inspect async tooling/rewriters */ throw; }","preventionTips":["Let the compiler drive async method builders; avoid manual Start calls","Keep IL-weaving/profiler tooling compatible with System.Reactive's builders","Rebuild after upgrading System.Reactive so generated code matches"],"tags":["argument-null","async","state-machine","compiler-services"],"backgroundTag":"null-argument","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"}