Subject: bug#70423: 29.3; cc-mode hang at 100% cpu and consuming all available memory

From: Alan Mackenzie <[email-masked]>
Date: Tue, 16 Apr 2024 17:59:54 +0000

Hello.

Thanks for taking the trouble to submit this bug report.

On Tue, Apr 16, 2024 at 13:36:16 +0000, 1stmil.eth wrote:
> Among other scenarios, when clang-format runs as a hook on file save
> when I make any (even noop) edit in
> https://github.com/pillowtrucker/mir/blob/main/src/platform/graphics/drm_formats.cpp
> this file, for example, emacs enters an infinite loop consuming 100%
> of a cpu core and all memory up to gc threshold.

> It is not always the regex function on the top of the stack but it is always a call around those functions
> "beginning-of-defun-raw"
> "beginning-of-defun"
> "c-get-fallback-scan-pos"
> (gdb) bt full

Yes.  c-parse-state is known for sometimes looping - it is an awkward
spot where doing something rigorusly is likely to be (far) too slow, but
the ad hoc speed ups occasionally go wrong.  That seems to be the
problem here.

[ The gdb backtrace (snipped) unfortunately isn't very helpful, here. ]

> Inside of emacs I get something like this:

> Debugger entered--Lisp error: (quit)  re-search-backward("^\\s(\\|\\(?:^[ \11]*\\(template\\s-*<[^>;.{}]+>\\s-*\\)?\\(\\(\\(auto\\|const\\|explicit\\|extern\\s-+\"[^\"]+\"\\|extern\\|friend\\|inline\\|mutable\\|overload\\|register\\|static\\|typedef\\|virtual\\)\\s-+\\)*\\(\\([[<a-zA-Z][]_a-zA-Z0-9]*\\(::[]_a-zA-Z0-9]+\\)?\\s-*<[_<>a-zA-Z0-9 ,]+>\\s-*[*&]*\\|[[<a-zA-Z][]_<>a-zA-Z0-9]*\\(::[[<a-zA-Z][]_<>a-zA-Z0-9]+\\)?\\s-*[*&]*\\)[*& \11\n\15]+\\)\\)?\\(\\(::\\|[[<a-zA-Z][]_a-zA-Z0-9]*\\s-*<[^>;{}]+>\\s-*[*&]*::\\|[[<a-zA-Z][]_~<>a-zA-Z0-9]*\\s-*[*&]*::\\)\\s-*\\)?\\(operator\\s-*[^ \11\n\15:;.,?~{}]+\\(\\s-*\\[\\]\\)?\\|[_~<a-zA-Z][^][ \11:;.,~{}()\177]*\\|[*&]?\\([_~<a-zA-Z][_a-zA-Z0-9]*\\s-*<[^>;{}]+[ \11\n\15>]*>\\|[_~<a-zA-Z][_~<>a-zA-Z0-9]*\\)\\)\\s-*\\(([^{;]*)\\(\\(\\s-+const\\|\\s-+mutable\\)?\\(\\s-*[=:][^;{]+\\)?\\)?\\)\\s-*\\)\\s(" nil move 1)
>   beginning-of-defun-raw(nil)
>   beginning-of-defun()
>   c-get-fallback-scan-pos(18875)
>   c-parse-state-get-strategy(18875 13474)
>   c-parse-state-1()
>   c-parse-state()
>   c-laomib-invalidate-cache(18875 18880)
>   c-before-change(18875 18880)

The functions thus far suggest that your file is somewhat unusual; it
has, perhaps, long stretches of text without braces, or long brace
blocks.

Could you possibly send me a copy of the above C++ file, or point me to
an open website (not github) where I could download a copy?  I have no
access to github, since I am not prepared to let probably hostile
scripts controlled by Microsoft run on my machine.

>   lsp--apply-text-edit-replace-buffer-contents(#<hash-table equal 2/2 0x120ad92d>)
>   #f(compiled-function (edit) #<bytecode -0x9b8e84b9515e2ea>)(#<hash-table equal 2/2 0x120ad92d>)
>   lsp--apply-text-edits((#<hash-table equal 2/2 0x12062b03>) format)
>   lsp-format-buffer()
>   run-hooks(before-save-hook)
>   basic-save-buffer(t)
>   save-buffer(1)
>   funcall-interactively(save-buffer 1)
>   #<subr call-interactively>(save-buffer nil nil)
>   call-interactively@ido-cr+-record-current-command(#<subr call-interactively> save-buffer nil nil)
>   apply(call-interactively@ido-cr+-record-current-command #<subr call-interactively> (save-buffer nil nil))
>   call-interactively(save-buffer nil nil)
>   command-execute(save-buffer)

> This is not the only scenario when this happens. I can also sometimes
> trigger the same thing by trying to jump to the definition of a symbol
> from a .cpp/c++-header file into a system header (all of which are on
> a read-only filesystem on my computer), for example one of the SDL
> headers. The main difference is that the buffer-switching gets mixed
> into the lot and emacs doesn't even respond to C-g and I have to kill
> -SIGUSR2 it to get it to start responding again, but I'm pretty sure
> it's the same bug - output from this scenario is here:
> https://0x0.st/X-RT.txt

What would be most helpful here would be a reliable recipe to reproduce
the bug without needing other libraries such as lsp, preferably starting
from emacs -Q.

> I'm sorry about the <optimized out> everywhere and lack of inlined
> sources, but I've already rebuilt it with nixos's enableDebugging
> flag, which should have enabled -Og and -g3 (I think it did do the
> latter but failed on the optimizations and left it at O2) and I've
> already spent most of my day debugging this and it would take another
> full day to figure out how to properly override the byzantine nixpkgs
> emacs infrastructure with the right optimization flag and to store the
> right source tree.

Yes, bugs are like that sometimes.  I'm afraid I don't know anything
about nixos.

> In GNU Emacs 29.3 (build 1, x86_64-pc-linux-gnu, X toolkit, cairo
> version 1.18.0, Xaw3d scroll bars)
> Windowing system distributor 'The X.Org Foundation', version 11.0.12101012
> System Description: NixOS 24.05 (Uakari)

[ .... ]

-- 
Alan Mackenzie (Nuremberg, Germany).