Subject: bug#12326: (24.2.50; js2-mode freezes on current trunk)

From: Dmitry Gutov <[email-masked]>
Date: Sun, 02 Sep 2012 05:41:58 +0400

Some further analysis showed that the freeze is caused by the infinite 
mutual loop between `js2-do-parse' and `js2-mode-reset-timer'.
Looks like it goes like this:

1. I enter some input.
2. js2-mode-edit is called; it calls js2-mode-reset-timer
3. js2-mode-reset-timer schedules js2-mode-idle-reparse to run
4. js2-mode-idle-reparse calls js2-do-parse
5. If I manage to press a button while js2-do-parse is still running, 
and it reaches an (input-pending-p), it stops parsing and calls 
js2-mode-reset-timer.
6. Apparently, at this point the timer code still thinks we're idle, 
because it immediately calls js2-mode-idle-reparse. So goto 4. While 
(input-pending-p) still returns true.

If I manually inline `run-with-idle-timer' into `js2-mode-reset-timer' 
and change the DONT-WAIT `timer-activate-when-idle' argument to nil, it 
fixes the problem. Like this:

(defun js2-mode-reset-timer ()
   "Cancel any existing parse timer and schedule a new one."
   (if js2-mode-parse-timer
       (cancel-timer js2-mode-parse-timer))
   (setq js2-mode-parsing nil)
   (let ((timer (timer-create)))
     (setq js2-mode-parse-timer timer)
     (timer-set-function timer 'js2-mode-idle-reparse (list 
(current-buffer)))
     (timer-set-idle-time timer js2-idle-timer-delay)
     (timer-activate-when-idle timer)))

Is this a problem in Emacs or consumer code?