hpjansson / chafa

📺🗿 Terminal graphics for the 21st century.
https://hpjansson.org/chafa/
GNU Lesser General Public License v3.0
2.97k stars 64 forks source link

DjVu documents (and potentially other cases) cause chafa to consume a lot cpu and memory #179

Closed xlucn closed 10 months ago

xlucn commented 12 months ago

Summary: Since chafa seems to try to display as many file formats as possible, sometimes it might just be inappropriate and should be prevented or optimized.

Example: One such case is DjVu files, many-page djvu documents to be accurate.

In my exact case, I have set in my lf preview script to use chafa to preview any file of image/* type. Unfortunately, DjVu files are "image/vnd.djvu" type. However, I think the argument applies to more general use cases as well.

When I use chafa to (pre)view a several-hundred-page djvu document, it consumes all the available memory and the system just hangs.

Discussion: Maybe chafa should at least check for potential memory consumption.

Maybe also optimize the preview, such as only preview the first page, which I believe will never cost many GBs of memory. This possibly applies to PDFs as well, which currently is very slow (a few seconds) to display.

Zerogaku commented 12 months ago

this has happened to me as well, when previewing a large pdf using lf, my window manager just froze

hpjansson commented 12 months ago

Hi, thanks for reporting this. This is happening because Chafa delegates to ImageMagick when there's no internal loader to handle the file format. ImageMagick gets very enthusiastic and sometimes trips on its own shoelaces.

The problem is fixed in master and will go away in the next release -- see #157.

In the meantime, you should be able to curb problematic formats in ImageMagick's configuration. On my distro, this lives in /etc/ImageMagick-7/. The best way is to list it in the security policy file:

  1. Find the format identifier by running magick -list format.
  2. Add it to the policy file (policy.xml), e.g:
    <policy domain="coder" rights="none" pattern="DJVU" />
    <policy domain="coder" rights="none" pattern="PDF" />

The default configuration varies between Linux distros; mine already has PDF on the do-not-load list, and it looks like DjVu support is not built in at all.

xlucn commented 12 months ago

thanks, I look forward to the next release!

hpjansson commented 10 months ago

I just released version 1.14.0, where ImageMagick is no longer an option. That should fix the issue (but it will no longer produce previews for DjVu and PDF; maybe one day it will be possible to do so safely).