alphapapa / prism.el

Disperse Lisp forms (and other languages) into a spectrum of colors by depth

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Emacs loops/segfaults with certain buffer contents in certain major modes

nathanvy opened this issue · comments

Hi all,

edit: Original title was "Prism mode causes emacs to freeze/lock up when typing double-quote characters on ARM macbooks"

Title says it all. I am not sure how to debug this further, because emacs hangs entirely and the only way to recover is to force-quit the application. I suppose one could use gdb or another debugger but I am not an expert in that sort of debugging and wouldn't know where to start.

Steps to reproduce:

  1. start emacs with blank config on M2 Macbook pro
  2. Install Prism from MELPA (version "20230416.626" is what package.el pulled down for me)
(use-package prism
    :commands prism-mode
    :init
    (add-hook 'prog-mode-hook #'prism-mode))
  1. Open a .lisp file and attempt to type the following: (#P"") which is a Common Lisp pathname literal. I have also seen this when editing HTML and attempting to type <html lang="en">

Hi Nathan,

Thanks for reporting this. I can confirm that there is a problem. It's quite strange, and I'm surprised it took this long to be found. Yet, it seems to only happen under certain circumstances, ones which I haven't encountered in my own usage, despite using prism every day.

Here's what I did to reproduce:

  1. Using https://github.com/alphapapa/with-emacs.sh: Run with-emacs.sh -e emacs-28.2 -i prism.
  2. C-x C-f /tmp/test.lisp RET
  3. M-x prism-mode RET
  4. Type (#P"").

After the first quotation mark (actually, after I press the key, but before it is displayed) Emacs goes into a 100%-CPU usage loop. If I press C-g, Emacs will show the quotation mark and go into the loop again. If I keep pressing C-g, I can get Emacs to respond to another keypress each time. But as long as prism-mode is active in the buffer, Emacs will go into the loop trying to fontify that text. (Another way to reproduce is to write that string into the buffer and then activate prism-mode.)

If, while Emacs is looping, I send the SIGUSR2 signal, which should cause Emacs to quit and show a backtrace, instead Emacs segfaults:

Fatal error 11: Segmentation fault
Backtrace:
/home/me/.guix-profile/bin/emacs-28.2[0x529b7e]
/home/me/.guix-profile/bin/emacs-28.2[0x4257ca]
/home/me/.guix-profile/bin/emacs-28.2[0x425c62]
/home/me/.guix-profile/bin/emacs-28.2[0x528208]
/home/me/.guix-profile/bin/emacs-28.2[0x528279]
/gnu/store/5h2w4qi9hk1qzzgi1w83220ydslinr4s-glibc-2.33/lib/libpthread.so.0(+0x11d80)[0x7fac20ab1d80]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6f9c]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
/home/me/.guix-profile/bin/emacs-28.2[0x4d6fc3]
...
/home/me/.bin/with-emacs: line 215: 130799 Segmentation fault      "$emacs" "${emacs_args[@]}"

I can confirm that the same thing happens in html-mode with the sample text you provided. Note that prism is not intended for use with HTML (see #16). But, of course, Emacs should not freeze or crash with it activated. However, prism-whitespace-mode does not cause a problem with that HTML fragment.

And, of course, typing the (#P"") text in emacs-lisp-mode (which is what I mostly use prism-mode for) causes no problems.

I tried using font-lock-studio to reveal the problem, but Emacs froze and crashed with it also.

So there may be a bug in Emacs here (because AFAIK Elisp should never be able to cause Emacs to segfault), but maybe also a bug in prism that's triggering it. I'm guessing it's related to the syntax tables used by the major modes, since it happens in lisp-mode but not in emacs-lisp-mode.

And, of course, I'm not on an M2 Mac, so that appears to be unrelated to the problem.

For reference, what version of Emacs are you using, and where did the build come from?

As you said, I suppose one would need to use gdb to debug this. I'm also not an expert with it, but I do recall that, if one makes an Emacs bug report, the developers will advise the reporter how to use gdb to get the necessary information. Would you be willing to pursue that? If not, I'll try to get to it eventually, but since I don't experience this problem myself, I probably won't have time to dig deeper anytime soon.

Hi Nathan,

Thanks for reporting this. I can confirm that there is a problem. It's quite strange, and I'm surprised it took this long to be found. Yet, it seems to only happen under certain circumstances, ones which I haven't encountered in my own usage, despite using prism every day.

You're most welcome, and thank you in return for Prism.

I can confirm that the same thing happens in html-mode with the sample text you provided. Note that prism is not intended for use with HTML (see #16). But, of course, Emacs should not freeze or crash with it activated. However, prism-whitespace-mode does not cause a problem with that HTML fragment.

Thanks, I wasn't aware of prism-whitespace-mode!

For reference, what version of Emacs are you using, and where did the build come from?

I've reproduced this on the emacs-mac fork which is currently on 28.2, as well as the 28.2 build from emacsformacosx.com which purports to be a straight port with minimal/no changes.

As you said, I suppose one would need to use gdb to debug this. I'm also not an expert with it, but I do recall that, if one makes an Emacs bug report, the developers will advise the reporter how to use gdb to get the necessary information. Would you be willing to pursue that? If not, I'll try to get to it eventually, but since I don't experience this problem myself, I probably won't have time to dig deeper anytime soon.

Sure, I'll pursue that this week and report back here with any findings.

Thank you!

Hi Nathan,

I hope I have fixed it. Please try this commit: ba53105 It seems to work for me with the following test code in Elisp mode, and your test code in Lisp mode, and the HTML snippet:

(P"")
(P "")
(P P)
(P"foo bar")
(P "foo bar")
(P "foo")
(P """""")

Please test it and let me know if it works for you. (Obviously, be sure to save any work in progress first, or use a separate Emacs session, in case it does still cause Emacs to crash.)

Thanks.

Thanks for this. I'll take a look as soon as able, life's been very hectic lately so I haven't yet been able to put together an emacs bug report to upstream.

Nevermind, I found that that change causes another bug. I'll continue working on it.

Ok, this commit seems to fix the problems noted here without causing any new problems: c2d52ed Please test it carefully and let me know what you find. Thanks.

It seems to be working well, so I merged it to master and released v0.3 with this fix. @nathanvy Please do let me know how it works for you. If there are any more problems, I'll release a fix in v0.3.1.

Thanks again. I noticed in your commit you mentioned prism isn't intended for HTML. Should I be using prism-whitespace-mode instead for that?

You can try it, but it's not intended for HTML either. See #16

Okay thanks. I basically have been using prism for literally anything, haha.

I'm glad whereever it works. :) As I described in #16, the Emacs functions for SGML-type languages aren't quite sufficient to do what prism is intended to do, but Emacs 29 will probably make that easier.

@nathanvy BTW, did you confirm that this issue is fixed by v0.3?

I've just tested now, while Peppa Pig was parenting my kids.

Edit: Just so we're on the same page, I upgraded to latest using straight-pull-all rather than pull down what's on MELPA. I navigate into ~/.emacs.d/straight/repos/prism.el, then git log --format="%H" -n 1 returns the commit 8d4b7726632eac8850b2509239ad0283a80afc93 which appears to be correct.

In any case, this no longer freezes emacs:

(#P"")

But this still does, immediately upon typing the doublequote.

<html lang="

I'm willing to just cease using Prism for HTML and friends, as a long-term workaround, as it seems the lisp side of things is resolved.

@nathanvy Thanks for finding that. I seem to have fixed it here: b1513e1 Please let me know if that helps. (It's the tip of the wip/18 branch, which you can install with Straight.)

Thanks I'll test it this evening

@nathanvy Thanks for finding that. I seem to have fixed it here: b1513e1 Please let me know if that helps. (It's the tip of the wip/18 branch, which you can install with Straight.)

Sorry for the delay; that change resolves the issue on my end, no longer able to reproduce!

Hello I've also experienced this issue, I tried using the wip/18 branch but it still hangs here. I've was able to narrow it down to this minimum example to reproduce.

// I'm a comment
foo = bar

The issue seems to be apostrophes in comments, I've disabled prism-comments for now as a workaround. Just FYI to anyone else stumbling across this

commented
[67181.425287] emacs[155037]: segfault at 560f914e5840 ip 00007f8abd68121a sp 00007ffdac05a7f0 error 7 in emacs-zmq.so[7f8abd600000+1a8000] likely on CPU 1 (core 0, socket 0)
[67181.425306] Code: 48 8b 03 48 8d 35 53 dd 0d 00 48 8b 68 10 48 89 c7 ff 50 58 48 8b 3b 48 89 c6 ff d5 48 8b 15 dd 2a 33 00 48 8d 35 db 53 0d 00 <48> 89 02 48 8b 03 48 8b 68 10 48 89 c7 ff 50 58 48 8b 3b 48 89 c6

just wanted to note that I also experienced this issue and can confirm that in my case
a) there were apostrophes in comments
b) turning off prism-comments works

@JasZhe Thanks for reporting that.

@fkr-0 Thanks. It's puzzling why Emacs is segfaulting, but when this bug is fixed, that shouldn't happen.

@JasZhe @fkr-0 I just pushed another commit to the WIP branch:

https://github.com/alphapapa/prism.el/tree/wip/18

It seems to fix that problem. Please test it and let me know if it does for you. Thanks.

I went ahead and pushed v0.3.1 that seems to fix this problem. Please let me know your experience. Thanks.

I was having this same issue this morning when trying to enable prism-mode in a buffer containing my Emacs init.el and this update has fixed it, thanks!

@polaris64 Thanks for letting me know.

Just updated as well.
It stopped hanging 👍.

Updated to the latest master, re-enabled comments, it no longer freezes emacs, but I do get the infinite loop error message:

Error during redisplay: (jit-lock-function 1) signaled (error "prism: Infinite loop detected in ‘prism-match’ (buffer:#<buffer test_prism.go> point:5).  Please report this bug")

using the same example from before in test_prism.go:

// I'm a comment
foo = bar

Thank you for the quick turnaround on this!

@JasZhe Hm, thanks, I'll look at that.

@JasZhe I just pushed v0.3.2 which should fix that Go example. Please let me know if it works for you.

And to all who have chimed in on this issue: Thanks. I didn't know that this package was being used for so many different major modes and languages. And I didn't know that some of those major modes don't work the same way internally with regard to syntax tables, so I've had to make some minor adjustments to compensate for that.

In the future, Emacs 29 will likely make this kind of code simpler to write by using tree-sitter. But for now, it's complicated.

🎉 working on my end now thanks!

image

@JasZhe Great, thank you!

I didn't think many people were using prism-comments, but I'm glad to see it "in the wild." :)

I'm a simple man, I just like seeing more pretty colors in my code :)

We've gotta keep the fun in computing!

BTW, feel free to suggest any screenshots you think might look good in the readme: #19 Your theme seems to fit prism well.