Forum Bugs

PHP wrapper idle

htorres
Hello

I'm testing prince on centOS 6 with cloudlinux.
I've installed prince rpm and added prince to cagefs, so it is available to all cpanel accounts.
On a specific account I can execute prince with success over command line, however with PHP wrapper it doesn't work, it just gives time out error after 30 s.
I've executed with --debug and -verbose and kill the process before timeout and I can see this:

Prince instantiation OK
Log file to /home/signedp/public_html/dev/kwb/Classes/PDF/prince/prince_error.log

"/usr/bin/prince" -i "html" --log="/home/signedp/public_html/dev/kwb/Classes/PDF/prince/prince_error.log" --fileroot="/home/signedp/public_html/dev/kwb/templates/flyer_txt_prince/" --debug --verbose --structured-log=normal "/home/signedp/public_html/dev/kwb/templates/flyer_txt_prince/pag1.html" -o "/home/signedp/public_html/dev/kwb/templates/flyer_txt_prince/pag1_prince_175904.pdf"


bool(false) DEBUG
Array
(
    [0] => Array
        (
            [0] => dbg
            [1] => 
            [2] => init locking for NSS
        )

    [1] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loading license: /usr/lib/prince/license/license.dat
        )

    [2] => Array
        (
            [0] => inf
            [1] => 
            [2] => loading document: /usr/lib/prince/license/license.dat
        )

    [3] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded resource: /usr/lib/prince/license/license.dat
        )

    [4] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded resource: type: no
        )

    [5] => Array
        (
            [0] => inf
            [1] => 
            [2] => loading style sheet: /usr/lib/prince/style/fonts.css
        )

    [6] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded resource: /usr/lib/prince/style/fonts.css
        )

    [7] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded resource: type: no
        )

    [8] => Array
        (
            [0] => dbg
            [1] => 
            [2] => starting prefetch threads
        )

    [9] => Array
        (
            [0] => inf
            [1] => 
            [2] => loading HTML5 input: /home/signedp/public_html/dev/kwb/templates/flyer_txt_prince/pag1.html
        )

    [10] => Array
        (
            [0] => inf
            [1] => 
            [2] => loading document: /home/signedp/public_html/dev/kwb/templates/flyer_txt_prince/pag1.html
        )

    [11] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded resource: /home/signedp/public_html/dev/kwb/templates/flyer_txt_prince/pag1.html
        )

    [12] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded resource: type: no
        )

    [13] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded document: /home/signedp/public_html/dev/kwb/templates/flyer_txt_prince/pag1.html
        )

    [14] => Array
        (
            [0] => dbg
            [1] => 
            [2] => sniffed doctype: XHTML
        )

    [15] => Array
        (
            [0] => inf
            [1] => 
            [2] => loading style sheet: /usr/lib/prince/style/xhtml-ns.css
        )

    [16] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded resource: /usr/lib/prince/style/xhtml-ns.css
        )

    [17] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded resource: type: no
        )

    [18] => Array
        (
            [0] => inf
            [1] => 
            [2] => loading style sheet: /usr/lib/prince/style/common.css
        )

    [19] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded resource: /usr/lib/prince/style/common.css
        )

    [20] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded resource: type: no
        )

    [21] => Array
        (
            [0] => inf
            [1] => 
            [2] => loading style sheet: /usr/lib/prince/style/hyph.css
        )

    [22] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded resource: /usr/lib/prince/style/hyph.css
        )

    [23] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded resource: type: no
        )

    [24] => Array
        (
            [0] => inf
            [1] => 
            [2] => loading style sheet: /usr/lib/prince/style/mathml.css
        )

    [25] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded resource: /usr/lib/prince/style/mathml.css
        )

    [26] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded resource: type: no
        )

    [27] => Array
        (
            [0] => inf
            [1] => 
            [2] => loading style sheet: /usr/lib/prince/style/svg.css
        )

    [28] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded resource: /usr/lib/prince/style/svg.css
        )

    [29] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded resource: type: no
        )

    [30] => Array
        (
            [0] => dbg
            [1] => 
            [2] => stopping prefetch threads
        )

    [31] => Array
        (
            [0] => dbg
            [1] => 
            [2] => pack
        )

    [32] => Array
        (
            [0] => dbg
            [1] => 
            [2] => font request: serif
        )

    [33] => Array
        (
            [0] => dbg
            [1] => 
            [2] => font scan: Times New Roman
        )

    [34] => Array
        (
            [0] => dbg
            [1] => 
            [2] => font scan: Times New Roman, 0 matches
        )

    [35] => Array
        (
            [0] => dbg
            [1] => 
            [2] => font scan: DejaVu Serif
        )

    [36] => Array
        (
            [0] => dbg
            [1] => 
            [2] => font scan: DejaVu Serif, 8 matches
        )

    [37] => Array
        (
            [0] => dbg
            [1] => 
            [2] => font scan: DejaVu LGC Serif
        )

    [38] => Array
        (
            [0] => dbg
            [1] => 
            [2] => font scan: DejaVu LGC Serif, 8 matches
        )

    [39] => Array
        (
            [0] => dbg
            [1] => 
            [2] => font scan: Liberation Serif
        )

    [40] => Array
        (
            [0] => dbg
            [1] => 
            [2] => font scan: Liberation Serif, 0 matches
        )

    [41] => Array
        (
            [0] => dbg
            [1] => 
            [2] => font scan: OpenSymbol
        )

    [42] => Array
        (
            [0] => dbg
            [1] => 
            [2] => font scan: OpenSymbol, 0 matches
        )

    [43] => Array
        (
            [0] => dbg
            [1] => 
            [2] => font scan: AR PL UMing CN
        )

    [44] => Array
        (
            [0] => dbg
            [1] => 
            [2] => font scan: AR PL UMing CN, 0 matches
        )

    [45] => Array
        (
            [0] => dbg
            [1] => 
            [2] => font scan: AR PL SungtiL GB
        )

)


