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

Avoid locking the php session #32162

Merged
merged 3 commits into from
Aug 23, 2022
Merged

Avoid locking the php session #32162

merged 3 commits into from
Aug 23, 2022

Conversation

juliusknorr
Copy link
Member

@juliusknorr juliusknorr commented Apr 26, 2022

Slightly different approach about has the same target as #29356 to reduce the time that request might need to wait for locked sessions.

We already try to write and close the session early but on multiple concurrent requests that is still not fast enough to not have noticeable wait time on the lock. To my knowledge we don't write anything into the session that would be critical in terms of using already read value that has been updated by another request, so I think this approach should be fine from the potential race condition perspective, especially with reopening the session we would read again.

Impact on page load can be tested by adding a sleep for specific routes:

diff --git a/lib/private/Session/Internal.php b/lib/private/Session/Internal.php
index 1dfec8d71e..db9002f8be 100644
--- a/lib/private/Session/Internal.php
+++ b/lib/private/Session/Internal.php
@@ -233,5 +233,8 @@ class Internal extends Session {
                        $sessionParams['read_and_close'] = $readAndClose;
                }
                $this->invoke('session_start', [$sessionParams], $silence);
+               if (strpos(\OC::$server->getRequest()->getRequestUri(), '/ocs/v2.php/search/providers') === 0) {
+                       sleep(10);
+               }
        }
 }

Currently every request writes to the session

$session->set('LAST_ACTIVITY', time());
so we always need to at least open once for reading and that write, so second commit implements what was described in #29356 but requires that we do no longer invalidate sessions on the Nextcloud side, but rely on the PHP garbage collection for it.

I decided to go for a config flag for this, as for most setups having a session timeout that is happening at the exact second might not be really needed. If that is not used, PHP may clean up the session at a later point depending on the session garbage collection configuration. If using redis, the session lifetime will be set anyways on redis, so redis would take care of the deletion using configured key eviction policy.

lib/base.php Outdated Show resolved Hide resolved
@juliusknorr
Copy link
Member Author

Needs extra care for talk which seems to have issues with no session locking and there this PR could then lead to similar problems where the one session might be lost if the other one writes

@PVince81
Copy link
Member

any connection with #31286 ?

@juliusknorr juliusknorr force-pushed the enh/session-reopen branch 2 times, most recently from 74d3fbc to f8650f3 Compare August 15, 2022 20:54
@juliusknorr
Copy link
Member Author

juliusknorr commented Aug 15, 2022

Tested with the Talk edge case of joining a public room with a different user and seems to work.

Further cases to test:

  • Files mobile client Android
  • Talk mobile client Android
  • Talk call and chat between user 1 (web), user 2 (android), public guest link (web)
  • Log and see how many reopening are happening

@juliusknorr juliusknorr changed the title RFC: Reopen sessions if we need to write to them Avoid locking the php session Aug 16, 2022
@juliusknorr juliusknorr marked this pull request as ready for review August 16, 2022 11:56
@juliusknorr
Copy link
Member Author

Some screenshots for comparison with the sleep added on master and this branch:

Screenshot 2022-08-16 at 13 42 00

Screenshot 2022-08-16 at 13 44 06

lib/base.php Outdated Show resolved Hide resolved
Copy link
Member

@PVince81 PVince81 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@PVince81
Copy link
Member

please rebase/fixup

Sessions are a locking operation until we write close them, so close
them early and reopen later in case we want to write to them

Signed-off-by: Julius Härtl <[email protected]>
@juliusknorr juliusknorr added the 3. to review Waiting for reviews label Aug 17, 2022
@skjnldsv skjnldsv mentioned this pull request Aug 18, 2022
@PVince81 PVince81 merged commit 1a2c008 into master Aug 23, 2022
@PVince81 PVince81 deleted the enh/session-reopen branch August 23, 2022 12:14
@blizzz blizzz mentioned this pull request Aug 24, 2022
Copy link

@violethaze74 violethaze74 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants