Weasyprint: GLib-GObject-CRITICAL **: 21:14:11.559: g_object_ref: assertion 'old_val > 0' failed Segmentation fault: 11

Created on 21 Mar 2019  Â·  14Comments  Â·  Source: Kozea/WeasyPrint

I am trying to generate a PDF using WeasyPrint and jinja2. A basic flow of my code is below:

from jinja2 import Environment, FileSystemLoader
from weasyprint import HTML

jinja_env = Environment(loader=FileSystemLoader(template_path))
template = jinja_env.get_template(template_name)
html_out = template.render(template_vars)

HTML(string=html_out, base_url='imgs/').write_pdf(target=out_path,stylesheets=['style.css'])
(process:5306): GLib-GObject-CRITICAL **: 21:14:11.559: g_object_ref: assertion 'old_val > 0' failed
Segmentation fault: 11

I am happy to provide anything that would help reporduce the issue (apologies I am new to using WeasyPrint and jinja2). I've never seen an error like this before.

I have seen similar issues #829 but is there any course of action to investigate what is causing the issue if it's not WeasyPrint?

Most helpful comment

Success! The homebrew team helped me out!

Homebrew shipped with python 3.7.2 and I had python 3.7.0.

I created a virtual env with python 3.7.2 and everything worked!
@hybridpicker for awareness

All 14 comments

Hello!

I am happy to provide anything that would help reporduce the issue (apologies I am new to using WeasyPrint and jinja2). I've never seen an error like this before.

Using jinja here is not related to your problem. template.render() generates a string that is read by WeasyPrint later. Using a normal string should give the same problem.

If you want to help, you can provide an sample that like this that raises your error:

from weasyprint import HTML

html = """
<html>
…
</html>
"""

HTML(string=html).write_pdf(target='/tmp/test.pdf')

Then give the whole traceback you get.

If you can't reproduce your error with a simple script, then you can provide more information about your OS, your application and the libraries installed on your system.

Also, if your code used to work but doesn't anymore, you can try to find what has changed on your system.

If you get this error also on macOS (like for #829), then there's probably been an update of glib that breaks things.

I have seen similar issues #829 but is there any course of action to investigate what is causing the issue if it's not WeasyPrint?

When there's a crash, you can look a the stack trace, it will probably help you to find where the error comes from.

Thanks so much!

A brief recap of my investigation:

1. Simple test

from weasyprint import HTML

```html
html = """


Title of doc



pic

<div style="text-align:center">
    <h1>PDF Title Page</h1>
    <h2><i>Subtitle</i></h2>
</div>
    <p style="page-break-before: always"></p>
    <h1>Page #2</h1>



"""

```python
HTML(string=html).write_pdf(target='/tmp/test.pdf')

The PDF gets written as expected, but I get the below error that kicks me out of my python session. If I was just writing one PDF, I could deal with it. But, I am writing multiple PDFs in a loop and this error message is problamtic (as it will only write the first PDF in my loop then exit).

#Output 
(process:697): GLib-GObject-CRITICAL **: 07:19:50.623: g_object_ref: assertion 'old_val > 0' failed
Bus error: 10

2. Look at stack trace

This wasn't helpful. Maybe I have an un-trained eye, but I couldn't see anything that would point me in the direction of the error. I have the logs is you'd like to see them.

3. glib?

I am, in fact, running on MacOS. I looked at glib in my Homebrew downloads (/usr/local/Cellar) and the version of glib showed 2.60.0_1.

Uninstalled glib.

brew uninstall --ignore-dependencies --force glib

Tried to run my program (knowing it would fail since I uninstalled glib). Got this error.

Traceback (most recent call last):
  File "run_perf_analysis.py", line 19, in <module>
    from make_pdf_report import report_funcs as rf
  File "/make_pdf_report/report_funcs.py", line 13, in <module>
    from weasyprint import HTML
  File "/anaconda3/lib/python3.7/site-packages/weasyprint/__init__.py", line 393, in <module>
    from .css import preprocess_stylesheet  # noqa
  File "/anaconda3/lib/python3.7/site-packages/weasyprint/css/__init__.py", line 26, in <module>
    from . import computed_values
  File "/anaconda3/lib/python3.7/site-packages/weasyprint/css/computed_values.py", line 17, in <module>
    from .. import text
  File "/anaconda3/lib/python3.7/site-packages/weasyprint/text.py", line 249, in <module>
    'libpango-1.0.dylib')
  File "/anaconda3/lib/python3.7/site-packages/weasyprint/text.py", line 243, in dlopen
    return ffi.dlopen(names[0])  # pragma: no cover
  File "/anaconda3/lib/python3.7/site-packages/cffi/api.py", line 141, in dlopen
    lib, function_cache = _make_ffi_library(self, name, flags)
  File "/anaconda3/lib/python3.7/site-packages/cffi/api.py", line 802, in _make_ffi_library
    backendlib = _load_backend_lib(backend, libname, flags)
  File "/anaconda3/lib/python3.7/site-packages/cffi/api.py", line 798, in _load_backend_lib
    return backend.load_library(path, flags)
