{"record":{"id":"d535238578c7516c","repo":"dotnet/reactive","slug":"argumentnullexception-timeout","errorCode":null,"errorMessage":"ArgumentNullException","messagePattern":"ArgumentNullException","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Timeout.cs","lineNumber":17,"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.Reactive.Concurrency;\nusing System.Reactive.Disposables;\nusing System.Threading;\nusing System.Threading.Tasks;\n\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<TSource> Timeout<TSource>(this IAsyncObservable<TSource> source, TimeSpan dueTime)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n\n            return CreateAsyncObservable<TSource>.From(\n                source,\n                dueTime,\n                static async (source, dueTime, observer) =>\n                {\n                    var sourceSubscription = new SingleAssignmentAsyncDisposable();\n\n                    var (sink, disposable) = await AsyncObserver.Timeout(observer, sourceSubscription, dueTime).ConfigureAwait(false);\n\n                    var sourceSubscriptionInner = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);\n\n                    await sourceSubscription.AssignAsync(sourceSubscriptionInner).ConfigureAwait(false);\n\n                    return disposable;\n                });\n        }\n","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Timeout.cs#L1-L35","documentation":"The Timeout operator shifts to an error/completion if the source produces no notification within dueTime, and it requires a non-null source observable. If source is null the operator cannot subscribe, so it throws ArgumentNullException at composition time, before any timer is scheduled.","triggerScenarios":"Calling source.Timeout(dueTime) where source == null — e.g. a null returned by an upstream factory, failed DI resolution, or a nullable variable not checked before chaining.","commonSituations":"Long operator chains where an early operator returned null; conditional pipeline construction missing an assignment; tests that forget to instantiate the observable under timeout behavior.","solutions":["Ensure the source observable is non-null before applying Timeout","Guard with a null check or substitute AsyncObservable.Never<TSource>()/Empty<TSource>() for a null source","Trace the upstream composition to find where null originated"],"exampleFix":"// before\nvar withTimeout = source.Timeout(TimeSpan.FromSeconds(5)); // source null\n// after\nvar withTimeout = (source ?? AsyncObservable.Never<int>()).Timeout(TimeSpan.FromSeconds(5));","handlingStrategy":"validation","validationCode":"if (source is null) throw new InvalidOperationException(\"source observable not initialized\");\nvar withTimeout = source.Timeout(TimeSpan.FromSeconds(5));","typeGuard":"bool IsSubscribable<TSource>(IAsyncObservable<TSource>? s) => s is not null;","tryCatchPattern":"try\n{\n    withTimeout = source.Timeout(TimeSpan.FromSeconds(5));\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\")\n{\n    withTimeout = AsyncObservable.Never<int>(); // or surface configuration error\n}","preventionTips":["Check the pipeline producing source before applying Timeout","Use Never/Empty sentinels instead of null observables","Enable nullable reference types so null sources surface at compile time"],"tags":["argument-null","timeout","observable"],"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"}