Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should session_write_close be used in long polling?

I was reading an article about long polling at Nolithius. Under the section PHP sleeps across the entire session, it is written that the session_write_close function should be called in order to prevent the entire session coming to a deadlock. What exactly is meant by deadlock here? Does it mean that without this function, any other page from the same domain opened in the client side won't be able to receive AJAX data from other scripts (like this one) until this one has finished executing and returned the result? Why should this happen? And how can session_write_close help here? Won't using it remove all personalization from the client side the next time he requests a page from this domain after he has received data from this request?

like image 543
SexyBeast Avatar asked Sep 13 '12 07:09

SexyBeast


1 Answers

This is my understanding:

When file based sessions are used each request literally locks the file until the end of the request.

Meaning that the next request (that also uses the session data) has to wait for the lock to be released.

This is used to avoid session data corruption.

Using session_write_close() will clean up (but not lose any session data) and release the lock earlier on the file allowing other requests to continue.

This is good practice especially if you have scripts that sleep a lot, probably.

like image 138
zaf Avatar answered Sep 21 '22 06:09

zaf