title: XHR request $.Ajax taking to long to respond after while
body: <p>I know it maybe reported as duplicated, but I lost a lot of time (3 weeks) digging online everywhere trying to find a solution..</p>
  
  <p>I'm working on <a href="/questions/tagged/php" class="post-tag" title="show questions tagged &#39;php&#39;" rel="tag">php</a> application for a <a href="/questions/tagged/sip" class="post-tag" title="show questions tagged &#39;sip&#39;" rel="tag">sip</a> - <a href="/questions/tagged/asterisk" class="post-tag" title="show questions tagged &#39;asterisk&#39;" rel="tag">asterisk</a> which allow me to get agents working on it.. and when I run my Application, I had an issue that <a href="/questions/tagged/ajax" class="post-tag" title="show questions tagged &#39;ajax&#39;" rel="tag">ajax</a> Stalled for so long, then after a lot of search, I knew that I have to close sessions using <code>session_write_close();</code> after I put my data into it.</p>
  
  <p>now I have a different situation, The same file takes about 2 sec. in the first I install my Application into my server "<strong>localserver, CentOS</strong>".
  <a href="https://i.stack.imgur.com/lBEaY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lBEaY.png" alt="enter image description here"></a></p>
  
  <p>now and after 2 or 3 weeks, it takes 20 sec!!! and I don't know why?!!!
  <a href="https://i.stack.imgur.com/J5EVl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/J5EVl.png" alt="enter image description here"></a></p>
  
  <ul>
  <li>here's my Ajax code:</li>
  </ul>
  
  <p>.</p>
  
  <pre><code>$("#totagentdetails").click(function () {
          $("#modal-body").html("");
      var queue = $("#queue").val();
  
          $.ajax({
                  type:'POST',
                  url:'getdetails.php',
                  data: {"queue":queue, "asker":"totagents"},
                  dataType: 'json',
                  success: function(response){
                      if(response.res != "error"){
                              $("#modal-title").html("Total agents details:");
                              $("#modal-body").html(response.tbldetails);
                      }else{
                              var result = response.msg;
                              alert(result);
                      }
                   }
              });
  
  });
  </code></pre>
  
  <p>and here's "getdetails.php" code:</p>
  
  <pre><code>&lt;?php
  session_start();
  session_write_close();
  include_once("../functions/main_config.php");
  include_once("../functions/function_mrh.php");
  include_once("../functions/enc_dec.php");
  include('../phpseclib1.0.9/Net/SSH2.php');
  
  $queue = $_POST['queue'];
  $asker = $_POST['asker'];
  
  date_default_timezone_set("Africa/Cairo");
  
  switch ($asker) {
      case "totagents":
      case "readyagents":
      case "hascalls":
      case "waitingcalls":
      case "breaks":
          $command = '/usr/sbin/asterisk -rx "queue show  '.$queue.'"';
          break;
  
      default:
          $command = "error!";
  }
  
  $connection = ssh2_connect($sip_host, 22); 
  ssh2_auth_password($connection,$sip_user,$sip_pass); 
  $stream = ssh2_exec($connection,$command);
  stream_set_blocking($stream, true);
  $output = stream_get_contents($stream);
  $output = rtrim($output);
  $output  = explode("\n", $output );
  
  
  switch ($asker) {
      case "totagents":
          $finalRes = "
          &lt;table class='table table-striped table-bordered table-hover'&gt;
              &lt;thead&gt;
                  &lt;tr&gt;
                      &lt;th&gt;Agent name&lt;/th&gt;
                      &lt;th&gt;Extension&lt;/th&gt;
                      &lt;th&gt;Status&lt;/th&gt;
                      &lt;th&gt;Num. of calls&lt;/th&gt;
                      &lt;th&gt;Total sign in&lt;/th&gt;
                  &lt;/tr&gt;
              &lt;/thead&gt;
              &lt;tbody&gt;
  
          ";
  
  
          foreach($output as $key=&gt;$value) {
              if ($key &gt; 1){
                  if (trim($value) == "No Callers"){
                      break;
                  } elseif (trim($value) == "Callers:"){
                      break;
                  }else{
  
                      @list($agentName, $restOfTheLine) = explode("(SIP/", $value);
                      $finalRes .= "&lt;tr&gt;&lt;td&gt;".trim($agentName) ."&lt;/td&gt;"; 
  
                      @list($Ext, $restOfTheLine) = explode("from", $restOfTheLine);
                      $finalRes .= "&lt;td&gt;".trim($Ext) ."&lt;/td&gt;"; 
  
                      @list($stat, $restOfTheLine) = explode(") has", $restOfTheLine);
                          // get break type if it == "paused) (N"
                          if (strpos(mb_substr($stat, 42, 10), 'paused') !== false) {
                                  $dbh6 = @mysql_connect($app_host, $app_user, $app_pass) or die ("error in host-log connection");
                                          @mysql_select_db($log_database, $dbh6) or die("error in agentsaction database selections"); 
  
                              $qry = "select status from ".$log_database.".livecounter where ext='".trim($Ext)."';";
                              $result = mysql_query($qry);
                              if($result){
                                  $row = mysql_fetch_array($result);
                                  $stat = $row['status'];
                                  include_once("../functions/closelogdb_mrh.php");
                              } else {
                                  $stat = "paused";
                              }
                          } else {
                              $stat = mb_substr($stat, 42, 10);
                              if(trim($stat) == "Unavailabl"){$stat = "Unavailable";}
                              if(trim($stat) == "Not in use"){$stat = "Ready";}
                          }
  
  
                      $finalRes .= "&lt;td&gt;".trim($stat)."&lt;/td&gt;";            
  
                      @list($hasTake, $restOfTheLine) = explode("taken", $restOfTheLine);
                      $hasTake = mb_substr($restOfTheLine, 0, 3);
                          if (trim($hasTake) == "no"){$hasTake = 0;}
  
                      $finalRes .= "&lt;td&gt;".trim($hasTake)."&lt;/td&gt;"; 
  
  
                      //calc inqueuetot from database
  
                                  $dbh6 = @mysql_connect($app_host, $app_user, $app_pass) or die ("error in host-log connection");
                                          @mysql_select_db($log_database, $dbh6) or die("error in agentsaction database selections"); 
  
                              $qry = "select * from ".$log_database.".livecounter where ext='".trim($Ext)."';";
                              $result = mysql_query($qry);
                              if($result){
                                  $row = mysql_fetch_array($result);
                                  $logdatetime = $row['logdatetime'];
                                  $current_datetime = date('Y-m-d H:i:s');
                                  $alllogTime = subTwoDates_2from1($current_datetime, $logdatetime);
                              } else {
                                  $alllogTime = "N/A";
                              }
  
                              $finalRes .= "&lt;td&gt;".$alllogTime."&lt;/td&gt;&lt;/tr&gt;";
                  }
              }
          }
  
  
          $finalRes .= "
              &lt;/tbody&gt;
          &lt;/table&gt;
          ";
          break;
  
      default:
          $command = "error!";
  }
  
  
  
         echo json_encode(array("res" =&gt; "greatJob", "msg" =&gt; $output, "tbldetails" =&gt; $finalRes)); 
  
  
  ?&gt;
  </code></pre>
  
  <p><strong>UPDATE 1:</strong></p>
  
  <p>interesting comment from @RamRaider, but I didn't understand a lot of things, I typed <strong>torch://net-internals/#events</strong> in address-bar then I clicked on <strong>URL_REQUEST</strong>  for the "getdetails.php" file, and it loads a bunch of data:</p>
  
  <pre><code>194691: URL_REQUEST
  https://10.1.80.7/cc/supervisor/getdetails.php
  Start Time: 2018-03-04 13:06:35.765
  
  t=559792 [st=    0] +REQUEST_ALIVE  [dt=20486]
                       --&gt; priority = "MEDIUM"
                       --&gt; url = "https://10.1.80.7/cc/supervisor/getdetails.php"
  t=559793 [st=    1]   +URL_REQUEST_DELEGATE  [dt=6]
  t=559793 [st=    1]      DELEGATE_INFO  [dt=4]
                           --&gt; delegate_blocked_by = "extension IDM Integration Module"
  t=559798 [st=    6]      DELEGATE_INFO  [dt=1]
                           --&gt; delegate_blocked_by = "extension AdGuard AdBlocker"
  t=559799 [st=    7]   -URL_REQUEST_DELEGATE
  t=559799 [st=    7]   +URL_REQUEST_START_JOB  [dt=20466]
                         --&gt; load_flags = 33024 (MAYBE_USER_GESTURE | VERIFY_EV_CERT)
                         --&gt; method = "POST"
                         --&gt; upload_id = "0"
                         --&gt; url = "https://10.1.80.7/cc/supervisor/getdetails.php"
  t=559799 [st=    7]     +URL_REQUEST_DELEGATE  [dt=4]
  t=559799 [st=    7]        DELEGATE_INFO  [dt=2]
                             --&gt; delegate_blocked_by = "extension IDM Integration Module"
  t=559801 [st=    9]        DELEGATE_INFO  [dt=2]
                             --&gt; delegate_blocked_by = "extension IDM Integration Module"
  t=559803 [st=   11]     -URL_REQUEST_DELEGATE
  t=559803 [st=   11]      HTTP_CACHE_GET_BACKEND  [dt=0]
  t=559803 [st=   11]     +HTTP_STREAM_REQUEST  [dt=96]
  t=559803 [st=   11]        HTTP_STREAM_REQUEST_STARTED_JOB
                             --&gt; source_dependency = 194693 (HTTP_STREAM_JOB)
  t=559899 [st=  107]        HTTP_STREAM_REQUEST_BOUND_TO_JOB
                             --&gt; source_dependency = 194693 (HTTP_STREAM_JOB)
  t=559899 [st=  107]     -HTTP_STREAM_REQUEST
  t=559900 [st=  108]      URL_REQUEST_DELEGATE  [dt=0]
  t=559900 [st=  108]     +HTTP_STREAM_REQUEST  [dt=68]
  t=559900 [st=  108]        HTTP_STREAM_REQUEST_STARTED_JOB
                             --&gt; source_dependency = 194697 (HTTP_STREAM_JOB)
  t=559968 [st=  176]        HTTP_STREAM_REQUEST_BOUND_TO_JOB
                             --&gt; source_dependency = 194697 (HTTP_STREAM_JOB)
  t=559968 [st=  176]     -HTTP_STREAM_REQUEST
  t=559968 [st=  176]     +UPLOAD_DATA_STREAM_INIT  [dt=0]
  t=559968 [st=  176]        UPLOAD_DATA_STREAM_INIT  [dt=0]
                             --&gt; is_chunked = false
                             --&gt; net_error = 0 (?)
                             --&gt; total_size = 23
  t=559968 [st=  176]     -UPLOAD_DATA_STREAM_INIT
                           --&gt; is_chunked = false
                           --&gt; net_error = 0 (?)
                           --&gt; total_size = 23
  t=559968 [st=  176]     +HTTP_TRANSACTION_SEND_REQUEST  [dt=1]
  t=559968 [st=  176]        HTTP_TRANSACTION_SEND_REQUEST_HEADERS
                             --&gt; POST /cc/supervisor/getdetails.php HTTP/1.1
                                 Host: 10.1.80.7
                                 Connection: keep-alive
                                 Content-Length: 23
                                 Accept: application/json, text/javascript, */*; q=0.01
                                 Origin: https://10.1.80.7
                                 X-Requested-With: XMLHttpRequest
                                 User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.0.12335 Safari/537.36
                                 Content-Type: application/x-www-form-urlencoded; charset=UTF-8
                                 DNT: 1
                                 Referer: https://10.1.80.7/cc/supervisor/index.php
                                 Accept-Encoding: gzip, deflate, br
                                 Accept-Language: en-US,en;q=0.8,ar;q=0.6
                                 Cookie: [36 bytes were stripped]
  t=559968 [st=  176]        HTTP_TRANSACTION_SEND_REQUEST_BODY
                             --&gt; did_merge = false
                             --&gt; is_chunked = false
                             --&gt; length = 23
  t=559968 [st=  176]       +UPLOAD_DATA_STREAM_READ  [dt=0]
                             --&gt; current_position = 0
  t=559968 [st=  176]          UPLOAD_DATA_STREAM_READ  [dt=0]
                               --&gt; current_position = 0
  t=559968 [st=  176]       -UPLOAD_DATA_STREAM_READ
  t=559969 [st=  177]        UPLOAD_DATA_STREAM_READ  [dt=0]
                             --&gt; current_position = 23
  t=559969 [st=  177]     -HTTP_TRANSACTION_SEND_REQUEST
  t=559969 [st=  177]     +HTTP_TRANSACTION_READ_HEADERS  [dt=20288]
  t=559969 [st=  177]        HTTP_STREAM_PARSER_READ_HEADERS  [dt=20288]
  t=580257 [st=20465]        HTTP_TRANSACTION_READ_RESPONSE_HEADERS
                             --&gt; HTTP/1.1 200 OK
                                 Date: Sun, 04 Mar 2018 11:06:29 GMT
                                 Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.1e-fips PHP/5.4.16
                                 X-Powered-By: PHP/5.4.16
                                 Expires: Thu, 19 Nov 1981 08:52:00 GMT
                                 Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
                                 Pragma: no-cache
                                 Keep-Alive: timeout=5, max=100
                                 Connection: Keep-Alive
                                 Transfer-Encoding: chunked
                                 Content-Type: text/html; charset=utf-8
  t=580257 [st=20465]     -HTTP_TRANSACTION_READ_HEADERS
  t=580257 [st=20465]     +URL_REQUEST_DELEGATE  [dt=8]
  t=580257 [st=20465]        DELEGATE_INFO  [dt=5]
                             --&gt; delegate_blocked_by = "extension IDM Integration Module"
  t=580262 [st=20470]        DELEGATE_INFO  [dt=3]
                             --&gt; delegate_blocked_by = "extension AdGuard AdBlocker"
  t=580265 [st=20473]     -URL_REQUEST_DELEGATE
  t=580265 [st=20473]   -URL_REQUEST_START_JOB
  t=580265 [st=20473]    URL_REQUEST_DELEGATE  [dt=1]
  t=580266 [st=20474]    HTTP_TRANSACTION_READ_BODY  [dt=11]
  t=580277 [st=20485]    URL_REQUEST_JOB_FILTERED_BYTES_READ
                         --&gt; byte_count = 9775
  t=580278 [st=20486]    HTTP_TRANSACTION_READ_BODY  [dt=0]
  t=580278 [st=20486]    URL_REQUEST_JOB_FILTERED_BYTES_READ
                         --&gt; byte_count = 1
  t=580278 [st=20486]    HTTP_TRANSACTION_READ_BODY  [dt=0]
  t=580278 [st=20486] -REQUEST_ALIVE
  </code></pre>