{"record":{"id":"0f6c712a34f75b85","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-gate","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'gate')","messagePattern":"Value cannot be null\\. \\(Parameter 'gate'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Synchronize.cs","lineNumber":24,"sourceCode":"\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<TSource> Synchronize<TSource>(this IAsyncObservable<TSource> source)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n\n            return Create(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.Synchronize(observer)));\n        }\n\n        public static IAsyncObservable<TSource> Synchronize<TSource>(this IAsyncObservable<TSource> source, AsyncGate gate)\n        {\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","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Synchronize.cs#L6-L42","documentation":"Synchronize(source, gate) requires a non-null AsyncGate to serialize observer callbacks. The operator validates its arguments eagerly and throws ArgumentNullException before returning the observable. Passing a null gate means there is no lock object to coordinate concurrent OnNext/OnError/OnCompleted calls, so the library refuses to build the operator.","triggerScenarios":"Calling AsyncObservable.Synchronize(source, null) — e.g. a gate variable that was never initialized or was set to null after conditional cleanup.","commonSituations":"Developers caching an AsyncGate in a field that is null before initialization, or refactoring from the gate-less Synchronize(source) overload and passing null by mistake.","solutions":["Pass a constructed gate: new AsyncGate()","If you don't already have a gate, call Synchronize(source) without the gate parameter, which creates one internally","Ensure the field/property holding the gate is initialized before the operator is built"],"exampleFix":"// before\nvar gate = GetGate(); // may return null\nvar synced = source.Synchronize(gate);\n// after\nvar gate = GetGate() ?? new AsyncGate();\nvar synced = source.Synchronize(gate);","handlingStrategy":"validation","validationCode":"if (gate is null) throw new InvalidOperationException(\"Gate must be initialized before Synchronize\");\nvar synced = source.Synchronize(gate);","typeGuard":"bool HasGate(AsyncGate? gate) => gate is not null;","tryCatchPattern":"try { var synced = source.Synchronize(gate); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"gate\") { /* supply default gate */ }","preventionTips":["Initialize gate fields inline or in the constructor","Prefer the gate-less Synchronize(source) overload when you don't need to share a gate","Never null out shared gate fields; keep one instance per coordination scope"],"tags":["null-argument","synchronization","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"}