{"record":{"id":"49f2a24d9c8ef010","repo":"dotnet/reactive","slug":"advanceby-cannot-be-called-when-the-scheduler-is-already","errorCode":null,"errorMessage":"AdvanceBy cannot be called when the scheduler is already running. Try using Sleep instead.","messagePattern":"AdvanceBy cannot be called when the scheduler is already running\\. Try using Sleep instead\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Concurrency/VirtualTimeScheduler.cs","lineNumber":272,"sourceCode":"\n            var dueToClock = Comparer.Compare(dt, Clock);\n            if (dueToClock < 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(time));\n            }\n\n            if (dueToClock == 0)\n            {\n                return;\n            }\n\n            if (!IsEnabled)\n            {\n                AdvanceTo(dt);\n            }\n            else\n            {\n                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Strings_Linq.CANT_ADVANCE_WHILE_RUNNING, nameof(AdvanceBy)));\n            }\n        }\n\n        /// <summary>\n        /// Advances the scheduler's clock by the specified relative time.\n        /// </summary>\n        /// <param name=\"time\">Relative time to advance the scheduler's clock by.</param>\n        /// <exception cref=\"ArgumentOutOfRangeException\"><paramref name=\"time\"/> is negative.</exception>\n        public void Sleep(TRelative time)\n        {\n            var dt = Add(Clock, time);\n\n            var dueToClock = Comparer.Compare(dt, Clock);\n            if (dueToClock < 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(time));\n            }\n","sourceCodeStart":254,"sourceCodeEnd":290,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Concurrency/VirtualTimeScheduler.cs#L254-L290","documentation":"VirtualTimeScheduler's AdvanceBy (and AdvanceTo) can only be called while the scheduler clock is not running. When the scheduler is enabled (e.g. inside a test that has started the scheduler or during Start/Stop), advancing the clock directly would corrupt the internal ordering, so System.Reactive throws InvalidOperationException and points you at Sleep as the valid alternative inside a running scheduler.","triggerScenarios":"Calling scheduler.AdvanceBy(ts) or scheduler.AdvanceTo(t) when IsEnabled is true — typically advancing the clock from inside a scheduled action, or after Start()/Stop() left the scheduler running.","commonSituations":"Rx TestScheduler-based unit tests where a callback scheduled on the virtual clock tries to advance time; nested test steps that re-enter the scheduler while it is executing queued work.","solutions":["Move the AdvanceBy call outside the running scheduler (after Start()/Stop() completes, or when IsEnabled is false)","If you need a delay inside running scheduled work, schedule a relative item via scheduler.Schedule(dueTime, action) instead of advancing the clock","Catch InvalidOperationException only if the running state is expected, and fall back to Sleep semantics or defer the advance"],"exampleFix":"// before\nscheduler.Schedule(TimeSpan.FromTicks(10), () =>\n{\n    scheduler.AdvanceBy(TimeSpan.FromTicks(5)); // throws when running\n});\n// after\nscheduler.Schedule(TimeSpan.FromTicks(10), () =>\n{\n    // do work; use relative scheduling for delays instead\n    scheduler.Schedule(TimeSpan.FromTicks(5), () => { /* later work */ });\n});","handlingStrategy":"validation","validationCode":"if (scheduler.IsEnabled) throw new InvalidOperationException(\"Use Sleep/relative scheduling instead of AdvanceBy while running\");\nscheduler.AdvanceBy(ts);","typeGuard":"bool CanAdvance(VirtualTimeSchedulerBase<long, long> s) => !s.IsEnabled;","tryCatchPattern":null,"preventionTips":["Only advance the virtual clock from test orchestration code, never from inside scheduled callbacks","Check IsEnabled before AdvanceTo/AdvanceBy","Inside running work, use Schedule(dueTime, action) for delays"],"tags":["rx","scheduler","virtual-time","invalid-operation"],"backgroundTag":"invalid-state-transition","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"}