Problem/Motivation

Trying to set this up for Anon users to start with. We have a Kubernetes pod configured with Nginx and a Varnish VCL that works for anonymous users without this module enabled. I assume this module provides access to some of this configuration through the UI (as well as providing use with authenticated users - our real goal here).

We have swapped out our entire working vcl with the default one and made the couple IP changes required (but used host names instead of IP). When caching is enabled for anon the site works fine for admin (since this isnt doing anything for auth users). But as soon as i go to a page as anon i get this error:

Error: Call to a member function getRequestUri() on null in Drupal\adv_varnish\CacheManager->cachingEnabled() (line 467 of modules/contrib/adv_varnish/src/CacheManager.php).

Checking that code (which i should have done earlier.. ughh) and i see this:

    // Check if we can be on disabled domain.
    $excluded_urls = explode(PHP_EOL, $this->config->get('available.excluded_urls'));
    $request_uri = $this->request->getRequestUri();

It looks like if no exclude domain added to config this will crash. I tested by simply adding goggle.ca to the list and now it works.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Comments

liquidcms created an issue. See original summary.

liquidcms’s picture

Title: Error: Call to a member function getRequestUri() on null » Fails with no Excluded Domains set.
shumer’s picture

Assigned: Unassigned » shumer
liquidcms’s picture

this time it didnt seem to fix it.. not sure why not.. but tomorrow i'll do a patch to just bypass

shumer’s picture

StatusFileSize
new120.78 KB
new95.94 KB

Hello @liquidcms

Error: Call to a member function getRequestUri()
The getRequestUri performed on request object and returns the requested URI (path and query string).
This has no deal with Excluded domain option which can be empty.
Here is an example with the default VCL povided by module:

Varnish
Varnish
As you can see it works as expected. In your case it looks like your request object was empty.... which is weird.
Are your sure that there were no any additional steps done?
Have you tried to clear caches after module was installed/configured?

BR, Alex

shumer’s picture

If you're going to make a patch. Can you add a debug to see the state of request?
Maybe this will help us to understand the problem and fix it properly.

maltaib1’s picture

@shumer, I have followed the instruction and deployed a copy of the default.vcl file with the required modifications.

It does not appear that the issue we are observing is linked to the varnish vcl config. If I leave our previously used varnish vcl in place and enable adv_varnish + configure + drush cr the same error is present... I even remove varnish altogether and got the same error showing up... so, I suspect it is something specific to our site/environment that might be causing this...

We deploy our site on a kubernetes cluster using the drupalwxt helm chart: https://github.com/drupalwxt/helm-drupal

We leverage a varnish pod, nginx pod, redis pods, php fpm pod and a mysql pod.

This won't help you figure out why, but I figured it might be worth noting.

liquidcms’s picture

Debugging locally (varnish not installed, no vcl, etc) and i see $this->request (CacheManager.php is always null). Is it possible this is not the correct way to inject this service for D9?

shumer’s picture

To get the request we're using '@request_stack' service which is recommended way to get the request object.
In case of D9 specfic - my screenshots was done on the 9.3.5 Core.

Interesting moment that this is a Symfony component, I don't think that you overrride it somehow... unless you have somewhere calls to `pop` method from there.

Are you able to check the Symfony\Component\HttpFoundation\RequestStack
we're interested in value of:

    /**
     * @var Request[]
     */
    private $requests = [];

you can debug `push` and `pop` methods to see how the Drupal handle the request lifecycle.
Also can you share the local setup with me? I can try to reproduce the issue to help you with the fix.

liquidcms’s picture

Not sure what local setup would be that you are referring to - i don't even have varnish installed for debugging this.

We have decided to not use this module at this time so not sure how much more time i can spend debugging this (sorry to just post and run) - but will try to debug inside the Symphony class directly to see what is going on (later this afternoon).

I have no custom code which is overriding this or popping anything from the stack. Could be in some contrib module i guess.

shumer’s picture

Could be in some contrib module i guess.

that's can be our issue. Can you share the list of enabled contrib modules? Composer file maybe?
I'll try to reproduce that and maybe you'll be able to use the module :)

