Page 1 of 1

Issue with php nginx and ImageMagick policy

Posted: 2017-02-24T08:38:11-07:00
by jonathan06
Hello

I have an issue using ImageMagick with php when I set the time policy.

When the time set in the policy is reached, I get an 502 Bad Gateway error (so my die('in') isn't showing )! And I can't catch it in PHP.
But php process seems still runing (while 300sec). So when sevral calls to the code are made, php finaly crash on server.

When I don't set a max time into policy.xml or when I use a small image all is working fine !
But if I use a big image, it crash.

Is there a solution to catch this 'time limit reached' into php instead of getting a connection reset by peer ?

The ningx error :

Code: Select all

[error] 28773#28773: *165 recv() failed (104: Connection reset by peer) while reading response header from upstream
Here is the code :

Code: Select all

<?php
$img = new Imagick();
$img->readImage('samples/big_image.jpg');
die('in');
In my policy.xml :

Code: Select all

<policy domain="resource" name="time" value="10"/>
Imagick version:

Code: Select all

imagick module version 	3.4.3
imagick classes 	Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator, ImagickKernel
Imagick compiled with ImageMagick version 	ImageMagick 6.8.9-9 Q16 x86_64 2016-11-26 http://www.imagemagick.org
Imagick using ImageMagick library version 	ImageMagick 6.8.9-9 Q16 x86_64 2016-11-26 http://www.imagemagick.org
ImageMagick copyright 	Copyright (C) 1999-2014 ImageMagick Studio LLC
ImageMagick release date 	2016-11-26 
I'm on debian 8 Jessie,with nginx and php7 fpm

Thanks

Re: Issue with php nginx and ImageMagick policy

Posted: 2017-02-24T08:58:17-07:00
by magick
When the security policy time limit is exceeded, ImageMagick sets the errno to ECANCELED and ImageMagick exits. Anything that wraps the ImageMagick API's is forced to exit too. PHP should be able to catch the fatal error and recover. Its been awhile since we used PHP, but can't you do a eval() for example and check the status of the eval method? That should be recoverable.

Re: Issue with php nginx and ImageMagick policy

Posted: 2017-02-24T09:58:06-07:00
by jonathan06
Thanks for your help magick.
PHP should be able to catch the fatal error and recover
Are you sure ??
Even if i do a try/catch , I never 'enter' into the catch part (try catching ErrorException, Exception, ImagickException...).
If the 'try' fail, code is exited showing 502 BAD GATEWAY

When I do

Code: Select all

<?php
$img = new Imagick();
$a = eval('return ' . $img->readImage('samples/test.jpg') . ';');
var_dump($a);
with a small image :

Code: Select all

int(1) 
with a big image :

Code: Select all

502 Bad Gateway
and code is exited before the var dump

Have you an exemple of php script who catch the 'time limit exceeded' please ?

Re: Issue with php nginx and ImageMagick policy

Posted: 2017-02-27T07:05:16-07:00
by jonathan06
Hello
I try to install ImageMagik 7 but still the same issue.
Does anyone has already use time limit in php ?
Thanks

Re: Issue with php nginx and ImageMagick policy

Posted: 2017-02-27T08:12:48-07:00
by magick
Again, we have limited knowledge of PHP. However, doesn't PHP have a time limit in its configuration file, php.ini? If so, you can limit scripts with a PHP time-limit rather than the ImageMagick time-limit policy.