New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cairo out of memory #566

Closed
edkirin opened this Issue Jan 25, 2018 · 5 comments

Comments

Projects
None yet
2 participants
@edkirin

edkirin commented Jan 25, 2018

Sometimes cairo raises exception with out of memory error when converting simple html document to single page pdf. Example of html which sometimes raises the error is included in issue #565

Traceback (most recent call last):
  File "/home/eden/env/pdfworker2/lib/python3.5/site-packages/flask/app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/eden/env/pdfworker2/lib/python3.5/site-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/home/eden/env/pdfworker2/lib/python3.5/site-packages/flask/app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/eden/env/pdfworker2/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/eden/env/pdfworker2/lib/python3.5/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/eden/env/pdfworker2/lib/python3.5/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/eden/env/pdfworker2/lib/python3.5/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/eden/env/pdfworker2/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/eden/env/pdfworker2/lib/python3.5/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/eden/env/pdfworker2/lib/python3.5/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/eden/www/mysite/run.py", line 70, in process_api
    pdf = html_to_pdf(html)
  File "/home/eden/www/mysite/main/util.py", line 9, in html_to_pdf
    pdf = h.write_pdf()
  File "/home/eden/env/pdfworker2/lib/python3.5/site-packages/weasyprint/__init__.py", line 183, in write_pdf
    target, zoom, attachments)
  File "/home/eden/env/pdfworker2/lib/python3.5/site-packages/weasyprint/document.py", line 497, in write_pdf
    surface.show_page()
  File "/home/eden/env/pdfworker2/lib/python3.5/site-packages/cairocffi/surfaces.py", line 529, in show_page
    self._check_status()
  File "/home/eden/env/pdfworker2/lib/python3.5/site-packages/cairocffi/surfaces.py", line 141, in _check_status
    _check_status(cairo.cairo_surface_status(self._pointer))
  File "/home/eden/env/pdfworker2/lib/python3.5/site-packages/cairocffi/__init__.py", line 79, in _check_status
    raise exception(message, status)
MemoryError: ("cairo returned CAIRO_STATUS_NO_MEMORY: b'out of memory'", 1)

Environment:

  • python 3.5.2
(pdfworker2) eden@sunce:~> pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
cairocffi (0.8.0)
CairoSVG (2.1.3)
cffi (1.11.4)
click (6.7)
cssselect (1.0.1)
cssselect2 (0.2.1)
defusedxml (0.5.0)
elaphe3 (0.2.0)
Flask (0.12.2)
html5lib (1.0.1)
itsdangerous (0.24)
Jinja2 (2.9.6)
lxml (3.8.0)
MarkupSafe (1.0)
olefile (0.44)
pdfrw (0.4)
Pillow (5.0.0)
pip (9.0.1)
pkg-resources (0.0.0)
pyBarcode (0.8b1)
pycparser (2.18)
Pyphen (0.9.4)
pyStrich (0.8)
setuptools (36.4.0)
six (1.11.0)
tinycss (0.4)
tinycss2 (0.6.1)
WeasyPrint (0.42)
webencodings (0.5.1)
Werkzeug (0.12.2)
wheel (0.30.0a0)
@liZe

This comment has been minimized.

Member

liZe commented Jan 26, 2018

Do you have the same problem using WeasyPrint in a terminal, or does it only happen in your Flask application?

@liZe liZe added the crash label Jan 26, 2018

@liZe liZe added this to the 43 milestone Jan 26, 2018

@edkirin

This comment has been minimized.

edkirin commented Jan 26, 2018

Error appears both in Flask app and in standalone python app.

Here is a test application to reproduce the error:
outofmemory.zip

Replace file:///tmp/xx/outofmemory/ with directory where files are unpacked.

Simple application create the same pdf from same html in loop and after certain number of loops, it crashes. Sometimes after 10, 100 or 200 iterations. The same exception appears much sooner with weasyprint 0.40 than with latest version. Of course, my PC doesn't runs out of memory, it has like 9GB free out of 16GB.

(pdfworker2) eden@sunce:/tmp/xx/outofmemory> python convert.py 
[1] converting... done.
[2] converting... done.
[3] converting... done.
[4] converting... done.
[5] converting... done.
[6] converting... done.
[7] converting... done.
[8] converting... done.
[9] converting... done.
[10] converting... done.
  ...
[91] converting... done.
[92] converting... Traceback (most recent call last):
  File "convert.py", line 21, in <module>
    html_to_pdf(html=html, save_to=out_fname)
  File "convert.py", line 5, in html_to_pdf
    pdf = h.write_pdf()
  File "/home/eden/env/pdfworker2/lib/python3.5/site-packages/weasyprint/__init__.py", line 183, in write_pdf
    target, zoom, attachments)
  File "/home/eden/env/pdfworker2/lib/python3.5/site-packages/weasyprint/document.py", line 497, in write_pdf
    surface.show_page()
  File "/home/eden/env/pdfworker2/lib/python3.5/site-packages/cairocffi/surfaces.py", line 529, in show_page
    self._check_status()
  File "/home/eden/env/pdfworker2/lib/python3.5/site-packages/cairocffi/surfaces.py", line 141, in _check_status
    _check_status(cairo.cairo_surface_status(self._pointer))
  File "/home/eden/env/pdfworker2/lib/python3.5/site-packages/cairocffi/__init__.py", line 79, in _check_status
    raise exception(message, status)
MemoryError: ("cairo returned CAIRO_STATUS_NO_MEMORY: b'out of memory'", 1)

Environment:

(pdfworker2) eden@sunce:/tmp/xx/outofmemory> pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
cairocffi (0.8.0)
CairoSVG (2.1.3)
cffi (1.11.4)
click (6.7)
cssselect (1.0.1)
cssselect2 (0.2.1)
defusedxml (0.5.0)
elaphe3 (0.2.0)
Flask (0.12.2)
html5lib (1.0.1)
itsdangerous (0.24)
Jinja2 (2.9.6)
lxml (3.8.0)
MarkupSafe (1.0)
olefile (0.44)
pdfrw (0.4)
Pillow (5.0.0)
pip (9.0.1)
pkg-resources (0.0.0)
pyBarcode (0.8b1)
pycparser (2.18)
Pyphen (0.9.4)
pyStrich (0.8)
setuptools (36.4.0)
six (1.11.0)
tinycss (0.4)
tinycss2 (0.6.1)
WeasyPrint (0.42)
webencodings (0.5.1)
Werkzeug (0.12.2)
wheel (0.30.0a0)
@liZe

This comment has been minimized.

Member

liZe commented Jan 26, 2018

Thank you for this full example. I can reproduce the issue.

The same exception appears much sooner with weasyprint 0.40 than with latest version.

Yes, that's because of #441. This issue seems to be very similar, as it happens when using both @font-face and SVG files.

Of course, my PC doesn't runs out of memory, it has like 9GB free out of 16GB.

Yes, CAIRO_STATUS_NO_MEMORY is used for various unrelated errors.

@edkirin

This comment has been minimized.

edkirin commented Jan 26, 2018

Yea, it looks like the issue #441 is still present. Thanks for looking into it.

@liZe liZe closed this in cdd175c Jan 28, 2018

@liZe

This comment has been minimized.

Member

liZe commented Jan 28, 2018

#441 was still present, its fix was not enough. Keeping a mandatory reference to font_config in the document is much better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment