From: Michael Witrant Date: 2001-12-13T03:39:41+09:00 Subject: [ruby-talk:28361] CGI::Session::FileStore and flock problem Hello, In cgi/session.rb, CGI::Session::FileStore uses IO#flock to lock the session file while it's restoring it. But: 1. it doesn't unlock the file when done. So the next #restore will wait forever. 2. FileStore#update doesn't lock the file. So #update could change the file while it's being restored. 3. flock isn't thread safe ([ruby-talk:16818]) and I think mod_ruby uses threads to handle multiple requests. Could this be a problem? See below for a patch that: 1. change #restore's flock to LOCK_SH instead of LOCK_EX 2. unlock when #restore is done 3. LOCK_EX and unlock on #update What do you think? Mike. --- session.old Mon Jun 4 10:59:56 2001 +++ session.rb Wed Dec 12 19:29:59 2001 @@ -122,23 +122,32 @@ def restore unless @hash @hash = {} - @f.flock File::LOCK_EX - @f.rewind - for line in @f - line.chomp! - k, v = line.split('=',2) - @hash[CGI::unescape(k)] = CGI::unescape(v) + @f.flock File::LOCK_SH + begin + @f.rewind + for line in @f + line.chomp! + k, v = line.split('=',2) + @hash[CGI::unescape(k)] = CGI::unescape(v) + end + ensure + @f.flock File::LOCK_UN end end @hash end def update - @f.rewind - for k,v in @hash - @f.printf "%s=%s\n", CGI::escape(k), CGI::escape(v) + @f.flock File::LOCK_EX + begin + @f.rewind + for k,v in @hash + @f.printf "%s=%s\n", CGI::escape(k), CGI::escape(v) + end + @f.truncate @f.tell + ensure + @f.flock File::LOCK_UN end - @f.truncate @f.tell end def close