Bug Report #2957

Make sure Session::write is called last

Added by Israel Canasa over 4 years ago. Updated almost 4 years ago.

Status:ClosedStart date:06/12/2010
Priority:NormalDue date:
Assignee:Woody Gilk% Done:

100%

Category:Core
Target version:v3.0.10
Resolution:fixed Points:

Description

Unexpected behavior may arise when using register_shutdown_function and if the function called by register_shutdown_function uses the Session.

This is apparent when I use the Captcha module. When Captcha class is instantiated, it calls register_shutdown_function to run Captcha::update_response_session which calls the Session::instance()->set().

The problem arises when the Session is instantiated before the Captcha. For example, if I use Auth(which calls the Session) first, Session::write will be registered before Captcha::update_response_session causing the Session::instance()->set() attempt to fail.

This problem may be solved by making sure that Session::write is called last. Or by alerting developers to take this issue in consideration and avoid calling Session::instance()->get() on their register_shutdown_function calls.

A dirty fix in making sure Session::write is called last would be something like this:

public static function instance($type = NULL, $id = NULL)
    {
        if ($type === NULL)
        {
            // Use the default type
            $type = Session::$default;
        }

        if ( ! isset(Session::$instances[$type]))
        {
            // Load the configuration for this type
            $config = Kohana::config('session')->get($type);

            // Set the session class name
            $class = 'Session_'.ucfirst($type);

            // Create a new session instance
            Session::$instances[$type] = $session = new $class($config, $id);

            // Make sure the Session::write is the last to be called on shutdown
            register_shutdown_function(array('Session', 'prepare'), $session);
        }

        return Session::$instances[$type];
    }

    public static function prepare($session)
    {
        // Write the session at shutdown
        register_shutdown_function(array($session, 'write'));
    }

Instead of registering write, register a prepare function first which will insert the Session::write to the end of the queue.


Related issues

Related to Kohana v3.x - Bug Report #3454: Session is not closed prior to writing files, can cause d... Closed 12/03/2010

History

#1 Updated by Woody Gilk over 4 years ago

  • Target version changed from v3.0.7 to v3.0.8

#2 Updated by Woody Gilk about 4 years ago

  • Target version changed from v3.0.8 to v3.0.9

#3 Updated by Woody Gilk almost 4 years ago

  • Target version changed from v3.0.9 to v3.0.10

#4 Updated by Woody Gilk almost 4 years ago

  • Status changed from New to Feedback

I think closing #3454 also closes this. In fact, the fix for #3454 probably makes using register_shutdown_function completely unnecessary.

#5 Updated by Woody Gilk almost 4 years ago

  • Status changed from Feedback to Closed
  • % Done changed from 0 to 100
  • Resolution set to fixed

Fix for #3454 fixes this in a more elegant way.

Also available in: Atom PDF