--- imagecache_original/imagecache.module	2011-05-16 21:12:33.000000000 +0400
+++ imagecache_new/imagecache.module	2012-01-16 22:30:35.000000000 +0400
@@ -463,10 +463,15 @@
 
   $lockfile = file_directory_temp() .'/'. $preset['presetname'] . basename($src);
   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;
+    if(time() - filemtime($lockfile) > ini_get('max_execution_time')) {
+      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;
+    } else {
+      watchdog('imagecache', 'Abandoned lock file has been deleted: %tmp.', array('%tmp' => $lockfile), WATCHDOG_NOTICE);
+      unlink($lockfile);
+    }
   }
   touch($lockfile);
   // register the shtdown function to clean up lock files. by the time shutdown
