1

So I have a quite expensive and complex PHP process which makes its execution long lasting, lets call it function "expensive_process()".

I have an interface which through a press of a button calls an ajax request to a PHP script which in turn initiates "expensive_process()". Here's the javascript code:

$('#run_expensive_process_button').click( function(){

  var url = "initiate_expensive_process.php";

  $.ajax({
    url: url
  });

});

And initiate_expensive_process.php code:

<?php
  session_start();    

  run_expensive_process();
?>

Simple and trivial. Now the issue with this is that while expensive_process() is running, the browser is losing the ability to navigate the domain. If I refresh the browser window it hangs indefinitely while the process last. If I redirect to a different url under the same domain, same thing. This happens in all browsers. However, if I relaunch the browser (close and open a new window, not a tab), navigation works normally, even though expensive_process() is still running.

I've inspected network traffic, and the HTTP request to initiate_expensive_process.php doesn't get a response while expensive_process() is running, but I'm assuming this shouldn't be locking the browser given the asynchronous nature of the request..

One more thing, which I believe is relevant. This situation is happening on a replica server. On my local machine, where I run WAMP and the same source code, this is not happening, i.e., while expensive_process() is running, I'm still able to navigate the hosting domain without having to relaunch the browser. This seems to be an indication of a server configuration problem of some sort, but I'm not sure I can rule out other possible reasons.

Anyone know what might be causing this or what can be done to figure out the source of the problem?

Thanks

David Neto
  • 809
  • 1
  • 12
  • 20
  • 1
    Are you sure that when you close your browser and open it again you're not getting another sessionId from cookies cuz you browser delete it when you close it? Try to log your session id before launching expensive_process() and then again when you close it and open it. – Lucarnosky Jun 06 '18 at 11:42
  • 2
    maybe some kind of session locking ? have you tried session_write_close(); before run_expensive_process() ? – FatFreddy Jun 06 '18 at 11:54
  • @Lucarnosky I've logged PHP session ids in the first and second browser windows, they're different. Maybe I'm overlooking it, but what does this tell us about the problem? – David Neto Jun 06 '18 at 11:59
  • 1
    @David then you're not making it multithreaded so you'll get stuck until the script finish. BTW multithreading is not a good idea in php if is your first time approaching it – Lucarnosky Jun 06 '18 at 12:02
  • @FatFreddy I've just tried adding session_write_close() inside and before "expensive_process()". Unfortunately the problem remains – David Neto Jun 06 '18 at 12:03
  • @Lucarnosky Could it be that multithreading is disabled on the server where the problem is happening and enabled on my localhost through some PHP.ini setting? Would that make sense? – David Neto Jun 06 '18 at 12:17
  • 1
    Check out https://stackoverflow.com/q/36029431/2191572, do you have enough CPU cores? – MonkeyZeus Jun 06 '18 at 12:19
  • 1
    Can you confirm that `run_expensive_process()` does not re-enable the session? Does `run_expensive_process()` manually place a file lock on something which is needed by the other requests? – MonkeyZeus Jun 06 '18 at 12:21
  • 1
    @DavidNeto Nah man, the point is different: you can't execute another script until it finish, that's how PHP work, you can try to create temporary session with AJAX and make it work while you're doing stuff but, again, as mentioned before is not a good idea if you never approach it. So my final answer is that you can't do that, you have to wait your expensive_process finish. – Lucarnosky Jun 06 '18 at 12:32

2 Answers2

1

Most likely the other PHP scripts also session variables. Only one script process can access a session at a time; if a second script tries to access the session while the first script is still running, it will be blocked until the first script finishes.

The first script can unlock the session by calling session_write_close() when it's done using the session. See If call PHP page via ajax that takes a while to run/return (and it sets session variables), will a 2nd ajax call see those session changes? for more details about how you can construct the script.

Barmar
  • 741,623
  • 53
  • 500
  • 612
-1

I wonder whether it might be due to ajax. The javascript is being executed client-side. Maybe you might consider a stringified JSON call instead of ajax?

RAF
  • 1
  • 1