Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Bug] Strange session behaviour #6886

Closed
@kotsis

Description

@kotsis

I have managed to find a session bug that is easily reproducible.

First you have to enalbe database sessions, i did so by following this guide http://symfony.com/doc/current/cookbook/configuration/pdo_session_storage.html

Then you can create 2 actions like this:

/**
    * @Route("/lol", name="myroute_lol")
    * 
    */
    public function lolAction()
    {
    $this->container->get('session')->set('testsess', '[email protected]');

    //send an email
    $message = \Swift_Message::newInstance()
        ->setSubject('Test subject')
        ->setFrom('[email protected]')
        ->setTo('[email protected]')
        ->setBody($this->renderView('MyBundle:Default:mail.txt.twig', array('body' => 'some body text')))
    ;
    $this->get('mailer')->send($message);

            return $this->redirect($this->generateUrl('myroute_lol2'));
    }

/**
    * @Route("/lol2", name="myroute_lol2")
    */
public function lol2Action()
    {
    $email = $this->container->get('session')->get('testsess');
        return new Response('email:'.$email);
    }

If you clear you browser cookies then each time you go to the /lol url then you get redirected to /lol2 and the session returns an empty string for session variable 'testsess'
If you refresh /lol2 then the second time it works and shows the correct value.

It has something to do with mail. If you comment out the line where the send() happens then all is ok. It has something to do with not writing the session in the database after the completion of the execution of the action.

I have debugged the swift mail bundle a bit and it seems that when send() is called the mail is saved in memory (spool memory, uses class Swift_MemorySpool), so i suppose mails are sent after the completion of the action, after the action returns, somewhat asynchronously i suppose.

It seems that something happens in the code after the action returns a redirect.
If you change the redirect and simply output something then the session is ok.

This bug has 3 conditions, using mysql sessions, sending an email and then redirecting to another action.
It is reproducible. I hope i didn't cause confusion.

Thank you.

P.S. using symfony 2.1.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions