{"record":{"id":"adafff35d03b3302","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-cts-cancellationdisposable","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'cts')","messagePattern":"Value cannot be null\\. \\(Parameter 'cts'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Disposables/CancellationDisposable.cs","lineNumber":23,"sourceCode":"using System.Threading;\n\nnamespace System.Reactive.Disposables\n{\n    /// <summary>\n    /// Represents a disposable resource that has an associated <seealso cref=\"CancellationToken\"/> that will be set to the cancellation requested state upon disposal.\n    /// </summary>\n    public sealed class CancellationDisposable : ICancelable\n    {\n        private readonly CancellationTokenSource _cts;\n\n        /// <summary>\n        /// Initializes a new instance of the <see cref=\"CancellationDisposable\"/> class that uses an existing <seealso cref=\"CancellationTokenSource\"/>.\n        /// </summary>\n        /// <param name=\"cts\"><seealso cref=\"CancellationTokenSource\"/> used for cancellation.</param>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"cts\"/> is <c>null</c>.</exception>\n        public CancellationDisposable(CancellationTokenSource cts)\n        {\n            _cts = cts ?? throw new ArgumentNullException(nameof(cts));\n        }\n\n        /// <summary>\n        /// Initializes a new instance of the <see cref=\"CancellationDisposable\"/> class that uses a new <seealso cref=\"CancellationTokenSource\"/>.\n        /// </summary>\n        public CancellationDisposable()\n            : this(new CancellationTokenSource())\n        {\n        }\n\n        /// <summary>\n        /// Gets the <see cref=\"CancellationToken\"/> used by this <see cref=\"CancellationDisposable\"/>.\n        /// </summary>\n        public CancellationToken Token => _cts.Token;\n\n        /// <summary>\n        /// Cancels the underlying <seealso cref=\"CancellationTokenSource\"/>.\n        /// </summary>","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Disposables/CancellationDisposable.cs#L5-L41","documentation":"CancellationDisposable's constructor accepts an existing CancellationTokenSource and stores it as the source it will cancel on Dispose. A null cts has nothing to cancel, so the constructor throws ArgumentNullException immediately.","triggerScenarios":"new CancellationDisposable((CancellationTokenSource)null) — typically when the CTS comes from a field, property, method parameter, or linked-source helper that was never initialized.","commonSituations":"Passing a CancellationTokenSource obtained from an API that can return null; refactoring where a nullable CTS field is passed without a check.","solutions":["Ensure the CancellationTokenSource is initialized before constructing the disposable (e.g. cts ??= new CancellationTokenSource())","If you have no existing source, call the parameterless constructor CancellationDisposable() which creates its own CTS","If the value is legitimately nullable, guard: if (cts != null) new CancellationDisposable(cts); else new CancellationDisposable()"],"exampleFix":"// before\nCancellationDisposable cd = new CancellationDisposable(_cts); // _cts may be null\n// after\nCancellationDisposable cd = new CancellationDisposable(_cts ?? new CancellationTokenSource());","handlingStrategy":"type-guard","validationCode":"if (cts == null) cts = new CancellationTokenSource();\nvar cd = new CancellationDisposable(cts);","typeGuard":"CancellationDisposable SafeCreate(CancellationTokenSource? cts) => cts is null ? new CancellationDisposable() : new CancellationDisposable(cts);","tryCatchPattern":"try { return new CancellationDisposable(cts); }\ncatch (ArgumentNullException) { return new CancellationDisposable(); }","preventionTips":["Initialize CancellationTokenSource fields at declaration","Use the parameterless constructor when you don't own a source","Treat CTS fields as non-nullable where possible"],"tags":["rx","disposable","cancellation","null-argument"],"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"}