{"record":{"id":"9a40af117163938f","repo":"dotnet/reactive","slug":"advanceto-cannot-be-called-when-the-scheduler-is-already","errorCode":null,"errorMessage":"AdvanceTo cannot be called when the scheduler is already running. Try using Sleep instead.","messagePattern":"AdvanceTo 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":241,"sourceCode":"                    {\n                        if (Comparer.Compare(next.DueTime, Clock) > 0)\n                        {\n                            Clock = next.DueTime;\n                        }\n\n                        next.Invoke();\n                    }\n                    else\n                    {\n                        IsEnabled = false;\n                    }\n                } while (IsEnabled);\n\n                Clock = time;\n            }\n            else\n            {\n                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Strings_Linq.CANT_ADVANCE_WHILE_RUNNING, nameof(AdvanceTo)));\n            }\n        }\n\n        /// <summary>\n        /// Advances the scheduler's clock by the specified relative time, running all work scheduled for that timespan.\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        /// <exception cref=\"InvalidOperationException\">The scheduler is already running. VirtualTimeScheduler doesn't support running nested work dispatch loops. To simulate time slippage while running work on the scheduler, use <see cref=\"Sleep\"/>.</exception>\n        public void AdvanceBy(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            }","sourceCodeStart":223,"sourceCodeEnd":259,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Concurrency/VirtualTimeScheduler.cs#L223-L259","documentation":"AdvanceTo runs a dispatch loop and therefore requires the scheduler not to be running. If the scheduler is already enabled (IsEnabled == true, e.g. the call is made inside a scheduled action or from within a prior Start/AdvanceTo loop), a nested dispatch loop would occur, so an InvalidOperationException is thrown with this message.","triggerScenarios":"Calling AdvanceTo from inside work executed by the same virtual scheduler (a scheduled action re-advancing time), or calling AdvanceTo/Start re-entrantly while a dispatch loop is active.","commonSituations":"Test actions that schedule more work and try to advance the clock from within the callback; event handlers triggered by scheduled work that call back into the test harness; Rx operators that schedule follow-up work and user code hooks into it.","solutions":["Use scheduler.Sleep(relativeTime) to slip time while the scheduler is running, as the message suggests.","Move the AdvanceTo call outside the scheduled work — schedule a new action or advance after the current loop finishes.","Restructure test code so clock advancement happens only from the outer test thread when IsEnabled is false."],"exampleFix":"// before\nscheduler.Schedule(() => scheduler.AdvanceTo(targetTime)); // inside running scheduler\n// after\nscheduler.Schedule(() => scheduler.Sleep(TimeSpan.FromMilliseconds(50)));","handlingStrategy":"validation","validationCode":"if (scheduler.IsEnabled)\n    scheduler.Sleep(relativeSlip);\nelse\n    scheduler.AdvanceTo(time);","typeGuard":"bool CanAdvance<TAbsolute, TRelative>(VirtualTimeSchedulerBase<TAbsolute, TRelative> s) => !s.IsEnabled;","tryCatchPattern":"try { scheduler.AdvanceTo(time); } catch (InvalidOperationException ex) when (ex.Message.Contains(nameof(VirtualTimeSchedulerBase<TAbsolute, TRelative>.AdvanceTo))) { scheduler.Sleep(remainingSlip); }","preventionTips":["Never call AdvanceTo/Start from inside code the scheduler itself invokes; use Sleep there.","Keep clock advancement on the outer test thread and check IsEnabled first.","For time slippage during running work, prefer Sleep(relativeTime)."],"tags":["rx","invalid-operation","virtual-time","reentrancy"],"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"}