Scheduler.flush throws unclear error when actions is empty (cannot read property 'execute' of undefined)
See original GitHub issueBug Report
Current Behavior Calling .flush() on a scheduler (e.g. animationFrameScheduler) with no pending actions causes an unclear exception to be thrown:
TypeError: Cannot read property 'execute' of undefined
at AnimationFrameScheduler.flush (https://rxjs-pc1ga8.stackblitz.io/turbo_modules/rxjs@6.4.0/internal/scheduler/AnimationFrameScheduler.js:31:32)
at Object.eval (https://rxjs-pc1ga8.stackblitz.io/~/index.ts:12:36)
at eval (https://rxjs-pc1ga8.stackblitz.io/~/index.ts:19:4)
at eval (https://rxjs-pc1ga8.stackblitz.io/~/index.ts:20:3)
at eval (<anonymous>)
at Qt (https://c.staticblitz.com/d/webcontainer.e1e926a8972546110c3.js:15:30145)
at https://c.staticblitz.com/d/webcontainer.e1e926a8972546110c3.js:15:38799
at U (https://c.staticblitz.com/d/webcontainer.e1e926a8972546110c3.js:15:13565)
at https://c.staticblitz.com/d/webcontainer.e1e926a8972546110c3.js:15:13207
at boot (https://rxjs-pc1ga8.stackblitz.io/dev/boot:6:3)
Reproduction
- REPL or Repo link: https://stackblitz.com/edit/rxjs-pc1ga8?devtoolsheight=60
import {animationFrameScheduler} from 'rxjs';
animationFrameScheduler.flush();
Expected behavior Either one of:
- not throwing when there are no pending actions
- throwing a clearer error (e.g. ‘Attempting to flush an empty queue’)
Environment
- Runtime: Chrome v73
- RxJS version: 6.4.x
Possible Solution
See expected behavior. This appears to affect all schedulers that have this do while loop without verifying that a valid action has been shift’d off of this.actions
This may be working as intended (and no change desired for performance reasons) in which case just having this issue open and google-able would provide additional documentation
Additional context/Screenshots
#2697 Appears to have been the same issue given their stacktrace: https://github.com/ReactiveX/rxjs/blob/5.4.1/src/scheduler/AsapScheduler.ts#L17
A clearer error (or no error at all) would probably have pointed them in the right direction.
Happy to submit a fix myself if the desired behavior is defined.
Issue Analytics
- State:
- Created 4 years ago
- Comments:9

Top Related StackOverflow Question
Still working on steps to repro, but FWIW we have a significant volume of Bugsnag reports of this bug in production despite not calling
flush()explicitly anywhere:Edit: this is with rxjs@6.5.1, and not isolated to any browser.
My concern is that the issue is not reproducible and that there are historical issues - AFAICT - in some browsers with
cancelAnimationFramenot working. Without a repro, there is nothing that can be done, IMO.Regarding testing, a polyfill is used in this project to allow the scheduler to be tested in Node. Such an approach should work fine with fake time without Sinon.