Req #41245 [Opn]: Ability to set handler for "memory limit exceeded"

From: Date: Tue, 04 Sep 2012 20:18:22 +0000
Subject: Req #41245 [Opn]: Ability to set handler for "memory limit exceeded"
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-173176@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=41245&edit=1

 ID:                 41245
 Updated by:         rasmus@php.net
 Reported by:        bens at effortlessis dot com
 Summary:            Ability to set handler for "memory limit exceeded"
 Status:             Open
 Type:               Feature/Change Request
-Package:            Feature/Change Request
+Package:            *General Issues
 Operating System:   Any
 PHP Version:        4.4.6
 Block user comment: N
 Private report:     N

 New Comment:

That is rather tricky because such a callback could continue consuming memory. We 
would have to introduce the concept of soft and hard limits or something along 
those lines for this to be realistic.


Previous Comments:
------------------------------------------------------------------------
[2012-09-04 19:58:07] slusarz at curecanti dot org

This would be tremendously useful, especially when the cause of the memory overrun is not the PHP
code but rather due to the data input to the script.

For example: In Horde/IMP, we need to parse MIME e-mail messages.  These e-mail messages contain
data that potentially could be unfiltered.  A particular mail message script MUST parse the entire
data input in order to show anything (you cannot truncate MIME data, or else the resulting MIME
message is invalid).  Even using things like temporary data streams, there is still a chance that
the memory limit can be exceeded.

It would be tremendously useful to be able to define a memory limit exceeded handler for any given
script that could catch the case where we can not parse the message due to a memory limit caused by
the message data itself.  This handler could allow the script to do something like automatically
redirect the browser to a different page and display an error message.

------------------------------------------------------------------------
[2007-04-30 19:33:55] bens at effortlessis dot com

Description:
------------
When Memory Limit is exceeded, PHP simply terminates, which shows as a "blank white
screen". 

It would be very helpful if a callback function or simply an optional header redirect could be
emitted when memory limit is exceeded. It could be as simple as an option in php.ini which would
work like the "ErrorDocument" apache directive, so that an attractive, "What you
asked for used up too much memory, so sorry!" page could be displayed rather than just an
uninformative and unfriendly blank screen.

This idea could perhaps be expanded to a default "PHP died" handler, so that a friendly
"something went wrong" page could be displayed. 

Reproduce code:
---------------
$somevar=''; 
for ($i=0; $i<2000000000000; $i++) 
 $somevar.="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; 

Expected result:
----------------
Program output: 
Location: /value/set/in/php.ini/or/apache/directive/helpful.html

Logfile output:
[client 63.195.17.22] PHP Fatal error:  Allowed memory size of 100663296 bytes exhausted (tried to
allocate 11870154 bytes) in /path/to/php/file.php on line 216, referer: http://mysite.com/path/to/php/file.php?PHPSESSID=2a39d521e3660f0a4fc2132dc34e1e68

Actual result:
--------------
Program output: 

Logfile output:
[client 63.195.17.22] PHP Fatal error:  Allowed memory size of 100663296 bytes exhausted (tried to
allocate 11870154 bytes) in /path/to/php/file.php on line 216, referer: http://mysite.com/path/to/php/file.php?PHPSESSID=2a39d521e3660f0a4fc2132dc34e1e68


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=41245&edit=1


Thread (1 message)

  • rasmus@php.net
  • Unknown Message
    • rasmus@php.net
« previous php.bugs (#173176) next »