Subject: bug#74738: 31.0.50; Freezes in Python-mode on some Python file when searching or scrolling

From: rehan malak <[email-masked]>
Date: Sun, 8 Dec 2024 14:05:36 +0100

Hi,
I can reproduce systematically this freeze dealing with a 10000lines
Python file :

wget https://raw.githubusercontent.com/hugsy/gef/refs/heads/main/gef.py
emacs -Q gef.py

PageDown several times or scrolling with the mouse

Emacs freezes, Ctrl-g not working, CPU 100%

It works also by searching : Ctrl-s show RET
then Ctrl-s several times

With a minimal .emacs :

(set debug-on-error t)
(set debug-on-quit t)

and before scrolling

M-x profiler-start RET

then in an external terminal

pkill -SIGUSR2 emacs

I get the backtrace :

Debugger entered--entering a function:
* #f(compiled-function () #<bytecode -0x179dcd1db31182ae>)()
  syntax-ppss()
  python-syntax-context-type()
  python-nav-forward-block(-1)
  python-nav-backward-block()
  python-nav-beginning-of-block()
  python-nav-end-of-block()
  python-info-statement-ends-block-p()
  python-info-end-of-block-p()
  python-nav--forward-sexp(1 nil nil)
  python-nav-forward-sexp(1)
  forward-sexp(1)
  up-list(1)
  python--font-lock-f-strings(30419)
  font-lock-fontify-keywords-region(28914 30419 nil)
  font-lock-default-fontify-region(28914 30414 nil)
  font-lock-fontify-region(28914 30414)
  #f(compiled-function (fun) #<bytecode
-0x8a96d65e1315e71>)(font-lock-fontify-region)
  run-hook-wrapped(#f(compiled-function (fun) #<bytecode
-0x8a96d65e1315e71>) font-lock-fontify-region)
  jit-lock--run-functions(28914 30414)
  jit-lock-fontify-now(28914 30414)
  jit-lock-function(28914)

and can look at profiler report

M-x profiler-stop
M-x profiler-report

showing time spent in syntax-ppss

       17770  88% - jit-lock-function
       17770  88%  - jit-lock-fontify-now
       17770  88%   - jit-lock--run-functions
       17770  88%    - run-hook-wrapped
       17770  88%     - #<byte-code-function FA0>
       17770  88%      - font-lock-fontify-region
       17770  88%       - font-lock-default-fontify-region
       17690  87%        - font-lock-fontify-keywords-region
       17678  87%         - python--font-lock-f-strings
       17674  87%          - up-list
       17674  87%           - forward-sexp
       17674  87%            - python-nav-forward-sexp
       17674  87%             - python-nav--forward-sexp
        9253  45%              - python-info-end-of-block-p
        9245  45%               - python-info-statement-ends-block-p
        9221  45%                - python-nav-end-of-block
        8609  42%                 - python-nav-beginning-of-block
        8557  42%                  - python-nav-backward-block
        8557  42%                   - python-nav-forward-block
        8005  39%                    - python-syntax-context-type
        7997  39%                     - syntax-ppss
        7097  35%                        parse-partial-sexp
         753   3%                      + #<byte-code-function A23>
         376   1%                    + python-nav-beginning-of-statement
         132   0%                      re-search-backward
          24   0%                      looking-at
          28   0%                    current-indentation
           8   0%                  + python-nav-beginning-of-statement
           4   0%                    python-info-current-line-empty-p
           4   0%                  + python-info-current-line-comment-p
         604   2%                 + python-nav-end-of-statement
           4   0%                 + python-info-current-line-comment-p
           4   0%                   current-indentation
          24   0%                + python-nav-end-of-statement
           8   0%               + python-info-end-of-statement-p
        8178  40%              - python-info-statement-ends-block-p
        8150  40%               - python-nav-end-of-block
        7754  38%                - python-nav-beginning-of-block
        7738  38%                 - python-nav-backward-block
        7734  38%                  - python-nav-forward-block
        7178  35%                   - python-syntax-context-type
        7174  35%                    - syntax-ppss
        6978  34%                       parse-partial-sexp
           8   0%                       make-closure
           8   0%                       #<byte-code-function B1E>
           4   0%                       syntax-table
           4   0%                       syntax-ppss--update-stats
         412   2%                   + python-nav-beginning-of-statement
          92   0%                     re-search-backward
          44   0%                     looking-at
          12   0%                   current-indentation
           4   0%                   looking-at
         380   1%                + python-nav-end-of-statement
          16   0%                + python-util-forward-comment
          28   0%               + python-nav-end-of-statement
         151   0%              + python-nav-end-of-block
          28   0%              + python-info-beginning-of-block-p
          24   0%              + python-info-end-of-statement-p
          20   0%              + python-info-beginning-of-statement-p
           8   0%              + python-syntax-context-type
           8   0%              + python-info-statement-starts-block-p
           4   0%          + syntax-ppss
           8   0%         + #<byte-code-function 7E0>
           4   0%         + #<byte-code-function 740>
          80   0%        + font-lock-fontify-syntactically-region
        2279  11%   Automatic GC
          73   0% + redisplay_internal (C function)
          40   0% + command-execute
           8   0% + ...
           4   0% + mouse--click-1-maybe-follows-link

