Issue with php nginx and ImageMagick policy

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
jonathan06
Posts: 3
Joined: 2017-02-24T07:49:46-07:00
Authentication code: 1151

Issue with php nginx and ImageMagick policy

Post 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
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Issue with php nginx and ImageMagick policy

Post 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.
jonathan06
Posts: 3
Joined: 2017-02-24T07:49:46-07:00
Authentication code: 1151

Re: Issue with php nginx and ImageMagick policy

Post 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 ?
jonathan06
Posts: 3
Joined: 2017-02-24T07:49:46-07:00
Authentication code: 1151

Re: Issue with php nginx and ImageMagick policy

Post by jonathan06 »

Hello
I try to install ImageMagik 7 but still the same issue.
Does anyone has already use time limit in php ?
Thanks
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Issue with php nginx and ImageMagick policy

Post 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.
Post Reply