Problem/Motivation

Quoting from SessionHandler::write():

  public function write($sid, $value) {
    // The exception handler is not active at this point, so we need to do it
    // manually.
    try {
      [...]
      return TRUE;
    }
    catch (\Exception $exception) {
      require_once DRUPAL_ROOT . '/core/includes/errors.inc';
      // If we are displaying errors, then do so with no possibility of a
      // further uncaught exception being thrown.
      if (error_displayable()) {
        print '<h1>Uncaught exception thrown in session handler.</h1>';
        print '<p>' . Error::renderExceptionSafe($exception) . '</p><hr />';
      }
      return FALSE;
    }
  }

The comments are out of date, the exception handler is in place way before the session subsystem gets initialized. In addition it is preferable to let an exception fall through to the exception handler instead of attempting to render it into the (already finished) output because:

  1. The exception handler will try to properly log the exception
  2. The output might be of a different type than HTML

Proposed resolution

Remove the try...catch.

Remaining tasks

Review.
Decide if a BC layer is needed.

User interface changes

None.

API changes

None.

Data model changes

None.

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Bug because the custom try...catch possibly clobbers non-HTML output and suppresses logging
Issue priority Normal

Issue fork drupal-2536052

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

znerol’s picture

Status: Active » Needs review
Issue tags: +Quick fix, +Quickfix
StatusFileSize
new1.75 KB
dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Great observation! I doubt that we should protect the session subsystem, because when this particular DB write fails, other reads might fail as well for example.

It seems to be that this try catch is inherited from D7: https://api.drupal.org/api/drupal/includes%21session.inc/function/_drupa...

alexpott’s picture

Status: Reviewed & tested by the community » Needs review

I'm not sure about this because it looks like we're trying to obey PHP's session hander interface...

	 * @return bool
	 * The return value (usually TRUE on success, FALSE on failure).
	 * Note this value is returned internally to PHP for processing.
znerol’s picture

I was also had concerns about that but after looking at the session code in PHP I think the return value is rather irrelevant. In fact what happens is that if the session handler returns failure and no exception was thrown, then the following PHP warning is emitted: session_write_close(): Failed to write session data (user). Please verify that the current setting of session.save_path is correct (/var/lib/php5). Execution continues after that.

First, for debugging purposes the warning is clearly less useful than an exception because it does not point to the underlying reason. Second, if that db query fails, then the site most likely has much more serious issues than just not being able to save session data. Therefore I think it is reasonable to fail hard in this case.

In addition I reviewed some other session handler implementations (especially from Symfony) and none of them cares to catches exceptions inside the session handler.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

mile23’s picture

StatusFileSize
new1.75 KB

The patch from #1 miraculously still applies to 8.1.x.

Re-uploading it here for the testbot. Note that this is @znerol's work and not mine.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Status: Needs review » Needs work

The last submitted patch, 6: remove_try_catch_from-2536052-1.patch, failed testing.

dawehner’s picture

This is a really nice improvement IMHO. On the other hand I'm wondering, was the php7 failure a random one?

On top of that I'm wondering whether someone actually tried this out, so drop the database table and see what happens. Does maybe the logging system maybe rely on some session stuff?

anish.a’s picture

Version: 8.2.x-dev » 8.3.x-dev
Status: Needs work » Needs review
StatusFileSize
new1.75 KB

This patch applies at 8.3.x too. Bumping up the version.

dawehner’s picture

@anish.a
If a patch still applied there is no reason to reupload it :) Just trigger the retesting and be done with it.

One thing we maybe could do to reduce the maybe potential risk of this patch is to move the exception handler registration further up in \Drupal\Core\DrupalKernel::bootEnvironment basically to the top. The earlier, especially before the test setup, the better.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

voleger’s picture

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

jungle’s picture

Issue tags: +Bug Smash Initiative
StatusFileSize
new1.63 KB

I'm not sure about this because it looks like we're trying to obey PHP's session hander interface...

* @return bool
* The return value (usually TRUE on success, FALSE on failure).
* Note this value is returned internally to PHP for processing.