other people talking about python/profiling/syntax-ppss :

https://www.reddit.com/r/emacs/comments/z0oye9/emacs_freezes_when_opening_a_python_file/
https://www.reddit.com/r/emacs/comments/9h7onq/large_python_files_hanging_with_an_unmatched_quote/

Please tell me how I can help ?

Please tell me if there is a workaround without losing python syntax color ?

Thanks

Rehan

In GNU Emacs 31.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version
 3.24.43, cairo version 1.18.2) of 2024-12-08 built on cndl
Repository revision: b953bcb17047998c9e41cede7c5e5ffec22209b2
Repository branch: master
System Description: Debian GNU/Linux trixie/sid

Configured using:
 'configure --prefix=/home/jean/local/emacs --with-pgtk'

Configured features:
CAIRO DBUS FREETYPE GLIB GMP GNUTLS GSETTINGS HARFBUZZ JPEG LIBSELINUX
LIBXML2 MODULES NOTIFY INOTIFY PDUMPER PGTK PNG SECCOMP SOUND THREADS
TIFF TOOLKIT_SCROLL_BARS WEBP XIM GTK3 ZLIB

Important settings:
  value of $LANG: en_US.UTF-8
  value of $XMODIFIERS: @im=ibus
  locale-coding-system: utf-8-unix

Major mode: Python

Minor modes in effect:
  tooltip-mode: t
  global-eldoc-mode: t
  eldoc-mode: t
  show-paren-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  minibuffer-regexp-mode: t
  line-number-mode: t
  transient-mark-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t

Load-path shadows:
None found.

Features:
(shadow sort mail-extr emacsbug message mailcap yank-media puny dired
dired-loaddefs rfc822 mml mml-sec password-cache epa derived epg rfc6068
epg-config gnus-util text-property-search time-date mm-decode mm-bodies
mm-encode mail-parse rfc2231 mailabbrev gmm-utils mailheader sendmail
rfc2047 rfc2045 ietf-drums mm-util mail-prsvr mail-utils cl-seq python
rx project byte-opt gv bytecomp byte-compile compat pcase treesit comint
subr-x ansi-osc ring cl-loaddefs cl-lib ansi-color rmc iso-transl
tooltip cconv eldoc paren electric uniquify ediff-hook vc-hooks
lisp-float-type elisp-mode mwheel term/pgtk-win pgtk-win term/common-win
touch-screen pgtk-dnd tool-bar dnd fontset image regexp-opt fringe
tabulated-list replace newcomment text-mode lisp-mode prog-mode register
page tab-bar menu-bar rfn-eshadow isearch easymenu timer select
scroll-bar mouse jit-lock font-lock syntax font-core term/tty-colors
frame minibuffer nadvice seq simple cl-generic indonesian philippine
cham georgian utf-8-lang misc-lang vietnamese tibetan thai tai-viet lao
korean japanese eucjp-ms cp51932 hebrew greek romanian slovak czech
european ethiopic indian cyrillic chinese composite emoji-zwj charscript
charprop case-table epa-hook jka-cmpr-hook help abbrev obarray oclosure
cl-preloaded button loaddefs theme-loaddefs faces cus-face macroexp
files window text-properties overlay sha1 md5 base64 format env
code-pages mule custom widget keymap hashtable-print-readable backquote
threads dbusbind inotify dynamic-setting system-font-setting
font-render-setting cairo gtk pgtk multi-tty move-toolbar
make-network-process emacs)

Memory information:
((conses 16 60995 13527) (symbols 48 7517 0) (strings 32 19122 1493)
 (string-bytes 1 515162) (vectors 16 13554)
 (vector-slots 8 145472 8433) (floats 8 26 2) (intervals 56 436 0)
 (buffers 984 12))