Hello,

Just after updating to Drupal 8.3.0 almost all my website frontend pages show this error:

The website encountered an unexpected error. Please try again later.
Error: Call to a member function getTotalCount() on boolean in statistics_get() (line 129 of core/modules/statistics/statistics.module).

statistics_get('94') (Line: 142)
darna_preprocess_node(Array, 'node', Array) (Line: 287)
Drupal\Core\Theme\ThemeManager->render('node', Array) (Line: 435)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array, ) (Line: 226)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 574)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 227)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 117)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object) (Line: 149)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 64)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 656)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Do you have any idea on how to solve it?

Regards

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

connectedrobots created an issue. See original summary.

webchick’s picture

Status: Active » Postponed (maintainer needs more info)

Reviewing the list of commits to statistics module since August 1, seems like the most likely culprit is probably #1446932: Improve statistics performance by adding a swappable backend, as it touched the statistics_get() function.

+function statistics_get($id) {
+  if ($id > 0) {
+    /** @var \Drupal\statistics\StatisticsViewsResult $statistics */
+    $statistics = \Drupal::service('statistics.storage.node')->fetchView($id);
+    return [
+      'totalcount' => $statistics->getTotalCount(),
+      'daycount' => $statistics->getDayCount(),
+      'timestamp' => $statistics->getTimestamp(),
+    ];
   }
 }
....

+  public function fetchView($id) {
+    $views = $this->fetchViews(array($id));
+    return reset($views);
+  }

That reset() call will return FALSE if $views is an empty array, which it would be if it were checking against a node ID that no longer existed, for example.

I see this call to statistics_get() is coming from your theme; is it possible it's checking against an expired node ID? Or is this happening on every page?

connectedrobots’s picture

Hello and thank you for yout reply,

What do you mean by expired node ID? A node ID which no longer exists?

The problem appears on several pages but not on all: frontpages and blog pages seem not to affected.

Do you need more information?

I am really worried about this problem.

Regards

timmillwood’s picture

Thanks for reporting this.

I'll try to get a fix tomorrow if no one else has by then. I think we just need to put a few checks in place.

cilefen’s picture

Title: Core statistics module error » Error: Call to a member function getTotalCount() on boolean in statistics_get()
Issue tags: +Needs tests

We need some test coverage.

xjm’s picture

Assigned: Unassigned » xjm
Status: Postponed (maintainer needs more info) » Active

I have a test; will post shortly.

catch’s picture

Just a note I thought we'd seen something like this before, but it wasn't, it was #2616330: statistics.php can exit early and prevent output errors which won't help here - in case anyone has the same thought.

xjm’s picture

Status: Active » Needs review
FileSize
903 bytes

So yeah, this is a small BC break for statistics_get(). In 8.2.x it would have returned FALSE for an invalid node ID; in 8.3.x/8.4.x it fatals. Attached demonstrates the fatal. I also manually removed a couple lines of context to prove it works on 8.2.x.

xjm’s picture

And with the fix. This is why we want deprecated test coverage (both dedicated tests for deprecated code and tests to check for deprecated code).

xjm’s picture

Adding a more explicit comment in the test. This probably still won't stop someone from converting it indiscriminately, but it at least should stop us from committing their indiscriminate conversion.

timmillwood’s picture

Instead of checking its empty should be instead check it's a StatisticsViewsResult object.

xjm’s picture

Sure, that makes sense. More robust that way BC-wise.

catch’s picture

+++ b/core/modules/statistics/src/Tests/StatisticsLoggingTest.php
@@ -125,6 +126,17 @@ public function testLogging() {
+
+    // Try fetching statistics for an invalid node ID and verify it returns
+    // an empty array.

Should be FALSE rather than empty array no?

Otherwise looks good.

xjm’s picture

timmillwood’s picture

Status: Needs review » Reviewed & tested by the community

Awesome turnaround

xjm’s picture

This will get NW in a bit once that interdiff gets tested. ;)

Probably should wait for CI in any case.

The last submitted patch, 8: statistics-2867493-FAIL.patch, failed testing.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 14: interdiff-statistics-14.patch, failed testing.

xjm’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
2.08 KB

As above.

cilefen’s picture

cilefen’s picture

@connectedrobots Are you able to manually test the patch in comment #19?

xjm’s picture

The test-only patch in #8 also shows the exact fatal BTW (in the console output):

20:31:59 Fatal error: Call to a member function getTotalCount() on boolean in /var/www/html/core/modules/statistics/statistics.module on line 129
xjm’s picture

Assigned: xjm » Unassigned

Didn't mean to leave this assigned to myself.

alexpott’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
1.22 KB
3.29 KB

Very minor docs improvements so other consumers of the new API might expect it to work this way. Fortunately, statistics_node_links_alter() does it right.

xjm’s picture

