vedang / pdf-tools

Emacs support library for PDF files.
https://pdftools.wiki
GNU General Public License v3.0
618 stars 89 forks source link

Emacs sometimes gets stuck after it is idle in a `pdf-tools` buffer #278

Open benthamite opened 5 months ago

benthamite commented 5 months ago

Describe the bug

When I leave the computer and the current buffer is displaying a PDF file with pdf-tools, upon returning to it hours later I sometimes have to run pkill -SIGUSR2 Emacs. In those cases, the debugger returns a backtrace like this:

Debugger entered--entering a function:
* help-command-error-confusable-suggestions((quit) "" nil)
recursive-edit()
debug(lambda)
* help-command-error-confusable-suggestions((quit) "" nil)
recursive-edit()
debug(lambda)
* substitute-command-keys("Quit")
pdf-info-query(pagesize "/Users/pablostafforini/Google Drive/library-pdf/Borges1974ObrasCompletas1923.pdf" 1048)
pdf-info-pagesize(1048)
pdf-cache-pagesize(1048)
pdf-view-desired-image-size(1048 #<window 3 on Borges1974ObrasCompletas1923.pdf>)
pdf-view-create-page(1048 #<window 3 on Borges1974ObrasCompletas1923.pdf>)
pdf-view-display-page(1048 #<window 3 on Borges1974ObrasCompletas1923.pdf>)
pdf-view-redisplay(#<window 3 on Borges1974ObrasCompletas1923.pdf>)
pdf-view-maybe-redisplay-resized-windows()
pdf-view-redisplay-some-windows()

Steps to Reproduce the behaviour The behavior is not easily reproducible because it happens erratically and only after long periods of inactivity.

What is the expected behaviour? Emacs does not get stuck.

Desktop Please complete the following information:

Your pdf-tools install Please complete the following information:

     Status: Available from nongnu -- Install
    Archive: nongnu
    Version: 1.1.0
     Commit: a9c9a12c3ecf2005fa641059368ac8284f507620
    Summary: Support library for PDF documents
   Requires: emacs-26.3, tablist-1.0, let-alist-1.0.4
    Website: https://github.com/vedang/pdf-tools/
   Keywords: files multimedia 
 Maintainer: Vedang Manerikar <vedang.manerikar@gmail.com>
     Author: Andreas Politz <mail@andreas-politz.de>
(use-package pdf-tools
  :defer 10
  :mode ("\\.pdf\\'" . pdf-view-mode)
  :config
  (setq pdf-view-use-scaling t)
  (setq pdf-view-use-imagemagick nil)
  (setq pdf-view-resize-factor 1.1)
  (setq pdf-annot-default-annotation-properties
        '((t
           (label . user-full-name))
          (text
           (color . "#ff0000")
           (icon . "Note"))
          (highlight
           (color . "LightBlue2"))
          (underline
           (color . "blue"))
          (squiggly
           (color . "orange"))
          (strike-out
           (color . "red"))))

  (pdf-tools-install t)

  (add-hook 'pdf-tools-enabled-hook #'pdf-view-fit-page-to-window)
  (add-hook 'pdf-tools-enabled-hook #'sow-mode)

  :general
  ((pdf-view-mode-map pdf-annot-minor-mode-map pdf-history-minor-mode-map)
   "e" 'pdf-annot-add-highlight-markup-annotation
   "j" 'pdf-view-goto-page
   "k" 'pdf-view-previous-line-or-previous-page
   "l" 'pdf-view-next-line-or-next-page
   "H-c" 'pdf-view-kill-ring-save
   "A-u" 'pdf-view-midnight-minor-mode))
aikrahguzar commented 5 months ago

I have seen Emacs hang with pdf-tools but it has been due to some interaction with url-queue that I don't understand. My best guess is there is some race condition somewhere in the tq library that pdf-tools use and if pdf-info-query always shows up at the top of backtrace, you are probably seeing another manifestation of the same thing.

You can try disabling pdf-cache-prefetch-minor-mode and see if the hangs persist. This helped me, but since the hangs happen after inactivity (prefetching should be done by then) I doubt that it will help your case.

Another thing to figure out is why is the size of the window changing (likely when the Emacs frame gets focus). Do you have any customizations for after-focus-change-function or focus-in-hook?

benthamite commented 5 months ago

Thanks. I have disabled pdf-cache-prefetch-minor-mode and will report back if I notice any change.

I don't have any customizations for after-focus-change-function or focus-in-hook.

aikrahguzar commented 5 months ago

Assuming pdf-view-maybe-redisplay-resized-windows always shows up in the backtrace, one thing to try will be to resize a pdf window manually using mouse or keybind and see if you can reproduce the issue that way.

goofansu commented 2 months ago

The issue can be reproduced by opening a PDF and M-x toggle-frame-fullscreen. Resize using mouse or Rectangle keybindings doesn't stuck Emacs.

Environment:

A workaround is to (setq ns-use-native-fullscreen t).

Screencast https://github.com/user-attachments/assets/1b7959ff-71e1-4a78-88e5-17a65cd31519
aikrahguzar commented 1 month ago

The issue can be reproduced by opening a PDF and M-x toggle-frame-fullscreen. Resize using mouse or Rectangle keybindings doesn't stuck Emacs.

Environment:

* macOS Version 14.6 (23G80)

* GNU Emacs 29.3 (build 1, aarch64-apple-darwin22.6.0, NS appkit-2299.70 Version
  13.5.2 (Build 22G91))

* `(setq ns-use-native-fullscreen nil)`

A workaround is to (setq ns-use-native-fullscreen t). Screencast CleanShot.2024-08-06.at.22.12.51.mp4

Does it happen with just emacs -Q and pdf-tools-mode? And can you please produce a backtrace? (Do M-x toggle-debug-on-quit RET before full screening. C-g will produce a backtrace if it works. If it doesn't pkill -SIGUSR2 emacs should do the trick.)

goofansu commented 1 month ago

@aikrahguzar I tried to get the backtrace according to your methods.

C-g cannot recover the Emacs from frozen state, then I tried pkill -SIGUSR2 Emacs (I'm using the Emacs.app on macOS). It kills the Emacs but I don't know where to get the backtrace.

benthamite commented 1 month ago

@goofansu, pkill -SIGUSR2 Emacs should both kill the process and create an associated backtrace. Did you look for it in the list of buffers (C-x b *Backtrace*)?

goofansu commented 1 month ago

@benthamite I can only see a transparent *Backtrace* buffer in Mission Control after using the command, see the video: https://share.cleanshot.com/GPdZtJWbDHZvsGfxdqxM

benthamite commented 1 month ago

@goofansu, what appears to be going on is that pkill -SIGUSR2 Emacs didn’t succeed in killing the relevant process, so Emacs continues to be unresponsive. You can try the "nuclear option" while true; do pkill -SIGUSR2 Emacs; done and see if this does the trick. You may also want to try a combination of pkill -SIGUSR2 Emacs, hitting ESC repeatedly and wait a few minutes, which in my experience sometimes eventually restores the Emacs session and triggers the backtrace. Otherwise, this may be one of those cases when restarting is the only option, in which case producing a backtrace won’t be possible.

benthamite commented 1 month ago

(Also, before you do any of the above, you may want to run pkill -SIGUSR2 Emacs when Emacs is working normally, just to test if this command has its intended effect in normal circumstances.)

goofansu commented 1 month ago

(Also, before you do any of the above, you may want to run pkill -SIGUSR2 Emacs when Emacs is working normally, just to test if this command has its intended effect in normal circumstances.)

@benthamite It works in normal circumstance: CleanShot 2024-08-30 at 23 57 59

Then I made Emacs frozen like https://github.com/vedang/pdf-tools/issues/278#issuecomment-2321548661, and run while true; do pkill -SIGUSR2 Emacs; done in bash shell, Emacs just crashed and I still cannot visit the *Backtrace* buffer.

You may also want to try a combination of pkill -SIGUSR2 Emacs, hitting ESC repeatedly and wait a few minutes, which in my experience sometimes eventually restores the Emacs session and triggers the backtrace.

I'll try this combination.

goofansu commented 1 month ago

@benthamite I found I can select and run command in the transparent *Backtrace* buffer although I cannot see the content, but unfortunately the content I copied doesn't help:

Debugger entered--entering a function:
* help-command-error-confusable-suggestions((quit) "" nil)

CleanShot 2024-08-31 at 00 26 55

benthamite commented 1 month ago

Mmh, weird: I don’t quite understand what’s going on.

In any case, thank you for your efforts in helping us debug this issue.

aikrahguzar commented 1 month ago

@goofansu I don't have a good idea on how to proceed with debugging. I think running under gdb might help but I don't know how to go about that. If you can please post a question about how to debug this situation to help-gnu-emacs. You will likely get response for Emacs maintainers and people on that list are very helpful.

goofansu commented 1 month ago

@aikrahguzar Ok, I will. Thank you for the guide.