Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Haven't fully investigated this, but when, for some reason, the $src variable is empty, inside _imagecache_cache function, the consecutive requests for that image lead into a 307 redirect loop that some clients don't handle properly, thus the servers become hammered until they drop. Simple fix is adding a check for $src. Replace:
if (!is_file($src) && !is_file($src = file_create_path($src))) {
with
if (!$src || (!is_file($src) && !is_file($src = file_create_path($src)))) {
inside function _imagecache_cache in imagecache.module
Comments
Comment #1
BogdanN CreditAttribution: BogdanN commentedOk,
Further investigated. That was not the problem. The flow is like this:
1st request needs to generate a new image. It checks for a lock, doesn't find it, creates the lock and starts the generation. Further requests check for the lock, find it and issue a 307. All goes like this until the 1st request finishes generating the image. If for some reason the image generation fails, the lock is never deleted so anything that points to that image will get into a neverending 307 loop. That is fine with Firefox, for instance, but some other browsers/robots, etc. simply redirect endlessly until the servers go down.
The fix is to add: file_delete($lockfile); right before the last 3 lines of the _imagecache_cache function.
Comment #2
MixologicAwesome. You have save me a *ton* of time on this.
Comment #3
BogdanN CreditAttribution: BogdanN commentedThe fix above should do the trick, but sometimes even it is not enough, as the script might simply bail out due to extreme server load. To finally put an end to the never-ending loop problems, modify _imagecache_cache and replace:
if (file_exists($lockfile)) {
watchdog('imagecache', 'ImageCache already generating: %dst, Lock file: %tmp.', array('%dst' => $dst, '%tmp' => $lockfile), WATCHDOG_NOTICE);
// 307 Temporary Redirect, to myself. Lets hope the image is done next time around.
header('Location: '. request_uri(), TRUE, 307);
exit;
}
with:
if (file_exists($lockfile)) {
// prevent the loop for running more than 3 seconds. the pic will be created on the next request anyway, and by that time the server load would have dropped
if (time() - filemtime($lockfile) > 3) {
file_delete($lockfile);
} else {
watchdog('imagecache', 'ImageCache already generating: %dst, Lock file: %tmp.', array('%dst' => $dst, '%tmp' => $lockfile), WATCHDOG_NOTICE);
// 307 Temporary Redirect, to myself. Lets hope the image is done next time around.
header('Location: '. request_uri(), TRUE, 307);
exit;
}
}
Comment #4
ezra-g CreditAttribution: ezra-g commentedThis is the same issue as #310656: Some Imagecaches producing a loop redirect..
Comment #5
bora-89 CreditAttribution: bora-89 commented@Bogdan Nicolau
Thanks for you work!
Comment #6
eric constantinides CreditAttribution: eric constantinides commentedWorked like a charm BogdanN! Thanks so much!!
Comment #7
BetoAveiga@BogdanN THANKS! :) I was having this problem for weeks! Thanks a lot for your help!!!! It works, in my opinion, it must be considered in the next release...
Comment #8
nd987 CreditAttribution: nd987 commented@BogdanN the man! The second, longer bit of code did the trick!