Any ideia what is wrong ?
mikeday
Is it a big input document? If you run "top" can you see if Prince is using any cpu while it appears to be hanging?
htorres
Hello Mike,

No, it only has:
<h1>Hello world</h1>

If i run "top" I can see the process but it is very low on RAM/CPU
htorres
In attach the "top" result for prince process.

The process wont stop unti I force a kill.
The prince_error.log is created but it remains empty :(

No clue what is happening here :(
  1. Captura de ecrã 2016-01-29, às 11.33.31.png47.0 kB
    top

Edited by htorres

mikeday
Thanks, does the log file stop at the same place as the PHP messages, or does it continue further?
htorres
Sorry Mike, I couldn't understand, what did you mean?

This is my PHP code:

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

// header('Content-Type: application/pdf');
// header('Content-Disposition: inline; filename="exemplo.pdf"');

include("/home/signedp/public_html/dev/kwb/Classes/PDF/prince/prince.php");

$prince = new Prince('/usr/bin/prince');

if(!$prince) die("<p>Prince instantiation failed</p>");
else echo "Prince instantiation OK<br />";

$sFileRoot = dirname(__FILE__).'/';
$logLoc = $sFileRoot.'prince_error.log';
echo("<p>Log file to $logLoc</p>");

$prince->setHTML(1);

$prince->setLog($logLoc);
$prince->setFileRoot($sFileRoot);


$prince->addStyleSheet($sFileRoot.'style.css');

// $prince->setPDFAuthor('Author');
// $prince->setPDFCreator('Creator');

$aErros = array();
$oPrince = $prince->convert_file_to_file($sFileRoot.'pag1.html', $sFileRoot.'pag1_prince_'.date('His').'.pdf', $aErros);
// $oPrince = $prince->convert_string_to_file("<h1>Hello world</h1>", $sFileRoot.'pag1_prince_'.date('His').'.pdf', $aErros);

echo "<pre>";
print_r($prince);
echo "</pre>";

var_dump($oPrince);
echo "DEBUG<pre>";
print_r($aErros);
echo "</pre>";
?>
mikeday
Can you attach prince_error.log as well?
htorres
it is completly empty Mike :(

The only way I can get any result from what is going on is by killing the prince process and dumping the array "$aErros"
mikeday
That is unfortunate. It looks like the process is deadlocking somehow, but I don't know why. Does the convert_string_to_file fail in a similar way?
htorres
If I run prince by command line then the prince_error.log has this(see attachment)
  1. prince_error.log5.9 kB
    prince_error.log
htorres
Yep, exactly the same problem :(
Prince instantiation OK
Log file to /home/signedp/public_html/dev/kwb/templates/flyer_txt_prince/prince_error.log

Prince Object
(
    [exePath:Prince:private] => /usr/bin/prince
    [styleSheets:Prince:private] => -s "/home/signedp/public_html/dev/kwb/templates/flyer_txt_prince/style.css" 
    [scripts:Prince:private] => 
    [fileAttachments:Prince:private] => 
    [licenseFile:Prince:private] => 
    [licenseKey:Prince:private] => 
    [inputType:Prince:private] => html
    [javascript:Prince:private] => 
    [baseURL:Prince:private] => 
    [doXInclude:Prince:private] => 1
    [httpUser:Prince:private] => 
    [httpPassword:Prince:private] => 
    [httpProxy:Prince:private] => 
    [httpTimeout:Prince:private] => 
    [insecure:Prince:private] => 
    [logFile:Prince:private] => /home/signedp/public_html/dev/kwb/templates/flyer_txt_prince/prince_error.log
    [fileRoot:Prince:private] => /home/signedp/public_html/dev/kwb/templates/flyer_txt_prince/
    [embedFonts:Prince:private] => 1
    [subsetFonts:Prince:private] => 1
    [noArtificialFonts:Prince:private] => 
    [compress:Prince:private] => 1
    [pdfTitle:Prince:private] => 
    [pdfSubject:Prince:private] => 
    [pdfAuthor:Prince:private] => 
    [pdfKeywords:Prince:private] => 
    [pdfCreator:Prince:private] => 
    [authMethod:Prince:private] => 
    [authUser:Prince:private] => 
    [authPassword:Prince:private] => 
    [authServer:Prince:private] => 
    [authScheme:Prince:private] => 
    [noAuthPreemptive:Prince:private] => 
    [pageSize:Prince:private] => 
    [pageMargin:Prince:private] => 
    [encrypt:Prince:private] => 
    [encryptInfo:Prince:private] => 
    [options:Prince:private] => 
)
bool(false) DEBUG
Array
(
    [0] => Array
        (
            [0] => dbg
            [1] => 
            [2] => init locking for NSS
        )

    [1] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loading license: /usr/lib/prince/license/license.dat
        )

    [2] => Array
        (
            [0] => inf
            [1] => 
            [2] => loading document: /usr/lib/prince/license/license.dat
        )

    [3] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded resource: /usr/lib/prince/license/license.dat
        )

    [4] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded resource: type: no
        )

    [5] => Array
        (
            [0] => inf
            [1] => 
            [2] => loading style sheet: /usr/lib/prince/style/fonts.css
        )

    [6] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded resource: /usr/lib/prince/style/fonts.css
        )

    [7] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded resource: type: no
        )

    [8] => Array
        (
            [0] => dbg
            [1] => 
            [2] => starting prefetch threads
        )

    [9] => Array
        (
            [0] => inf
            [1] => 
            [2] => loading HTML5 input: -
        )

    [10] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded document: -
        )

    [11] => Array
        (
            [0] => dbg
            [1] => 
            [2] => sniffed doctype: XHTML
        )

    [12] => Array
        (
            [0] => inf
            [1] => 
            [2] => loading style sheet: /usr/lib/prince/style/xhtml-ns.css
        )

    [13] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded resource: /usr/lib/prince/style/xhtml-ns.css
        )

    [14] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded resource: type: no
        )

    [15] => Array
        (
            [0] => inf
            [1] => 
            [2] => loading style sheet: /usr/lib/prince/style/common.css
        )

    [16] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded resource: /usr/lib/prince/style/common.css
        )

    [17] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded resource: type: no
        )

    [18] => Array
        (
            [0] => inf
            [1] => 
            [2] => loading style sheet: /usr/lib/prince/style/hyph.css
        )

    [19] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded resource: /usr/lib/prince/style/hyph.css
        )

    [20] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded resource: type: no
        )

    [21] => Array
        (
            [0] => inf
            [1] => 
            [2] => loading style sheet: /usr/lib/prince/style/mathml.css
        )

    [22] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded resource: /usr/lib/prince/style/mathml.css
        )

    [23] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded resource: type: no
        )

    [24] => Array
        (
            [0] => inf
            [1] => 
            [2] => loading style sheet: /usr/lib/prince/style/svg.css
        )

    [25] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded resource: /usr/lib/prince/style/svg.css
        )

    [26] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded resource: type: no
        )

    [27] => Array
        (
            [0] => inf
            [1] => 
            [2] => loading style sheet: /home/signedp/public_html/dev/kwb/templates/flyer_txt_prince/style.css
        )

    [28] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded resource: /home/signedp/public_html/dev/kwb/templates/flyer_txt_prince/style.css
        )

    [29] => Array
        (
            [0] => dbg
            [1] => 
            [2] => loaded resource: type: no
        )

)