liquidcms’s picture

StatusFileSize
new14.91 KB

Composer.json is attached; note that it calls another module called wxt. This has its own large composer.json which pulls in a bunch more contrib modules (and Drupal core).

shumer’s picture

That's fine.
One last thing can you also share
drush pml --status=enabled
so I won't have to enable ALL of them

liquidcms’s picture

StatusFileSize
new30.23 KB
shumer’s picture

Thx for the list, I'll try to deploy the project at my local tomorrow,

shumer’s picture

Question.. the page where you see that error. is it build with the page manager?

shumer’s picture

@liquidcms, can you try to replace:

$request_uri = $this->request->getRequestUri();

with

use Drupal\Core\Url;
$request_uri = Url::fromRoute('<current>', [])->getInternalPath();

if it works we can prepare proper patch and make a new release.

arnaud-brugnon’s picture

Hi,

Just kick out stupid purge modules from my project and i got a brand new issue.

Wonderfull

I am so glad that someone got the same error before me.

Btw, the fix purposed in #17 only solve an error to bring another one.

Error: Call to a member function getHost() on null in Drupal\adv_varnish\CacheManager->cachingEnabled()

Like request object is empty, it s logical that every call's to it fail :(

arnaud-brugnon’s picture

Adding those line at the beginning of the method seems to solve every issue

if(empty($this->request)){
    $this->request = \Drupal::request();
     }
arnaud-brugnon’s picture

Here 's my full patch btw

diff --git a/src/CacheManager.php b/src/CacheManager.php
--- a/src/CacheManager.php (date 1645106840879)
+++ b/src/CacheManager.php (date 1645106840879)
@@ -450,6 +450,9 @@
*/
public function cachingEnabled() {

+ if (empty($this->request)) {
+ $this->request = \Drupal::request();
+ }
// Check if user has permission to bypass varnish.
if ($this->account->hasPermission('bypass advanced varnish cache')) {
$this->log(RfcLogLevel::DEBUG, 'Cache disabled by Bypass Varnish permission.');

liquidcms’s picture

@shumer, yes, replacing that line fixes my error and the error is then on the following line as mentioned by arnaud-brugnon.

also, this happens on every page. we don't use page manager.

liquidcms’s picture

Title: Fails with no Excluded Domains set. » Fails for anon when anon not enabled ($this->request is null).
shumer’s picture

StatusFileSize
new2.59 KB

Thanks for helping with this issue guys,

@amaud-brugnon - that was a great idea, actually I was done some research for this issue, looks like we have the same situation with the Forms in Drupal Core...

Anyway, I did a patch that work in Drupal Core way. IF someone of you can test it would be awesome.
In case of success I'll do a new release.

P.S. it works fine at my local setup.

shumer’s picture

shumer’s picture

StatusFileSize
new2.55 KB

Sorry wrong file was attached.
The proper one here:

shumer’s picture

Status: Active » Needs review
arnaud-brugnon’s picture

Well, it don't see why this may not work.

I will test it tomorow in the morning and i will let you know.

Btw, good work with your module, i love it (i waste so much time to this stupid purges modules)

arnaud-brugnon’s picture

It works for me.

Perfect, thanks

  • shumer committed eb3bb84 on 3264583-Request-object-fixes
    Issue #3264583 by shumer, liquidcms, arnaud-brugnon: Fails for anon when...

  • shumer committed 0c792ea on 3264583-Request-object-fix
    Issue #3264583 by shumer, liquidcms, arnaud-brugnon: Fails for anon when...

  • shumer committed f73ed7a on 4.x
    Issue #3264583 by shumer, liquidcms, arnaud-brugnon: Fails for anon when...
shumer’s picture

Status: Needs review » Fixed

Btw, good work with your module, i love it (i waste so much time to this stupid purges modules)

Nice to hear :)

Thanks guys for your help on this issue. New release with the fix was published.

liquidcms’s picture

Haven't tested much else; but this fix does fix the issue reported here. Thanks for the quick update.

Status: Fixed » Closed (fixed)

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