Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wkhtmltopdf segfault when ran from python

I have to run wkhtmltopdf from python with subprocess.call(...). From the command line, I can generate the pdf without problem but when its run from python, it fail with a segfault.

I have no idea what is causing wkhtmltopdf to segfault.

I even tried sending my terminal env attributes but it still segfaults. I sent stderr, stdin, stdout but nothing works. What's worrying me is that it runs from a terminal but not from python.

Also, calling the process from a different process in python also makes it segfault. For exemple, I added a script in between to call this application and the script written in python also receive a segfault from wkhtmltopdf.

#!/bin/env python
import subprocess
import sys
import pdb
import os


sys.argv[0] = "/usr/local/bin/wkhtmltopdf.b"

sys.argv.remove('--quiet')

status = subprocess.call(sys.argv,
    env=env,
    stdin=sys.stdin,
    stdout=open("/tmp/stdout.w", "w"),
    stderr=open("/tmp/stderr.w", "w"))

cmd = " ".join(sys.argv)

pdb.set_trace()

Right now I'm doing this to get me time to execute the command in an external terminal. OpenErp is checking for the content of the pdf file. wkhtmltopdf.b is the original binary. I removed the quiet parameter as I wanted to see what's going on.

It apparently segfaults at this moment:

Loading pages (1/6)
[======>                                                     ] 10%

And nothing else

My version of wkhtmltopdf amd64 static from the website wkhtmltopdf.org

$ wkhtmltopdf -V
wkhtmltopdf 0.12.1 (with patched qt)

I'm running one of the ubuntu amd64 binary package on my gentoo box. It's kind of hard/long to get wkhtmltopdf compiled with patched qt on gentoo it doesn't seems to be supported by default. Yet, since it's running from the command line, it should also run from python.

I'm running it from zsh but even if inside my python program I would instead call something like this:

'/bin/sh -c "%s"' % command

It would also segfault.

like image 794
Loïc Faure-Lacroix Avatar asked Mar 07 '26 09:03

Loïc Faure-Lacroix


1 Answers

I had the exact same issue as yours but running through a different stack (Apache & PHP) but I'm not 100% sure how you launch your python in fact. Anyway, it crashed exactly at the same place as yours and it was working fine from command line so I guess it may be worth sharing in case that helps anyone ;)

I found out my issue was an ulimit set differently when ran through apache then shell. Specifically my "virtual memory" ulimit -v was pretty low. I end up doing $cmd = "ulimit -v 1073741824; {$this->wkhtmltopdf_path}...." and that solves my issue! (You can check with running ulimit -a and compare the values from the same command on the shell!)

like image 186
ROunofF Avatar answered Mar 09 '26 23:03

ROunofF



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!