I am getting and setting values to PrivateTempStore on every request. This works fine while accessing the site through a browser but when my code is invoked through Drush on the command line. drush cron in this case, no session exists and the following error is thrown:
Error: Call to a member function getId() on null in Drupal\user\PrivateTempStore->getOwner()
getOwner is trying to access $this->requestStack->getCurrentRequest()->getSession()->getId(); where ->getSession() returns null.

Shouldn't PrivateTempStore act as a void store or throw a more informative error about usage in this case?

Comments

L-four created an issue. See original summary.

stephsem23’s picture

StatusFileSize
new576 bytes

getting this same error as well when trying to update the address module. This error prevents drush updb -y from completing the address module's db updates. I took a stab at a patch for this.

ephod’s picture

Hello I'm Michael,
I've seen that this method is using the chaining methods without checking if the result, which is being returned, is either a null or something else.

Therefore, I have created this patch to solve this issue.

ephod’s picture

RumyanaRuseva’s picture

Thank you for the patch. Here are a few changes:
1. I believe we can safely chain getCurrentRequest()->getSession(), because we should always have a request, so we do not need to check the result of every function.
2. Avoiding the error however may cause another problem - freely getting stored objects with owner NULL. This can be fixed either in get() not to return the object, or in set() so that it's not stored at all.
3. PrivateTempStore has been moved from Drupal\user to Drupal\Core\TempStore.

RumyanaRuseva’s picture

Version: 8.2.x-dev » 8.6.x-dev
Status: Active » Needs review
juampynr’s picture

Worked well for me. I faced this error when running configuration migrations. Here is an screenshot:

Tempstore error

I am also uploading another patch where I have updated the phpdoc and made explicit the return value.

juampynr’s picture

Doh. Removed an unneeded blank line.

Status: Needs review » Needs work
heddn’s picture

Status: Needs work » Needs review
StatusFileSize
new4.64 KB
new2.41 KB

Let's see if that has fewer errors.

heddn’s picture

StatusFileSize
new4.65 KB
new794 bytes

More semantic use of the API.

The last submitted patch, 11: 2860341-11.patch, failed testing. View results

The last submitted patch, 11: 2860341-11-test_only.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 12: 2860341-12.patch, failed testing. View results

joelpittet’s picture

@heddn, I'm a bit fussy but $has_session implies a boolean value is stored. There is a hasSession() method that does return a boolean on that object though.

Edit, apparently my coffee didn't kick in yet...

heddn’s picture

+++ b/core/lib/Drupal/Core/TempStore/PrivateTempStore.php
@@ -219,7 +207,34 @@ protected function createkey($key) {
+  protected function startSession() {
+    $has_session = $this->requestStack
+      ->getCurrentRequest()
+      ->hasSession();
+    if (!$has_session) {

That's what I'm doing, no?

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.

firfin’s picture

I was until now using the work-around patch from #2976862: PrivateTempStore throws exception if session is not started (drush users) which works spendidly. But tht's just aworkaround, so I thought I would check out this one. Currently testing, it applied without problems and I don't get errors anymore. I have used it with half a dozen custom migration files so far.

Edit:

One question though? Why is this still (again) on NW ?

Ah a failure on testing:

There was 1 error:
1) Drupal\Tests\node\Functional\NodePreviewAnonymousTest::testAnonymousPagePreview
Behat\Mink\Exception\ExpectationException: Link with label Back to content editing found.

/var/www/html/core/tests/Drupal/Tests/WebAssert.php:437
/var/www/html/core/tests/Drupal/Tests/WebAssert.php:231
/var/www/html/core/modules/node/tests/src/Functional/NodePreviewAnonymousTest.php:57

Unfortunately I have no idea how to fix that.

seanb’s picture

Status: Needs work » Needs review
StatusFileSize
new4.21 KB
new1.08 KB

This should fix the test.

heddn’s picture

I don't see the interdiff included in the patch? Is it just me?

seanb’s picture

I don't see the interdiff included in the patch? Is it just me?

The problem was that the patch in #12 removed some code that was still needed. Just the extra $this->startSession(); was added to PrivateTempStore::set().

robertom’s picture

sorry if I disturb the thread with a backport, but I need an equivalent patch for an old 8.4.x and the CI

Status: Needs review » Needs work

The last submitted patch, 23: backport_to_8.4.x-2860341-20_and_2743931-107.patch, failed testing. View results

robertom’s picture

Status: Needs work » Needs review
firfin’s picture

Status: Needs review » Reviewed & tested by the community

Patch from #20 works fine for me. Applies, fixes my problem (which was that importers weren't running with migrate_tools ), saw no other error. Been using it for 3 days without problems.

alexpott’s picture

StatusFileSize
new4.21 KB

@robertom the last patch on the issue should be the rtbc patch so adding an 8.4.x does upset the issue. If you do this in the future please upload the correct patch again in the next comment.

alexpott’s picture

Updating commit credit to credit people who contributed patches which progressed the issue.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed 7ae9670d22 to 8.7.x and 590a5e7904 to 8.6.x. Thanks!

  • alexpott committed 7ae9670 on 8.7.x
    Issue #2860341 by heddn, juampynr, seanB, RumyanaRuseva, ephod,...

  • alexpott committed 590a5e7 on 8.6.x
    Issue #2860341 by heddn, juampynr, seanB, RumyanaRuseva, ephod,...

Status: Fixed » Closed (fixed)

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