29

For some odd reason, just today our server decided to be very slow during the starting of sessions. For every session_start, the server either times out after 30 seconds, or it'll take about 20 seconds for it to start the session. This is very weird, seeing as it hasn't done this for a very long time (the last time our server did this was about 7 months ago). I've tried to change the session to run through a database instead, and that works fine, however, as our current website is built, it'd take days to go on every page and change the loading of sessions to include a new session handler. Therefore my question remains:

Why is it so slow, and why only sometimes?

We run on a dedicated hetzner server with 24GB's of ram, and a CPU fast enough to just run a simple webserver (a Xeon, I believe, but I'm not sure). We run debian on the server with an apache+fastcgi+php5 setup.

The server doesn't report much load, neither through server-status as well as the top command. Vnstat reports no problem whatsoever with our network link (again, that wouldn't result in a slow local session handling). IOtop reports no problem with processes taking over the entire harddrive. Writing to the tmp folder where the session files are located works fast if done through vim.

Again, to make this clear, my main concern here isn't whether or not we should switch to a DB or a memory-cached version of the sessions, it's simply to ask why this happens, because everything I take a look at seems to be working fine, except for the PHP itself.

EDIT: The maximum file in our PHP tmp directory is 2.9 MB, so nothing that should make an impact, I believe.

UPDATE: I did never figure out what was wrong and/or how to fix it, but the problem disappeared after we switched over to memcached/db sessions.

h2ooooooo
  • 36,580
  • 8
  • 61
  • 97
  • 2
    Have a look in your `tmp` directory. PHP stores it's sessions there. See if anything is amiss. – Tom Hallam Apr 30 '12 at 14:46
  • 1
    Something wrong with the disk or the filesystem perhaps? – Jon Apr 30 '12 at 14:47
  • 12
    "it'd take days to go on every page and change the loading of sessions to include a new session handler" If that's the case you seriously should consider fixing that fact first – PeeHaa Apr 30 '12 at 14:48
  • 1
    RepWhoringPeeHaa @ As I mentioned, I'd definitely do that. I've been moved into the system with my job, so unfortunately I haven't created the session management that the application has had. It's not been something that we've been needed to fix either, since, as mentioned, there's been no problem for over 7 months. – h2ooooooo Apr 30 '12 at 15:01
  • Were there any changes done recently that you think are forcing this behavior ? Can you look at the last part of the log files from yesterday and see if something indicating trouble was logged ? – happybuddha Mar 21 '13 at 13:28
  • @happybuddha As I've updated in the OP, we ended up switching over to memory cached sessions instead after it happened yet again. No changes were made beforehand, and the problem randomly (at least it seems random) disappears again. (Note, this question is 11 months old). – h2ooooooo Mar 21 '13 at 13:30
  • @h2ooooooo : > (Note, this question is 11 months old). SMH ! – happybuddha Mar 21 '13 at 13:34
  • Related http://stackoverflow.com/q/13772074/168034 – phunehehe Sep 22 '14 at 09:22

6 Answers6

18

Have you tried session_write_close(); ? This will disable write-ability in session variables but you can still read data from them. And later when you need to write a session variable, reopen it.

I have also suffered from this problem but this thing worked like a charm. This is what i do:

session_start(); //starts the session
$_SESSION['user']="Me";
session_write_close();   // close write capability
echo $_SESSION['user']; // you can still access it
Kush
  • 695
  • 7
  • 22
6

I had the same problem: suddenly the server took 30 seconds to execute a request. I noticed it was because of session_start(). The first request was fast, but each next request took some 30 sec to be executed. I found that the session file in c:\wamp\tmp was locked by the first request for some 30 sec. During this time the second request was waiting for the file to be unlocked. I found out it had something to do with rewrite_mod and .htaccess. I disabled rewrite_mod and commented out every line in .htaccess and it works again like a charm. I don't know why this happend because I don't remember change any settings or conf on wamp.

user2929078
  • 99
  • 1
  • 6
2

I ran into this problem too. It was answered here:

Problem with function session_start() (works slowly)

Sessions are locked by PHP while one script is executing, so if scripts are stacked under the same session, they can cause these surprisingly long delays.

Community
  • 1
  • 1
phphelp
  • 21
  • 2
  • I wish this would've simply fixed it, but it's just weird how it happened to EVERY user on the site (including access from our office on different computers), if it's simply a locked session file. Maybe it might be because we used text files, and therefore it used the **same** file, that was locked? – h2ooooooo Apr 15 '13 at 08:17
0

Each session is stored by apache as a text file.

When session start is use to resume an existing session (via cookie identifier for example) maybe a big session file (a Session with a lot of content inside) can be slow to be started?

If this is the case probably you application is putting to much data into sessions.

ab_dev86
  • 1,864
  • 16
  • 21
0

Please check if you have correct memcache settings e.g. in /etc/php.d/memcached.ini

vikramaditya234
  • 1,038
  • 1
  • 13
  • 30
0

I know this is an old question but I've just fixed this issue on my server. All I did was turn on the bypass cache for the domains in the cache manager in the cpanel.

My sessions were taking ages to start and close now they are instant.

user1432290
  • 157
  • 1
  • 3
  • 12