Index: ours/phpbb/includes/functions_messenger.php =================================================================== --- ours/phpbb/includes/functions_messenger.php (revision 196) +++ ours/phpbb/includes/functions_messenger.php (revision 198) @@ -619,6 +619,52 @@ } /** + * Obtains exclusive lock on queue cache file. + * Returns resource representing the lock + */ + function lock() + { + // For systems that can't have two processes opening + // one file for writing simultaneously + if (file_exists($this->cache_file . '.lock')) + { + $mode = 'rb'; + } + else + { + $mode = 'wb'; + } + $lock_fp = @fopen($this->cache_file . '.lock', $mode); + // Two processes may attempt to create lock file at the same time. + // Have the losing process try opening the lock file again for reading + // on the assumption that the winning process created it + if (!$lock_fp && $mode == 'wb') + { + $lock_fp = @fopen($this->cache_file . '.lock', 'rb'); + } + @chmod($this->cache_file . '.lock', 0777); + if ($lock_fp) + { + @flock($lock_fp, LOCK_EX); + } + return $lock_fp; + } + + /** + * Releases lock on queue cache file, using resource obtained from lock() + */ + function unlock($lock_fp) + { + // lock() will return null if opening lock file, and thus locking, failed. + // Accept null values here so that client code does not need to check them + if ($lock_fp) + { + @flock($lock_fp, LOCK_UN); + fclose($lock_fp); + } + } + + /** * Process queue * Using lock file */ @@ -626,24 +672,16 @@ { global $db, $config, $phpEx, $phpbb_root_path, $user; + $lock_fp = $this->lock(); + set_config('last_queue_run', time(), true); - // Delete stale lock file - if (file_exists($this->cache_file . '.lock') && !file_exists($this->cache_file)) + if (!file_exists($this->cache_file) || filemtime($this->cache_file) > time() - $config['queue_interval']) { - @unlink($this->cache_file . '.lock'); + $this->unlock($lock_fp); return; } - if (!file_exists($this->cache_file) || (file_exists($this->cache_file . '.lock') && filemtime($this->cache_file) > time() - $config['queue_interval'])) - { - return; - } - - $fp = @fopen($this->cache_file . '.lock', 'wb'); - fclose($fp); - @chmod($this->cache_file . '.lock', 0777); - include($this->cache_file); foreach ($this->queue_data as $object => $data_ary) @@ -700,6 +738,7 @@ break; default: + $this->unlock($lock_fp); return; } @@ -725,8 +764,6 @@ if (!$result) { - @unlink($this->cache_file . '.lock'); - messenger::error('EMAIL', $err_msg); continue 2; } @@ -770,16 +807,14 @@ { if ($fp = @fopen($this->cache_file, 'wb')) { - @flock($fp, LOCK_EX); fwrite($fp, "queue_data = unserialize(" . var_export(serialize($this->queue_data), true) . ");\n\n?>"); - @flock($fp, LOCK_UN); fclose($fp); phpbb_chmod($this->cache_file, CHMOD_READ | CHMOD_WRITE); } } - @unlink($this->cache_file . '.lock'); + $this->unlock($lock_fp); } /** @@ -792,6 +827,8 @@ return; } + $lock_fp = $this->lock(); + if (file_exists($this->cache_file)) { include($this->cache_file); @@ -811,13 +848,13 @@ if ($fp = @fopen($this->cache_file, 'w')) { - @flock($fp, LOCK_EX); fwrite($fp, "queue_data = unserialize(" . var_export(serialize($this->data), true) . ");\n\n?>"); - @flock($fp, LOCK_UN); fclose($fp); phpbb_chmod($this->cache_file, CHMOD_READ | CHMOD_WRITE); } + + $this->unlock($lock_fp); } }