{"record":{"id":"6732fb7ab97ef62d","repo":"louthy/language-ext","slug":"operationcanceledexception-iterable-add","errorCode":null,"errorMessage":"OperationCanceledException","messagePattern":"OperationCanceledException","errorType":"exception","errorClass":"OperationCanceledException","httpStatus":null,"severity":"warning","filePath":"LanguageExt.Core/Immutable Collections/Iterable/DSL/Iterable.Add.cs","lineNumber":29,"sourceCode":"        Source.IsAsync;\n    \n    public override IO<int> CountIO() =>\n        Source.CountIO().Map(c => Prefix.Count + c + Postfix.Count);\n\n    public override Iterable<A> Add(A item) =>\n        new IterableAdd<A>(Prefix, Source, (SeqStrict<A>)Postfix.Add(item));\n\n    public override Iterable<A> Cons(A item) =>\n        new IterableAdd<A>((SeqStrict<A>)Prefix.Cons(item), Source, Postfix);\n\n    public override IO<IEnumerable<A>> AsEnumerableIO()\n    {\n        return IO.lift(go);\n        IEnumerable<A> go(EnvIO env)\n        {\n            foreach (var x in Prefix)\n            {\n                if (env.Token.IsCancellationRequested) throw new OperationCanceledException();\n                yield return x;\n            }\n\n            foreach (var x in Source.AsEnumerable(env.Token))\n            {\n                if(env.Token.IsCancellationRequested) throw new OperationCanceledException();\n                yield return x;\n            }\n\n            foreach (var x in Postfix)\n            {\n                if (env.Token.IsCancellationRequested) throw new OperationCanceledException();\n                yield return x;\n            }\n        }\n    }\n\n    public override IO<IAsyncEnumerable<A>> AsAsyncEnumerableIO()","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/louthy/language-ext/blob/2f0e3628242889774d4141960a35671a0280051f/LanguageExt.Core/Immutable Collections/Iterable/DSL/Iterable.Add.cs#L11-L47","documentation":"Iterable.AsEnumerableIO returns an IO-wrapped IEnumerable whose iterator checks env.Token before yielding each Prefix element; when cancellation is requested it throws OperationCanceledException. This is cooperative cancellation: the enumeration aborts as soon as the token from EnvIO is signaled, so the consumer must be prepared for the sequence to terminate via this exception rather than completing.","triggerScenarios":"Enumerating the IEnumerable returned by iterable.AsEnumerableIO().Run(...) after (or while) the EnvIO CancellationToken is cancelled; consuming the sequence lazily after a timeout or shutdown signal fires.","commonSituations":"Host cancellation/shutdown mid-enumeration, ASP.NET request abort tokens flowing into EnvIO, or a CancellationTokenSource cancelled because a timeout elapsed while the consumer was still pulling items.","solutions":["Catch OperationCanceledException around enumeration and treat it as expected cancellation, not a failure.","Check env.Token.IsCancellationRequested before/while consuming and stop gracefully.","Use IO.cancel or run the whole IO under the caller's token semantics instead of mixing tokens.","If cancellation is not desired, run with a CancellationToken.None-based EnvIO."],"exampleFix":"// before\nforeach (var x in iterable.AsEnumerableIO().Run(env)) Process(x); // throws on cancel\n\n// after\ntry\n{\n    foreach (var x in iterable.AsEnumerableIO().Run(env)) Process(x);\n}\ncatch (OperationCanceledException) when (env.Token.IsCancellationRequested)\n{\n    // graceful cancellation\n}","handlingStrategy":"try-catch","validationCode":"if (env.Token.IsCancellationRequested) return; // stop before enumerating","typeGuard":"static bool CanEnumerate(EnvIO env) => !env.Token.IsCancellationRequested;","tryCatchPattern":"try { foreach (var x in seq) { ... } }\ncatch (OperationCanceledException) when (env.Token.IsCancellationRequested) { /* expected cancellation */ }","preventionTips":["Check the token before and during enumeration.","Always filter OperationCanceledException with a when(token.IsCancellationRequested) clause.","Treat partial consumption as the expected contract for cancellable enumerables.","Match the EnvIO token to the desired operation lifetime."],"tags":["cancellation","ienumerable","cooperative-cancellation"],"backgroundTag":"operation-canceled","analyzedSha":"2f0e3628242889774d4141960a35671a0280051f","analyzedAt":"2026-09-15T03:31:55.716Z","contentChangedAt":"2026-09-15T03:31:55.716Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}