{"issue":{"pull_request":{"diff_url":null,"html_url":null,"patch_url":null},"state":"open","milestone":null,"user":{"avatar_url":"https://secure.gravatar.com/avatar/3e6a0fb6566eda3a3b3e8646b39a15ef?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"3e6a0fb6566eda3a3b3e8646b39a15ef","url":"https://api.github.com/users/mmucklo","login":"mmucklo","id":245122},"updated_at":"2011-11-10T10:38:22Z","assignee":null,"labels":[],"closed_at":null,"comments":3,"url":"https://api.github.com/repos/symfony/symfony/issues/2591","body":"We seem to have encountered a case where PdoSessionStorage, or a similar class may not save to DB / storage at the end of the request.\r\n\r\nWe've worked around it, however I'm posting this issue at the request of someone on the newsgroup.\r\n\r\nWe have a derivative of NativeSessionStorage who's start() method is identical (+/- whitespace) to PdoSessionStorage.php\r\n\r\n```php\r\n    /**\r\n     * Starts the session.\r\n     */\r\n    public function start()\r\n    {\r\n        if (self::$sessionStarted) {\r\n            return;\r\n        }\r\n\r\n        // use this object as the session handler\r\n        session_set_save_handler(\r\n            array($this, 'sessionOpen'),\r\n            array($this, 'sessionClose'),\r\n            array($this, 'sessionRead'),\r\n            array($this, 'sessionWrite'),\r\n            array($this, 'sessionDestroy'),\r\n            array($this, 'sessionGC')\r\n        );\r\n\r\n        parent::start();\r\n    }\r\n```\r\n\r\nWe then more or less implement sessionWrite, sessionRead, etc.\r\n\r\nThe problem is that sessionWrite in certain circumstances doesn't seem to be called at the end of the request, resulting in variables not written to our session storage.\r\n\r\nWhat we see is :\r\n\r\n1) PdoSessionStorage(or Equivalent)->sessionWrite()  (called by PHP save_handler routine)\r\n2) Session->__destruct\r\n3) Calls Session->save()\r\n4) Calls PdoSessionStorage(NativeSessionStorage)->write()\r\n5) $_SESSION updated\r\n6) end of request. (updated $_SESSION never saved)\r\n\r\n\r\nWhat actually needs to happen:\r\n\r\n1) Session->__destruct\r\n2) Calls Session->save()\r\n3) Calls PdoSessionStorage(NativeSessionStorage)->write()\r\n4) $_SESSION updated\r\n5) PdoSessionStorage(or Equivalent)->sessionWrite() \r\n6) end of request.\r\n\r\nWe've hacked around it, but I think the problem is that PHP is calling the save handler before __destruct, and doesn't call it afterwards as perhaps it's cleared or something (don't know internally the order of things).\r\n\r\nOur hack extends Session and implementing a __destruct such as below, and using this as the session handler instead by overriding it in a custom session.xml file similar to this:\r\n\r\n```php\r\nnamespace Us\\OurBundle\\Session;\r\n\r\nclass HackedSession\r\n\textends \\Symfony\\Component\\HttpFoundation\\Session\r\n{\r\n    public function __destruct()\r\n    {\r\n        parent::__destruct();\r\n        if (true === $this->started && !$this->closed) {\r\n            if(method_exists($this->storage, 'sessionWrite'));\r\n            $this->storage->sessionWrite($this->getId(), session_encode());\r\n        }\r\n    }\r\n}\r\n```\r\n\r\nsession.xml:\r\n\r\n```xml\r\n\t<parameters>\r\n                 <!-- ... -->\r\n\t\t<parameter key=\"session.class\">Us\\OurBundle\\Session\\HackedSession</parameter>\r\n\t</parameters>\r\n```\r\n\r\nThen load this session.xml under our DependencyInjection class using $loader->load...\r\n\r\nAnyway, I'm not sure this is the best way to fix the problem but would appreciate input.\r\n\r\n","html_url":"https://github.com/symfony/symfony/issues/2591","number":2591,"title":"Possible PdoSessionStorage issue","id":2193218,"created_at":"2011-11-10T00:10:08Z"},"action":"created","comment":{"user":{"avatar_url":"https://secure.gravatar.com/avatar/14b567df9276f4bdb5acbcbc72b03bec?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"14b567df9276f4bdb5acbcbc72b03bec","url":"https://api.github.com/users/drak","login":"drak","id":251852},"updated_at":"2011-11-10T10:38:21Z","url":"https://api.github.com/repos/symfony/symfony/issues/comments/2693768","body":"Somewhere in your application, you should `register_shutdown_function('session_write_close');` and this should solve the issue.  That will ensure the session is closed out properly.","id":2693768,"created_at":"2011-11-10T10:38:21Z"}}