{"record":{"id":"a0124cbec3ff0899","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-source-synchronize","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'source')","messagePattern":"Value cannot be null\\. \\(Parameter 'source'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Synchronize.cs","lineNumber":14,"sourceCode":"﻿// Licensed to the .NET Foundation under one or more agreements.\n// The .NET Foundation licenses this file to you under the MIT License.\n// See the LICENSE file in the project root for more information. \n\nusing System.Threading;\n\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","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Synchronize.cs#L1-L32","documentation":"The Synchronize extension serializes notifications from the source through an AsyncGate and first validates that the source observable is non-null, throwing ArgumentNullException at Synchronize.cs:14. Eager validation keeps the failure at the composition call site instead of during subscription.","triggerScenarios":"Calling source.Synchronize() where source is a null IAsyncObservable<T>, typically when the source came from a lookup, event stream registry, or conditional initialization that returned null.","commonSituations":"Multithreaded apps wiring up synchronized pipelines where the source is assigned on another thread and not yet initialized; misconfigured dependency injection returning a null observable binding.","solutions":["Ensure the source observable is initialized before calling .Synchronize()","If the source comes from DI or a registry, validate the resolution result and fail fast with a clear message","Check for race conditions where the pipeline is built before the source is assigned","Add a null guard with a descriptive exception at the point the source is obtained"],"exampleFix":"// before\nvar sync = registry.GetObservable(key).Synchronize(); // GetObservable may return null\n// after\nvar src = registry.GetObservable(key) ?? throw new InvalidOperationException($\"no observable registered for {key}\");\nvar sync = src.Synchronize();","handlingStrategy":"validation","validationCode":"if (source is null)\n    throw new InvalidOperationException(\"Synchronize requires a non-null source observable.\");\nvar sync = source.Synchronize();","typeGuard":"static bool HasSource<T>(IAsyncObservable<T>? s) => s is not null;","tryCatchPattern":"try\n{\n    var sync = source.Synchronize();\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\")\n{\n    logger.LogError(ex, \"Synchronize called with null source; verify initialization order/DI binding\");\n}","preventionTips":["Initialize source observables before building dependent pipelines","Validate DI/registry resolutions for observables and fail fast on null","Be wary of building pipelines on one thread while sources are assigned on another","Use required members or constructor injection so observables cannot be null"],"tags":["argumentnull","async-rx","operator","synchronize"],"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"}