{"record":{"id":"0d38d8d31f0ac820","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-observer-synchronize","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'observer')","messagePattern":"Value cannot be null\\. \\(Parameter 'observer'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Synchronize.cs","lineNumber":38,"sourceCode":"        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (gate == null)\n                throw new ArgumentNullException(nameof(gate));\n\n            return CreateAsyncObservable<TSource>.From(\n                source,\n                gate,\n                static (source, gate, observer) => source.SubscribeSafeAsync(AsyncObserver.Synchronize(observer, gate)));\n        }\n    }\n\n    public partial class AsyncObserver\n    {\n        public static IAsyncObserver<TSource> Synchronize<TSource>(IAsyncObserver<TSource> observer)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n\n            return Synchronize(observer, new AsyncGate());\n        }\n\n        public static IAsyncObserver<TSource> Synchronize<TSource>(IAsyncObserver<TSource> observer, AsyncGate gate)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n            if (gate == null)\n                throw new ArgumentNullException(nameof(gate));\n\n            return Create<TSource>(\n                async x =>\n                {\n                    using (await gate.LockAsync().ConfigureAwait(false))\n                    {\n                        await observer.OnNextAsync(x).ConfigureAwait(false);\n                    }","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Synchronize.cs#L20-L56","documentation":"AsyncObserver.Synchronize(observer) wraps an observer so its callbacks are serialized under a new AsyncGate. A null observer cannot be wrapped, so the method throws ArgumentNullException immediately. This overload allocates the gate itself; only the observer must be supplied.","triggerScenarios":"Calling AsyncObserver.Synchronize<TSource>(null), typically from custom operator code that forwards an observer received as null or from a variable that was never assigned.","commonSituations":"Writing a custom IAsyncObservable whose Subscribe receives a null observer, or composing observers where an earlier factory call returned null.","solutions":["Pass the actual observer instance you intend to wrap","Check why the observer is null upstream (e.g. SubscribeSafeAsync was called with null) and fix that call site","Guard with a null check before calling Synchronize"],"exampleFix":"// before\nvar wrapped = AsyncObserver.Synchronize(observer); // observer null\n// after\nif (observer == null) throw new ArgumentNullException(nameof(observer));\nvar wrapped = AsyncObserver.Synchronize(observer);","handlingStrategy":"validation","validationCode":"if (observer is null) throw new ArgumentNullException(nameof(observer));\nvar wrapped = AsyncObserver.Synchronize(observer);","typeGuard":"bool IsValidObserver<T>(IAsyncObserver<T>? o) => o is not null;","tryCatchPattern":"try { var wrapped = AsyncObserver.Synchronize(observer); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"observer\") { /* log and use original pipeline */ }","preventionTips":["Always receive observers through Subscribe and forward them unchanged","Avoid factory methods that can return null observers","Assert observer != null at pipeline entry points"],"tags":["null-argument","observer","async-rx"],"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"}