Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: [Session] Redis - write_close_session #7695

Closed
gabriel-joy opened this issue Jul 11, 2023 · 12 comments
Closed

Bug: [Session] Redis - write_close_session #7695

gabriel-joy opened this issue Jul 11, 2023 · 12 comments
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@gabriel-joy
Copy link

PHP Version

8.2

CodeIgniter4 Version

4

CodeIgniter4 Installation Method

Manual (zip or tar.gz)

Which operating systems have you tested for this bug?

Linux

Which server did you use?

apache

Database

No response

What happened?

switching session filehandler to redis handler works as exepected as long as I'm not using session_write_close.

calling session_write_close in the same script works fine but in ajax calls I get an empty session.

after session_write_close, session->destroy() generate an exception as well

Steps to Reproduce

  1. switch to redis file handler
    set in
    public $sessionDriver = 'CodeIgniter\Session\Handlers\RedisHandler';
    public string $sessionSavePath = 'tcp://localhost:6379?database=10'

set session variable in main script
call session_write_close()
3. make an ajax call and check session variables.

Expected Output

to work like file handler

Anything else?

No response

@gabriel-joy gabriel-joy added the bug Verified issues on the current code behavior or pull requests that will fix them label Jul 11, 2023
@kenjis
Copy link
Member

kenjis commented Jul 11, 2023

What is the exact error message?
How did you make an ajax call?

It would be helpful if you could show a minimal working sample code to reproduce the error.

@kenjis kenjis changed the title Bug: Redis - write_close_session Bug: [Session] Redis - write_close_session Jul 11, 2023
@gabriel-joy
Copy link
Author

gabriel-joy commented Jul 11, 2023

Not exactly my case but very close:

Just create 2 functions in your controller and call one after the other in browser (or uncomment the redirect line):
With redis the session simply not working.

public function debug1()
	{
		
		$session = \Config\Services::session();
		$session->start();
		
		$session->set("test","hola");
		session_write_close();
		
		$session = \Config\Services::session();
		$session->start();
		print_r($_SESSION, false);
		session_write_close();

           //return redirect()->redirect( base_url('home/debug2'));
	}

	public function debug2()
	{
		
		$session = \Config\Services::session();
		$session->start();
		
		print_r($_SESSION, false);
		session_write_close();
	}

@ping-yee
Copy link
Contributor

ping-yee commented Aug 24, 2023

Out off the topic, how do I make sure the session store the data into Redis is working?

Because all operations of the session are performed normally after I set up the redisHandler in Config, but I find that there is no data stored in Redis.

@gabriel-joy
Copy link
Author

https://stackoverflow.com/questions/19257573/how-do-i-know-if-sessions-are-successfully-being-managed-by-redis

@kenjis
Copy link
Member

kenjis commented Sep 8, 2023

@gabriel-joy Try #7887

@gabriel-joy
Copy link
Author

Yes, it's working as expected.

Do you know when this fix will be published in the official release?
Thank you.

@kenjis
Copy link
Member

kenjis commented Sep 8, 2023

@gabriel-joy Thank you for testing!
The fix will be included in v4.4.2. It would be released in the next month.

@kenjis kenjis closed this as completed Sep 8, 2023
@gabriel-joy
Copy link
Author

gabriel-joy commented Sep 11, 2023

@kenjis There is a performance issue with this fix.
In my app I have a complex dialog with multiple ajax calls, with REDIS I have Session read length:1.0053s. With FILES I have Session read length:0.0058s. Otherwise, everything else works fine.
Therefore, the fix is partial, something is still missing. Sorry :(

My guess is the session is locked and all calls are sequential, session_write_close() not closing the session.

@kenjis
Copy link
Member

kenjis commented Sep 11, 2023

The performance issue is really because of the fix?

@gabriel-joy
Copy link
Author

how could I know? I will open a new bug and hopefully someone will find out.

@kenjis
Copy link
Member

kenjis commented Sep 12, 2023

@gabriel-joy
Copy link
Author

gabriel-joy commented Sep 12, 2023

I updated the script for performance tests, it seems that performance is not affected. Bot requests are executed in parallel not waiting for each other.
I assume for the moment my findings were related to network conditions (mobile connection) and not to REDIS handler.

Here is the updated test code:

public function debug1()
	{
		$benchmark = \Config\Services::timer();
		$benchmark->start('read session1');
			
		$session = \Config\Services::session();
		$session->start();
		
		$session->set("test","hola");
		session_write_close();
		
		sleep(5);
		
		$session = \Config\Services::session();
		$session->start();
		print_r($_SESSION, false);
		session_write_close();
		
		$benchmark->stop('read session1');
		
		$timers = $benchmark->getTimers();
		echo('Session read length:'.$timers['read session1']['duration'].'s');
	}

	public function debug2()
	{
		$benchmark = \Config\Services::timer();
		$benchmark->start('read session2');
		
		$session = \Config\Services::session();
		$session->start();
		
		print_r($_SESSION, false);
		session_write_close();
		
		$benchmark->stop('read session2');
		$timers = $benchmark->getTimers();
		echo('Session read length:'.$timers['read session2']['duration'].'s');
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

No branches or pull requests

3 participants