I get a php fatal error when running drush cron on a drupal where Flag is installed.

Executing: mysql --defaults-extra-file=/tmp/drush_kYQR0n --database=xxxxxx --host=xxxxxx --port=xxxx --silent  < /tmp/drush_lvGML4
Executing: mysql --defaults-extra-file=/tmp/drush_TmyFB0 --database=xxxxxx --host=xxxxxx --port=xxxx --silent  < /tmp/drush_DVX7iH
Starting execution of comment_cron().                                   [notice]
Starting execution of dblog_cron(), execution of comment_cron() took    [notice]
6.89ms.
Starting execution of field_cron(), execution of dblog_cron() took      [notice]
9.29ms.
Starting execution of file_cron(), execution of field_cron() took       [notice]
3.98ms.
Starting execution of history_cron(), execution of file_cron() took     [notice]
17.56ms.
Starting execution of locale_cron(), execution of history_cron() took   [notice]
56.67ms.
Starting execution of node_cron(), execution of locale_cron() took      [notice]
0.06ms.
Starting execution of search_cron(), execution of node_cron() took      [notice]
481.27ms.
PHP Fatal error:  Call to a member function get() on null in /var/www/html/drupal/modules/flag/src/Entity/Storage/FlaggingStorage.php on line 89
Drush command terminated abnormally due to an unrecoverable error.       [error]
Error: Call to a member function get() on null in
/var/www/html/drupal/modules/flag/src/Entity/Storage/FlaggingStorage.php,
line 89

Drupal is in 8.2.0 version and Flag on Flag 8.x-4.x-dev (2016-oct-07) version. It happend only when a Flag is created.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

geoffvio created an issue. See original summary.

geoffvio’s picture

Issue summary: View changes
oknate’s picture

Error goes away if line 89 is changed to

if ($session = \Drupal::request()->getSession() && $session_flaggings = $session->get('flaggings', [])) {

oknate’s picture

This seems to work better:

      $session = \Drupal::request()->getSession();
      if ($session_flaggings = $session->get('flaggings', [])) {
        $user_or_global_condition->condition('id', $session_flaggings, 'IN');
      }

I started to notice this error on a node edit form. I think instantiating a session in the condition is the issue.

Berdir’s picture

There is no functional difference in that change, your second patch can't possibly have a different outcome.

If the error stopped appearing then something else changed.

oknate’s picture

FileSize
764 bytes

The reason I've created several patches is because it seems the session object behaves differently when on the command line. I'm doing a third patch that tests for the existence of the session object (like the first patch).

Even though it's functionally the same, it seems that creating null objects in conditions and then calling methods on those objects throws errors. PHP isn't smart enough to stop processing and you get errors that you can't call a method of a null object. So that's why I moved the session instantiation outside of the condition.

This new patch checks if the session exists, because it seems that it's still trying to call the method of the null object on the command line when running a migration import.

achton’s picture

I am seeing this as well, but on the front page of my site for anonymous users. I think it's fair to add some defensive code here (as the patches above do), considering that Request::getSession() may return null, in which case the fatal PHP error will be triggered.

I've tested the patch in #6 which works for me. I'm running Drupal 8.2.1.

el1_1el’s picture

same here. if i run drush cron I get the fatal error. Patch in #6 fixes it

zaporylie’s picture

Status: Active » Reviewed & tested by the community
FileSize
743 bytes

I run across this issue when I was behat-testing nodes with flags. IMHO approach from #3 was just enough but it's missing parenthesis which makes it fail.

For me doesn't matter if #6 or #9 (#3 with parenthesis) gets in - both solves the same problem.

joachim’s picture

I don't understand how the parentheses make a difference between #3 and #9...

zaporylie’s picture

That is because of operator precedence. The assignment operator = has a lower precedence than &&

if ($session = \Drupal::request()->getSession() && $session_flaggings = $session->get('flaggings', [])) {}

so without the parenthesis you are effectively doing the following:

if ($session = (\Drupal::request()->getSession() && $session_flaggings = $session->get('flaggings', []))) {}

  • joachim committed 2f13df3 on 8.x-4.x authored by oknate
    Issue #2815633 by oknate, zaporylie: Fixed fatal error when session...
joachim’s picture

Status: Reviewed & tested by the community » Fixed

Thanks! Braindead maintainer appreciates the explanation :)

And thank you all for the work on the patch.

Status: Fixed » Closed (fixed)

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