Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jun 2010
    Posts
    278
    Thanks
    63
    Thanked 8 Times in 8 Posts

    Race hazard within a session?

    Is it possible for a race hazard to exist within (i.e. not between) a session? Or does session_start(); block all subsequent calls until the script has finished?

    For example, consider the following arbitrary piece of javascript:

    Code:
    ...
    sendAjaxRequest('myscript.php?x=flip');
    sendAjaxRequest('myscript.php?x=flop');
    ...
    Of course, the second call above will not block while myscript is executing in response to the first call.

    Now, suppose myscript.php looks like this:

    PHP Code:
    session_start();

    for (
    $i=0$i<100$i++)
      
    $_SESSION[$i] = myComplicatedFunction($_REQUEST[x]); // *** BANG!!! *** 
    Where I've made the comment "BANG" above, there's the danger of a collision occuring. Or is there? Will the call to session_start block silently upon the second call to myscript.php until the first instance of myscript.php has terminated? Or will I have to set a mutex at the start and end of myscript? (e.g. get a lock on a file that is unique to the session).
    Last edited by XmisterIS; 11-01-2013 at 04:49 PM.

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,090
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Sessions are thread safe. If you call a page while another request is doing it, the second request will not complete until the first one has finished. When the first finishes, the second requests call to session_start will continue.

    It's easy to test, create a script on your localhost, set the time limit to a couple of minutes, print something to the browser and then sleep it for a minute. Open one tab / browser to call it and then open another to that same url and you'll see that nothing happens in the second browser / tab (sharing the same session) until the first one has completed.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • Users who have thanked tangoforce for this post:

    XmisterIS (11-04-2013)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •