Hello,

`org-element-cache-map' with :granularity element (with or without
:restrict-elements) never returns when the buffer is narrowed to a
subtree and a heading with contents follows the narrowed region.
Emacs stays at 100% CPU until C-g.

I hit it through org-latex-preview: in a buffer narrowed with
`org-narrow-to-subtree', editing a LaTeX environment makes
`org-latex-preview-mode--regenerate-overlay' call
`org-latex-preview--get-numbered-environments', which calls
`org-element-cache-map' with :restrict-elements (latex-environment), and
Emacs freezes.  Backtrace after SIGUSR2:

  org-element--parse-to(7726)
  org-element-cache-map(#f(...) :granularity element
:restrict-elements (latex-environment) :from-pos 7463 :to-pos #<marker
at 7725 in notes.org>)
  org-latex-preview--get-numbered-environments(7463 nil)
  org-latex-preview-mode--regenerate-overlay(#<overlay from 7322 to
7463 in notes.org>)
  timer-event-handler(...)

The bug itself is in org-element and does not need LaTeX.

* Minimal reproduction (emacs -Q)

  (with-temp-buffer
    (insert "* A\na\n* B\nb\n* C\nc\n")
    (org-mode)
    (goto-char (point-min))
    (search-forward "* B")
    (org-narrow-to-subtree)
    (org-element-cache-map #'identity :granularity 'element))  ; never returns

All three sections need contents: with an empty "* A" or "* C" the call
returns.

The attached org-element-cache-map-narrow-repro.el runs the same buffer
in four cases.  It records the positions passed to
`org-element--parse-to' and signals an error after 2000 calls instead of
hanging:

  emacs -Q --batch -l org-element-cache-map-narrow-repro.el

Output (Org's own cache warning on stderr omitted):

  Org 10.0-pre, Emacs 32.0.50

  widened, :granularity element        ((org-data 1) (headline 1)
(section 5) (paragraph 5) (headline 7) (section 11) (paragraph 11)
(headline 13) (section 17) (paragraph 17))
  narrowed, default granularity        ((headline 1) (headline 7))
  narrowed, :granularity element       Infinite loop:
org-element--parse-to called 2002 times
                                        first parse-to positions: (10
12 1 1 5 5 12 19 5 7 11 11 13 13 19 13 19 13 19 13 19 13 19 13 19 13
19 13 19 13)
  narrowed, :restrict-elements         Infinite loop:
org-element--parse-to called 2002 times
                                        first parse-to positions: (10
12 1 1 5 5 12 19 5 7 11 11 13 13 19 13 19 13 19 13 19 13 19 13 19 13
19 13 19 13)

The output is the same with Org 9.8.12 (built into Emacs 32.0.50) and
Org 9.8.7 (built into Emacs 31.1).

* Analysis

The narrowed region is 7-12 ("* B\nb"), so TO-POS is 12.  Position 13 is
the start of "* C" and 19 is the end of the widened buffer.

After the elements of B are processed, START moves to 13, past TO-POS.
The traversal then finds a cached element after START and takes the
"DATA is after START.  Fill the gap." branch: it calls
(org-element--parse-to 13) and then `cache-walk-restart', which calls
(org-element-at-point (point-max)) on the widened buffer, i.e. 19.  The
gap at 13 is never filled in a way that lets the walk move on, so the
calls alternate 13, 19, 13, 19, ... forever.  Nothing in this branch
checks START against TO-POS.

* Suggested fix

Abort the walk before filling the gap when START is already at or past
TO-POS (patch attached, org-element-cache-map-narrow.patch, against
10.0pre0.20261108.969 from GNU-devel ELPA):

                        ;; DATA is after START.  Fill the gap.
  +                     ;; START may already be past TO-POS, e.g. at
  +                     ;; the next heading after a narrowed region.
  +                     (when (>= start to-pos) (cache-walk-abort))

With the patch the reproduction gives:

  narrowed, :granularity element       ((org-data 1) (headline 1)
(section 5) (paragraph 5) (headline 7) (section 11) (paragraph 11))
  narrowed, :restrict-elements         nil

These are the same elements as in the widened case, cut at TO-POS.  The
org-latex-preview freeze is also gone with the patch.  I have not run
the Org test suite, and I am not sure this is the right place for the
check rather than in how START is advanced, so please treat the patch as
a starting point.

Workaround for users: (setq org-latex-preview-numbered nil) avoids the
call from org-latex-preview.

* Versions

Org mode version 10.0-pre (10.0pre0.20261108.969 from GNU-devel ELPA)
Also reproduced with Org 9.8.12 (built-in, Emacs 32.0.50) and
Org 9.8.7 (built-in, Emacs 31.1).
GNU Emacs 32.0.50 (x86_64-pc-linux-gnu, GTK+ 3.24.52, cairo 1.18.6)

Thanks!
--- a/lisp/org-element.el
+++ b/lisp/org-element.el
@@ -8466,6 +8466,9 @@
 		                (setq prev data)
                               (setq prev nil)))
                         ;; DATA is after START.  Fill the gap.
+                        ;; START may already be past TO-POS, e.g. at
+                        ;; the next heading after a narrowed region.
+                        (when (>= start to-pos) (cache-walk-abort))
                         (if (org-element-type-p
                              (org-element--parse-to start)
                              '(plain-list table))
;;; org-element-cache-map-narrow-repro.el --- Reproduce: -*- lexical-binding: t; -*-
;;; Reproduce: `org-element-cache-map' with :granularity element never
;;; returns in a buffer narrowed to a subtree that has a following
;;; heading with contents.
;;
;; Run with:
;;   emacs -Q --batch -l org-element-cache-map-narrow-repro.el
;;
;; Without the guard below the call simply hangs at 100% CPU; the guard
;; turns the infinite loop into an error and prints the positions passed
;; to `org-element--parse-to'.

(require 'org)

(defvar repro-calls nil "Positions passed to `org-element--parse-to'.")

(defun repro-guard (pos &rest _)
  "Record POS and signal an error after too many calls."
  (push (if (markerp pos) (marker-position pos) pos) repro-calls)
  (when (> (length repro-calls) 2000)
    (error "Infinite loop: org-element--parse-to called %d times"
           (length repro-calls))))

(advice-add 'org-element--parse-to :before #'repro-guard)

(defun repro-run (label narrow args)
  "Map the element cache with ARGS and print the result under LABEL.
When NARROW is non-nil, narrow to the subtree of heading B first."
  (setq repro-calls nil)
  (with-temp-buffer
    (insert "* A\na\n* B\nb\n* C\nc\n")
    (org-mode)
    (when narrow
      (goto-char (point-min))
      (search-forward "* B")
      (org-narrow-to-subtree))
    (princ
     (format "%-36s %s\n" label
             (condition-case err
                 (format "%S"
                         (mapcar (lambda (e)
                                   (list (org-element-type e)
                                         (org-element-begin e)))
                                 (apply #'org-element-cache-map
                                        #'identity args)))
               (error
                (format "%s\n%38sfirst parse-to positions: %S"
                        (error-message-string err) ""
                        (seq-take (reverse repro-calls) 30))))))))

(princ (format "Org %s, Emacs %s\n\n" (org-version) emacs-version))
(repro-run "widened, :granularity element" nil '(:granularity element))
(repro-run "narrowed, default granularity" t '())
(repro-run "narrowed, :granularity element" t '(:granularity element))
(repro-run "narrowed, :restrict-elements" t
           '(:granularity element :restrict-elements (latex-environment)))

Reply via email to