+++ b/core/lib/Drupal/Core/Session/SessionHandler.php
@@ -73,32 +73,18 @@ public function read($sid) {
+    $request = $this->requestStack->getCurrentRequest();
+    $fields = array(
+      'uid' => $request->getSession()->get('uid', 0),
+      'hostname' => $request->getClientIP(),
+      'session' => $value,
+      'timestamp' => REQUEST_TIME,
+    );
+    $this->connection->merge('sessions')
+      ->keys(array('sid' => Crypt::hashBase64($sid)))
+      ->fields($fields)
+      ->execute();
+    return TRUE;

After removal, it always returns TRUE, looking at the code from the patch in #10, the database operation can go wrong, which may throw an exception and return FALSE accordingly. So proposing the following, then the all possible return values -- TRUE/FALSE of SessionHandlerInterface::write() get respected:

public function write($sid, $value) {
    $request = $this->requestStack->getCurrentRequest();
    $fields = [
      'uid' => $request->getSession()->get('uid', 0),
      'hostname' => $request->getClientIP(),
      'session' => $value,
      'timestamp' => \Drupal::time()->getRequestTime(),
    ];
    try {
      $this->connection->merge('sessions')
        ->keys(['sid' => Crypt::hashBase64($sid)])
        ->fields($fields)
        ->execute();
      return TRUE;
    }
    catch (\Exception $exception) {
      return FALSE;
    }
  }

(Rerolled the patch for 9.1.x, meanwhile, made the change proposed, no interdiff)

jungle’s picture

StatusFileSize
new3.72 KB
new2.7 KB
  1. Removed an unused use statement
  2. +++ b/core/lib/Drupal/Core/Session/SessionHandler.php
    @@ -68,16 +68,14 @@ public function read($sid) {
    +      'timestamp' => \Drupal::time()->getRequestTime(),
    

    Use IoC, not sure if we need a BC layer. so used a placeholder CR URL in @trigger_error in the attached patch.

quietone’s picture

Issue summary: View changes
Status: Needs review » Needs work

I reviewed the patch and found one nit although I can't comment on the validity of what this patch is doing. Also, does this need a test?

+++ b/core/lib/Drupal/Core/Session/SessionHandler.php
@@ -37,10 +44,17 @@ class SessionHandler extends AbstractProxy implements \SessionHandlerInterface {
+   * @param \Drupal\Component\Datetime\TimeInterface $time

Needs a 'null' added, \Drupal\Component\Datetime\TimeInterface|null

jungle’s picture

Issue tags: +Needs tests

Thanks @quietone! Nice catch, tagging "Needs tests".

andypost’s picture

__NAMESPACE__ . '\SessionHandler::__construct() could use __METHOD__ instead

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

rishi.kulshreshtha’s picture

StatusFileSize
new3.7 KB
new1.33 KB

This includes the suggestions mentioned above, this still needs a testcase.

andypost’s picture

+++ b/core/lib/Drupal/Core/Session/SessionHandler.php
@@ -37,10 +44,17 @@ class SessionHandler extends AbstractProxy implements \SessionHandlerInterface {
+      @trigger_error('Calling ' . __METHOD__ . ' without the $time argument is deprecated in drupal:9.1.0 and will be required in drupal:10.0.0. See https://www.drupal.org/node/TODO', E_USER_DEPRECATED);

should be 9.3.0 and will be required needs change to is required

alexpott’s picture

Re #21 and the change for REQUEST_TIME. That's not in scope here. This issue is about removing the try / catch and not about replacing REQUEST_TIME.

Also the change to catch the exception and return FALSE and then remove all logging is not correct - that will eat the errors even more than HEAD. An exception is not a return value it's an unrecoverable error - returning FALSE at this point does not matter - we expect this to fail. FALSE will mean the system soldiers on but we've failed.

alexpott’s picture

One thing that is interesting is that if this returns FALSE then PHP will write to the log... see https://github.com/cakephp/cakephp/issues/13581 for a very similar discussion for another CMS.

Not sure what the correct thing to do here is. It'd be good to add a test to see what happens if the sessions table is dropped mid request and we try to write to it.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

znerol’s picture

Status: Needs work » Needs review
StatusFileSize
new2.1 KB

Rerolling #1 for 11.x. #6 and #10 are identical. Changes introduced in #20 and #21 are not desirable. We do not want to eat the exception if the database write failed. Instead the request should crash hard.

This is inline with session handlers in other projects. E.g., the PdoSessionHandler in symfony explicitly rethrows any exception encountered during db writes.

znerol’s picture

Opened #3377256: Correctly implement SessionHandlerInterface for other changes (e.g., the REQUEST_TIME deprecation and changed method signatures). Also added credits for @jungle over there.

znerol’s picture

smustgrave’s picture

Status: Needs review » Needs work

Was previously tagged for a test which still needs to happen I believe.

Will add related issue to my review list.

znerol’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests

Re testing: This class gets installed into the PHP runtime via session_set_save_handler(). Given that fact, I'd argue that the only way to meaningfully cover this is using an end-to-end test.

Good news is that we have functional tests in place for session manager and co. in: Drupal\Tests\system\Functional\Session\SessionTest.php. No need to add special coverage for the refactoring here IMHO.

znerol’s picture

Ok, took me some time to come up this idea for a test: SessionTestController::triggerWriteException replaces the sessions table with an incomplete one in order to trigger an exception during SessionHandler::write(). So we can look for that from within SessionTest::testSessionWriteError().

znerol’s picture

Also switched to MR workflow.

smustgrave’s picture

Status: Needs review » Needs work

Seems to have a test failure.

znerol’s picture

Status: Needs work » Needs review

Ah right! The SQL error messages are db specific. Thus, let's look for DatabaseExceptionWrapper (wraps db specific exceptions) and also the string INSERT INTO which should be present in the error messages generated by all supported databases.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Perfect LGTM

  • catch committed ba5d3381 on 11.x
    Issue #2536052 by znerol, jungle, Rishi Kulshreshtha, smustgrave,...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Test coverage looks good. Read back through the discussion and just removing the try/catch seems right here, anything else can happen in other issues. Committed ba5d338 and pushed to 11.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.