4

session must be synchronized. When A writes/saves to session B or another A is put on wait. Which is a must for file based session handling.

But once A has loaded the session (but now saved its modifications) B should also be allowed to load the same session. because loading will open the file and bring the file contents in memory and close.

Is there any reason to block all other scripts during the whole time A loads session and A saves the session. can't the synchronization be done only with save handler ?

So two PHP scripts can never work concurrently If they share the same session.

for example seslock.php

<?php
header('Content-Type: text/plain');
session_start();
if(isset($_GET['wait'])){
    sleep(30);
    echo "waiting\n";   
}else{
    echo "No Waiting\n";
}
?>
done

visit seslock.php will respond immediately but seslock.php?wait will take 30 seconds to respond. But the problem is if you request seslock.php?wait first and only seslock.php second. event the non-sleep block will ask you to wait for 30 seconds too.

Why it block is not my question. I am asking why it blocks start to save ? instead of blocking only save ?

Neel Basu
  • 12,638
  • 12
  • 82
  • 146

2 Answers2

6

Possible duplicates:

How does session_start lock in PHP?

Why does session_start cause a timeout when one script calls another script using curl

session_start hangs

How to kill a PHP session?

... And many ;)

Edit

  1. The reason why it blocks is because the session file is being read and it might be modified at any point of time when the first script is running, hence the lock.

  2. The remedy to this could be a session_write_close() as pointed by this post

  3. How to prevent blocking php requests, by Konr Ness

Community
  • 1
  • 1
Lionel Chan
  • 7,894
  • 5
  • 40
  • 69
  • really ? I didn't know. But whats the remedy ? This way two php scripts sharing the session can never work concurrently. one has to wait other to finish. and What I am asking Why PHP is designed to block on `start to save` why not on only `save` ? – Neel Basu Jun 25 '12 at 11:23
  • What do you mean by `start to save` and `save`? I don't get it – Lionel Chan Jun 25 '12 at 11:46
  • why session start handler blocks. blocking only session save handler is sufficient for serialization and synchronization – Neel Basu Jun 25 '12 at 12:02
  • @Neel Basu: no, that isn't enough. Say a value is 1: one process sets it to 2, another to 3, which one wins then if they are simultaneous? If you don't care a lick about the reliability of data in your session, you can write your own non-blocking handler with `session_set_save_handler`. – Wrikken Jun 25 '12 at 23:20
0

The default PHP session handler is made to serialise session changes for each session id. This has the benefit of a guaranteed consistent session state across your scripts.

You can give up this advantage by running session_write_close() right after session_start(). This also makes your session read-only though.

Alternatively you can write your own session handler without locking.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309