session does not behave as expected

i’m trying to use the session (currently set to use dbSession) to communicate between a long running process and an ajax request for progress and i can only make it work one direction.

in my process i have the following:




                    if( Yii::$app->session['cancel-processing'] )

                    {

                        // cancels download

                        error_log( 'download canceled' );

                        return 1;

                    }


                    $new_progress = round( $downloaded / $download_size * 100 );


                    if( $progress != $new_progress )

                    {

                        $progress = $new_progress;

                        error_log( 'progress: '.$progress );


                        Yii::$app->session['processing-progress'] = $progress;

                        Yii::$app->session->close();

                    }



this works in setting the ‘processing-progress’ session variable which is retrievable via my ajax request. however in a different ajax request i set the ‘cancel-processing’ session variable and it never works.




        Yii::$app->session['cancel-processing'] = true;

        Yii::$app->session->close();



i have tried various combinations of session->open, session->close and session->destroy. there is no way i can force it to write and save to the session in my ajax post and then have that session variable accessible in my long running php script. how else am i supposed to communicate with this process without using file persistence or my own database table?

it appears that one of the processes overtakes the other unpredictably. generally the long running process, which is constantly updating the session, prevents the ajax process from actually writing to the session. why would this possibly be expected behavior? also: why do i have to close the session for the variable to be written? its just using the database. is it not expected behavior for it to write to the database as soon as the session variable is set? frustration level to the max.

Maybe you have a problem with this approach. Let’s consider the following scenario:

User comes on your website and wants to start long running processes. In the first web-browser tab user starts Process A and your process function is executed. In order to get current progress of specific process, you are calling ajax function to check it.

After some time T, Process A progress is 20%, and while waiting, user opens second tab and starts another Process B. Because Process B just started your Yii::$app->session[‘processing-progress’] will be set to let’s say 2%, but what happened is that progress for Process A is now overwritten in the session. This means that in the tab where user started Process A, progress will be equal to 2% which is wrong.

The worse scenario is when your Yii::$app->session[‘cancel-processing’] is set, then all your other running processes will be also canceled.

Your session key values should be unique per process ID, so you can track the progress of individual long running process.

If I understood you wrong please ignore my post.

sure, but this problem is irrelevant to the behavior i’m experiencing and fixing the problem as you describe would still not function as expected. the problem is that the session variables are not being properly saved. one process is stomping on another process’s attempt to set the session variable. given this unpredictable behavior i wouldn’t recommend using session variables for ANYTHING since an unlikely but possible concurrency issue would prevent them from functioning - as i have experienced.

in my case, there can be only one of these long processes per user anyway so your “problem” doesn’t really apply. thanks anyway.

i didn’t want to create a DB table just for this process but in the end that was the only way i could find to solve this without using third party process communication tools. oh well. it’s not the worst way to do it.