OSError: cannot load library '/usr/local/lib/libpango-1.0.dylib': dlopen(/usr/local/lib/libpango-1.0.dylib, 2): Library not loaded: /usr/local/opt/glib/lib/libgobject-2.0.0.dylib
  Referenced from: /usr/local/lib/libpango-1.0.dylib
  Reason: image not found

Re-installed glib

brew install glib

Still get the same initial error code:

(process:8135): GLib-GObject-CRITICAL **: 08:02:35.016: g_object_ref: assertion 'old_val > 0' failed
Bus error: 10

I think this may be a glib version issue like you mentioned, but how would I go about fixing this?

I am, in fact, running on MacOS.

Then you can be pretty sure that there's something wrong with glib-2.60.0 provided by homebrew. The same error on macOS reported twice the week glib is updated is not a coincidence. Is there a way to install an older version?

I have the logs is you'd like to see them.

Yes, and you can also post the trace corresponding to your script. Does the script fail if you just add a for-loop like:

for i in range(5):
    HTML(string=html).write_pdf(target=f'/tmp/test-{i}.pdf')

Screen Shot 2019-03-21 at 08 36 04

Only test-0.pdf gets written

Only test-0.pdf gets written

Well, it's a good old segmentation fault, and you'll need a debugger to find what's going on (I don't even know if it's possible with Python using CFFI). Unfortunately, I can't reproduce on Linux. TravisCI's build is OK too, even with glib-2.60.0.

Maybe you'll learn something by calling dmesg in a terminal.

Opening an issue on glib's GitLab and/or homebrew's GitHub may be useful to have more information about what's going on and how to fix it.

Thanks so much. Really appreciate your help here. I have opened an issue in both places. I will update the thread if I hear anything or find a solution. Thank you again!

Success! The homebrew team helped me out!

Homebrew shipped with python 3.7.2 and I had python 3.7.0.

I created a virtual env with python 3.7.2 and everything worked!
@hybridpicker for awareness

I created a virtual env with python 3.7.2 and everything worked!

Good to know, thanks for taking the time to create issues and to give the solution here.

Thanks for investigating!

For me I also needed to upgrade all my brew dependencies to solve this problem:

brew update && brew upgrade

Success! The homebrew team helped me out!

Homebrew shipped with python 3.7.2 and I had python 3.7.0.

I created a virtual env with python 3.7.2 and everything worked!
@hybridpicker for awareness

Thank you for bothering to post the solution that fixed your problem. I have tried it and it worked perfectly for a different type of issue:
(process:12404): GLib-GObject-CRITICAL **: 23:41:11.459: g_object_ref: assertion '!object_already_finalized' failed Bus error: 10

I'm getting a similar error, however I get an error when I close out my Python session, or when I try to write the second iteration (first one writes fine).

I've tried the brew update && brew upgrade approach...

(process:21963): GLib-GObject-CRITICAL **: 17:37:52.617: g_object_ref: assertion '!object_already_finalized' failed

Homebrew ships with python 3.9.1, and I used Conda to create a virtual environment for 3.9.1 (normally using python 3.7.3). I still get the crashes.

@jondoesntgit I ran into the same problem. I was able to work around it by following the macOS installation instructions, but jumping back to the general installation instructions at the top of that page. Those specify creating a clean virtual environment (a step missing from the macOS specific instructions) before actually running pip install WeasyPrint. Might be something worth looking at changing in the docs, since the instructions for both macOS and Windows don't mention the virtualenv step when walking the user through the remainder of installation.

Might be something worth looking at changing in the docs, since the instructions for both macOS and Windows don't mention the virtualenv step when walking the user through the remainder of installation.

You’re right. We will soon rewrite the documentation, we’ll put these steps for all the OSes.

I finally got it to work by running

source activate py39
pip uninstall weasyprint
pip install weasyprint
Was this page helpful?
0 / 5 - 0 ratings