retext-project / retext

ReText: Simple but powerful editor for Markdown and reStructuredText
GNU General Public License v2.0
1.86k stars 199 forks source link

ReText hangs or crashes when embedding large images using html #389

Open holzkohlengrill opened 6 years ago

holzkohlengrill commented 6 years ago

When embedding large images into documents (no preview active) it seems that it is getting loaded within the main thread (btw. why is it loaded anyway and not only the reference/path to it). This might freeze ReText for a while or let it crash. Example code:

<div align="center">
<img src="bigImage.png" height="330" />
</div>

$identify bigImage.png
bigImage.png PNG 13244x9354 13244x9354+0+0 8-bit sRGB 19.6964MiB 0.000u 0:00.009

ReText: 7.1.0 (Git) PyMarkups: 3.0.0 Qt: 5.10.1 Python: 3.7.0b5

mitya57 commented 6 years ago

Can you please attach your image or upload it somewhere?

Also, if this is a crash, maybe you can obtain a stack trace? The easiest is the stacktrace from Python when you run python with -X faulthandler, but a proper stacktrace from debugger will also help.

holzkohlengrill commented 6 years ago

I could not reproduce the crash anymore but the freezing with -X faulthandler enabled and with an image I can share (as the console output says the reason is probably just the high resolution). My initial image was generated from a SVG. That's why I ended up with this extremely high resolution.

What -X faulthandler outputs is the following: QImage: out of memory, returning null image. Before I saw also QFSFileEngine::open: No file name specified. This is because I hat a typo in the path.

Output (shortened some redundant prints):

$ python.exe -X faulthandler ./retext.py
QFSFileEngine::open: No file name specified
QFSFileEngine::open: No file name specified
QFSFileEngine::open: No file name specified
QFSFileEngine::open: No file name specified
QImage: out of memory, returning null image
QImage: out of memory, returning null image
QImage: out of memory, returning null image
QImage: out of memory, returning null image
QImage: out of memory, returning null image
QImage: out of memory, returning null image
Process Process-1:
Traceback (most recent call last):
  File "C:\Program Files (x86)\Python37-32\lib\multiprocessing\process.py", line 297, in _bootstrap
    self.run()
  File "C:\Program Files (x86)\Python37-32\lib\multiprocessing\process.py", line 99, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\holzkohlengrill\AppData\Local\Programs\retext\ReText\converterprocess.py", line 63, in _converter_process_func
    job = receiveObject(conn_child)
  File "C:\Users\holzkohlengrill\AppData\Local\Programs\retext\ReText\converterprocess.py", line 32, in receiveObject
    sizeBuf = recvall(sock, 4)
  File "C:\Users\holzkkohlengrill\AppData\Local\Programs\retext\ReText\converterprocess.py", line 23, in recvall
    data = sock.recv(remaining)
ConnectionResetError: [WinError 10054] Eine vorhandene Verbindung wurde vom Remotehost geschlossen

(To avoid confusion: I just pressed Ctrl-c at the end in order to exit)

===

Somehow I cannot load this image. I always get QFSFileEngine::open: No file name specified. Does it work for you? I saw this already for other images but did not have the time to carefully look at it. Possibly this might be a bug.

With this image everything works fine. However when I convert it with imagemagick (convert Big_Mandelbrot_set.jpg Big_Mandelbrot_set.png) to png I get again QFSFileEngine::open: No file name specified. Any other program is able to open it. Other "native" (meaning non-converted) png's work.

$ identify.exe Big_Mandelbrot_set*
Big_Mandelbrot_set.jpg JPEG 16000x12000 16000x12000+0+0 8-bit sRGB 16709300B 0.000u 0:00.004
Big_Mandelbrot_set.png PNG 16000x12000 16000x12000+0+0 8-bit sRGB 37.0583MiB 0.000u 0:00.000

O.K. I think I got it now for png's (for jpg it is still a little strange for me why we get two different error messages due to the same cause). It is again the size. However we get a misleading error. I just used this image as an example and generated several png's from it:

$ identify.exe SVG_logo*
SVG_logo-small.png PNG 400x400 400x400+0+0 8-bit sRGB 27155B 0.000u 0:00.000
SVG_logo.png PNG 13000x13000 13000x13000+0+0 8-bit sRGB 1.67808MiB 0.000u 0:00.000
SVG_logo.svg SVG 100x100 100x100+0+0 16-bit sRGB 9122B 0.047u 0:00.122
SVG_logo2.png PNG 4000x4000 4000x4000+0+0 8-bit sRGB 362025B 0.000u 0:00.000
SVG_logo3.png PNG 8000x8000 8000x8000+0+0 8-bit sRGB 869695B 0.016u 0:00.014

==== Currently I am testing on this commit: 37923cb1b74eb813714854503990b8a204b5610f

mitya57 commented 4 years ago

Somehow this issue got lost in my mailbox. Now I am reviewing old issues and got back to it.

Somehow I cannot load this image. I always get QFSFileEngine::open: No file name specified. Does it work for you?

Have you downloaded that image locally, or you are trying to load a remote image? I think such error may happen when the image is remote and you are using QTextBrowser viewer (not WebKit). That viewer does not support remote images at all. Or there is a typo somewhere in the file path…

Anyway, I just tried to open that file in a regular image viewer (eog) and it made my laptop hang. Looks like 4GB memory is not enough for it. So better I won't even try with ReText.

QImage: out of memory, returning null image

I doubt I can do anything about this. ReText uses Qt, which in turn uses libjpeg-turbo, so if there is inadequate memory consumption, it most probably lies in one of these two libraries.

Can you try to open the image in a file viewer using Qt, such as gwenview?

holzkohlengrill commented 4 years ago

I only tested it with the Windows file viewer and on Xubuntu with ristretto and viewing the image was just fine.

image

image

Tried it with eog: Wow, it's handled very inefficiently. eog eats up all the memory and then just crashes.

I have the image locally (no remote loading).

monperrus commented 4 years ago

I'm using ![](img.png) but img.png is a large image with high resolution.

How to force retext to fit the image width to the preview width?

Thanks!

mitya57 commented 4 years ago

@monperrus ReText in master already includes a style to fit images in preview: https://github.com/retext-project/retext/blob/16f15df808d8af077b38cd27c5878b5eb64f4cc2/ReText/tab.py#L204

In stable version you can achieve this by using attribute lists:

![caption](img.png){: width="100%" }
monperrus commented 4 years ago

great, thanks. any idea on when the debian package will be updated to master?

mitya57 commented 4 years ago

Maybe in February…