Any ideas?
htorres
What is strange is that I use Wkhtmltopdf in the same way as prince PHP wrapper, with proc_open and it just works fine....
I really would love to use prince, please help....
mikeday
It looks like it will be necessary to debug the PHP readMessage function and see exactly what is going on and where it is blocking. One possibility would be to change convert_internal_file_to_file, which is currently like this:
            $result = $this->readMessages($pipes[2], $msgs, $dats);

            fclose($pipes[0]);
            fclose($pipes[1]);
            fclose($pipes[2]);

            proc_close($process);

Perhaps it should close stdin and stdout before calling readMessages, and just leave stderr open to reduce the chance of deadlocks, like this:
            fclose($pipes[0]);
            fclose($pipes[1]);

            $result = $this->readMessages($pipes[2], $msgs, $dats);

            fclose($pipes[2]);

            proc_close($process);

Although it doesn't seem like this would be very likely to be causing this problem.
htorres
Hello Mike,

Just made that change but still same result :(
mikeday
I think it is going to require careful step by step debugging of exactly what is happening in readMessages, but unfortunately I cannot participate right at this moment as it is approaching midnight and I must sleep.
htorres
No problem Mike, when you come back we give another try.
In the mean while I will try to debug and will update you if I find any solution.

Best regards
htorres
Hi Mike,

If I call same command that PHP wrapper generates to proc_open with system, like:
system('/usr/bin/prince --log=/home/signedp/prince_error.log --debug --verbose --structured-log=normal - -o /home/signedp/pag1_prince_171148.pdf')
IT WORKS :O

Any ideas?

-------EDIT------
Bummer, It only worked because I've passed passthru command, if I indicate a file to read then it just hangs..... :(

Edited by htorres

mikeday
Apparently a Linux kernel bug in CentOS could cause deadlocks due to broken futex behaviour. Which kernel are you using?
htorres
Hello Mike, I've updated linux kernel but like dcheas (http://www.princexml.com/forum/topic/3456/centos-6.6-php-convert_file_to_file-infinite-loop-in?p=1#16980) it didn't fixed the issue.
A temporary workaround was to execute the command over php ssh2.
But multiple request could leave us with too much ssh connections so we drop this implementation :(
mikeday
It is still unclear exactly what is causing this problem. I'm not sure what your workaround involved, were you still running it on the same machine or a different machine?
mikeday
We have now fixed a deadlock issue when running Prince under PHP which could be related to this one; the fix is available in Prince 11.3.