Status: Needs review » Needs work
+++ b/core/modules/statistics/src/StatisticsStorageInterface.php
@@ -27,7 +27,10 @@ public function recordView($id);
-   * @return array \Drupal\statistics\StatisticsViewsResult
+   * @return array \Drupal\statistics\StatisticsViewsResult[]
+   *   An array of value objects representing the number of times each entity
+   *   has been viewed. The array is keyed by entity ID. If an ID does not
+   *   exist, it will not be present in the array.

The words and the datatype here do not agree -- datatype says it's a nested array.

Edit: Or I guess the datatype is not valid phpdoc? So probably a typo in fixing the typo.

xjm’s picture

BTW not entirely sure the bad docs are in scope to begin with; while they could cause this bug in other code, they probably didn't in statistics_get() since that was in the same patch where they were added.

(And also statistics_get() not retaining BC is its own bug regardless of what API replaces it.)

alexpott’s picture

Status: Needs work » Needs review
FileSize
761 bytes
3.29 KB

I dunno it was not being aware of this whilst converting statistics_get() to use the new API that caused the bug. If we'd had the correct docs you can programmatically work out where code doesn't code for all possibilities. PHPStorm for example can be set up to do this.

xjm’s picture

Status: Needs review » Reviewed & tested by the community

Okay.

I guess I can RTBC this since the docs changes correlate to what I saw in the patch @timmillwood previously RTBCed and it's a docs addition/fix. Someone other than me or @alexpott should commit the patch though.

  • cilefen committed c2f6edd on 8.4.x
    Issue #2867493 by xjm, alexpott, connectedrobots, webchick: Error: Call...

  • cilefen committed 3689c11 on 8.3.x
    Issue #2867493 by xjm, alexpott, connectedrobots, webchick: Error: Call...
cilefen’s picture

Status: Reviewed & tested by the community » Fixed

Thank you everyone!

Committed c2f6edd and pushed to 8.4.x. Cherry-picked as 3689c11 and pushed to 8.3.x.

connectedrobots’s picture

Hello all of you and thank you for your replies,

As I am not very familiar with the Drupal issues discussions I do not really understand where do we stand now. Should I apply patch in comment # 19, 24 or 27?

Regards

webchick’s picture

Generally-speaking, always go for the latest patch, which in this case is #27.

However! In this case, this change was already made to the code base, so you should be able to upgrade to the 8.3.x-dev release and see/test the fix.

connectedrobots’s picture

Hello and thank you for your reply,

I uploaded the patch to the www/core/modules/statistics folder and when I try to apply it I get this notice:

can't find file to patch at input line 32
Perhaps you should have used the -p or --strip option?
The text leading up to this was:
--------------------------
|diff --git a/core/modules/statistics/src/Tests/StatisticsLoggingTest.php b/core/modules/statistics/src/Tests/StatisticsLoggingTest.php
|index 152c95a..6f2f667 100644
|--- a/core/modules/statistics/src/Tests/StatisticsLoggingTest.php
|+++ b/core/modules/statistics/src/Tests/StatisticsLoggingTest.php

Do you know what is the problem?

webchick you said

[...] you should be able to upgrade to the 8.3.x-dev release and see/test the fix.

but I can not find this release.I only see the 8.4.x-dev one.

Regards

I did not checked my website then. The problem seems to be fixed.

catch’s picture

The patch is against the root of the Drupal core directory rather than the statistics module, try applying from there.

8.3.x dev release is here:
https://www.drupal.org/project/drupal/releases/8.3.x-dev

connectedrobots’s picture

Hello and thank you for your reply,

catch I do not know what should I do now. As I told you the problem seems to be fixed but apparently I applied the patch at the wrong place (www/core/modules/statistics). What should I do according to you? Reinstall Druapl 8.3.0 core statistics module and apply patch here: www/core? Upgrade the whole website to Drupal 8.3.0-dev? Do nothing?

Regards

cilefen’s picture

All core patches are created with respect to the base of the Drupal installation, not /core. So, I think, in this case you should be in directory "www", not "www/core" when you apply the patch.

connectedrobots’s picture

Hello and thank you for your reply,

cilefen are you sure ? Anyway thank you for the precision but that does not tell me what to do.

Regards

xjm’s picture

Hi @connectedrobots,

In the short term, updating to the current 8.3.x-dev release as @catch above linked it would probably be a good way to ensure that the bug does not resurface, especially if you are unsure whether or not the patch was applied or about maintaining a site with a patch running. If you do use the development release, just be sure to update it again to the latest dev code whenever there are announcements of new releases, especially security releases.

The next scheduled bugfix release for Drupal core is May 3. There could be other releases between now and then that might not include this fix, but the May 3 release almost certainly will. So after May 3 you should be able to update Drupal to a stable release and count on Drupal updates to work normally again after that. Just keep an eye on the release notes for any release between now and then. The first release that includes this fix will mention it prominently in the release notes.

I hope this helps! Thanks again for reporting this issue right away.

Status: Fixed » Closed (fixed)

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