If a module adds anything to $_SESSION on hook_exit, it results in a fatal error:

PHP Fatal error: session_start(): Failed to initialize storage module

The error is supressed in the browser since drupal_exit is called after the Location header is sent, but it shows up in the error log.

Attached is a module to replicate the problem and a potential fix.

Comments

neclimdul’s picture

subscribe

mfb’s picture

By the way this is related to a Drupal 6 issue: #500680: Fatal error on logout

Another way to work around the "failed to initialize storage module" PHP bug might be to reset the session handlers after destroying the session? i.e. session_set_save_handler('_drupal_session_open', '_drupal_session_close', '_drupal_session_read', '_drupal_session_write', '_drupal_session_destroy', '_drupal_session_garbage_collection'); This is assuming we do want to allow the session to be saved after logout, which perhaps some contrib module needs to do.

The suggested fix of emptying the session after logout might be too brittle, what if some other path is used to destroy the session?

mfb’s picture

Status: Active » Needs review
StatusFileSize
new2.41 KB

Here's the alternate potential fix. We make a new wrapper function, drupal_session_destroy() to workaround http://bugs.php.net/bug.php?id=32330

This allows contrib modules to save stuff to the session even after logout.

msonnabaum’s picture

I like this approach much better. Works well in my testing.

Btw, I ran into this initially using Pressflow and ubercart. It's a pretty rare scenario, but uc_store can end up writing to the session in hook_exit.

More details on the launchpad thread for those interested: https://bugs.launchpad.net/pressflow/+bug/513117

neclimdul’s picture

StatusFileSize
new1.56 KB

d6 port for pressflow et al.

alexpott’s picture

Just used the patch in #5 on a pressflow site using memcache and I needed to add the new function drupal_session_destroy() to the memcache-session.inc in order to get it to. Once done it works a treat. Thanks.

c960657’s picture

This problem was also discussed in #477944: Fix and streamline page cache and session handling, though apparently it wasn't fixed completely back then (or the problem has been reintroduced in the meantime).

Would it be sufficient to move the session_set_save_handler() call from drupal_session_initialize() to drupal_session_start()?

mfb’s picture

I think that might work, and would be a nicer fix. Unfortunately I can't test right now as I since upgraded the site where I reproduced this to PHP 5.3 (which doesn't suffer from the PHP bug).

The problem was mostly fixed by setting $_SESSION to an empty array, aside from this edge case.

msonnabaum’s picture

StatusFileSize
new730 bytes

Here's a patch for the session_test module to expose this bug in simpletest.

Status: Needs review » Needs work

The last submitted patch, session_test_module_hook_exit.patch, failed testing.

mfb’s picture

Status: Needs work » Needs review
StatusFileSize
new3.55 KB

Here's a shorter patch as suggested in #7 and a test that may trigger the fatal error (depending on server environment)

thedavidmeister’s picture

Status: Needs review » Needs work

Patch in #12 no longer applies:

error: session.inc: No such file or directory
error: simpletest/tests/session.test: No such file or directory

dcam’s picture

Issue summary: View changes
Issue tags: +Needs reroll, +Novice

My guess is that this error doesn't occur in 8.x, but that should probably checked. Isn't session handling done by Symfony?

Regardless, #12 needs to be rerolled for 7.x.

moymilo’s picture

Status: Needs work » Needs review
StatusFileSize
new2.99 KB

Reroll #12 for 7.x.

Status: Needs review » Needs work

The last submitted patch, 15: base-system-session-758730-15.patch, failed testing.

moymilo’s picture

Status: Needs work » Needs review
StatusFileSize
new3.06 KB

Status: Needs review » Needs work

The last submitted patch, 17: base-system-session-758730-17.patch, failed testing.

moymilo’s picture

StatusFileSize
new2.98 KB
moymilo’s picture

Status: Needs work » Needs review

The last submitted patch, 5: session_destroy-d6.patch, failed testing.

moymilo’s picture

Issue tags: -Needs reroll

Status: Needs review » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.