From b076f503406cf470500bc629b1dfb15f3bc491cc Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Fri, 8 May 2015 13:18:23 -0500
Subject: [PATCH] autocron: Release agent session before running

The PHP session system will hold a lock on the session until it is released.
It is important to release the session before performing long-running tasks
so that further requests from the agent are not locked.
---
 scp/autocron.php | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)

diff --git a/scp/autocron.php b/scp/autocron.php
index eabc67151..d6ff919b3 100644
--- a/scp/autocron.php
+++ b/scp/autocron.php
@@ -39,14 +39,25 @@ ob_start(); //Keep the image output clean. Hide our dirt.
 $sec=time()-$_SESSION['lastcroncall'];
 $caller = $thisstaff->getUserName();
 
-if($sec>180 && $ost && !$ost->isUpgradePending()): //user can call cron once every 3 minutes.
+// Agent can call cron once every 3 minutes.
+if ($sec < 180 || !$ost || $ost->isUpgradePending())
+    ob_end_clean();
+
 require_once(INCLUDE_DIR.'class.cron.php');
 
-$thisstaff = null; //Clear staff obj to avoid false credit internal notes & auto-assignment
-Cron::TicketMonitor(); //Age tickets: We're going to age tickets regardless of cron settings.
+// Clear staff obj to avoid false credit internal notes & auto-assignment
+$thisstaff = null;
+
+// Release the session to prevent locking a future request while this is
+// running
+$_SESSION['lastcroncall'] = time();
+session_write_close();
+
+// Age tickets: We're going to age tickets regardless of cron settings.
+Cron::TicketMonitor();
 
-// Run file purging about every 30 minutes
-if (mt_rand(1, 9) == 4)
+// Run file purging about every 20 cron runs (1h40 on a five minute cron)
+if (mt_rand(1, 20) == 4)
     Cron::CleanOrphanedFiles();
 
 if($cfg && $cfg->isAutoCronEnabled()) { //ONLY fetch tickets if autocron is enabled!
@@ -57,7 +68,5 @@ if($cfg && $cfg->isAutoCronEnabled()) { //ONLY fetch tickets if autocron is enab
 $data = array('autocron'=>true);
 Signal::send('cron', $data);
 
-$_SESSION['lastcroncall']=time();
-endif;
 ob_end_clean();
 ?>
-- 
GitLab