Just a heads up, in case anyone else experiences the same: We were getting this error after a production server reboot (v.7.0.2 Express Plus on AWS):
opened 01:46PM - 16 Aug 23 UTC
closed 06:24AM - 15 Dec 23 UTC
Hello :wave:
I've been experiencing some weird call to undefined method erro… rs from internally in twig. In production I have an apache2 webserver running mod_php with mpm_prefork and I have opcache and JIT enabled. I'm also using twig template caching with the default `\Twig\Cache\FilesystemCache` implementation. Deployment to production is by essentially syncing the new files into the directory (there is no symlink changes), clearing opcache, and removing the old twig cache. After a while (and seemingly at random) I get the following error which I "fix" by reloading apache2 (which clears opcache cache):
I have been unsuccessful at reproducing this error. I have also not been able to debug the issue since production crashes for everyone when it occurs giving me a quite small timeframe to resolve the issue. Searching the internet for the error message gives no helpful results.
The reason I'm reporting this issue is to get help from the people that are more familiar with twig internals in figuring it out.
The following is the stacktrace for twig:
```
Error: Call to undefined method Twig\\Token::setSourceContext() in /srv/app/vendor/twig/twig/src/Node/Node.php:171
Stack trace:
#0 /srv/app/vendor/twig/twig/src/Node/Node.php(171): Twig\\Node\\Node->setSourceContext()
#1 /srv/app/vendor/twig/twig/src/Node/Node.php(171): Twig\\Node\\Node->setSourceContext()
#2 /srv/app/vendor/twig/twig/src/Node/Node.php(171): Twig\\Node\\Node->setSourceContext()
#3 /srv/app/vendor/twig/twig/src/Node/Node.php(171): Twig\\Node\\Node->setSourceContext()
#4 /srv/app/vendor/twig/twig/src/Node/ModuleNode.php(55): Twig\\Node\\Node->setSourceContext()
#5 /srv/app/vendor/twig/twig/src/Parser.php(100): Twig\\Node\\ModuleNode->__construct()
#6 /srv/app/vendor/twig/twig/src/Environment.php(491): Twig\\Parser->parse()
#7 /srv/app/vendor/twig/twig/src/Environment.php(519): Twig\\Environment->parse()
#8 /srv/app/vendor/twig/twig/src/Environment.php(351): Twig\\Environment->compileSource()
#9 /srv/app/vendor/twig/twig/src/Environment.php(312): Twig\\Environment->loadTemplate()
#10 /srv/app/vendor/twig/twig/src/Environment.php(280): Twig\\Environment->load()
#11 /srv/app/src/HttpController.php(65): Twig\\Environment->render()
```
Twig is initialized like this in production:
```php
<?php
$options = [
'cache' => new \Twig\Cache\FilesystemCache('CACHEDIR', \Twig\Cache\FilesystemCache::FORCE_BYTECODE_INVALIDATION),
'debug' => false,
'strict_variables' => false,
];
$loader = new \Twig\Loader\FilesystemLoader(['VIEWSDIR']);
$twigEnv = new \Twig\Environment($loader, $options);
$twigEnv->addExtension(CUSTOM EXTENSION);
```
The custom extension adds some global variables, filters, and functions.
The twig render and display methods are used multiple times for different template parts in the app.
Deployment steps performed:
```shell
# files are synced over
dbmigrate
systemctl reload apache2
rm -r /var/cache/twig/
```
Specific opcache settings for the webserver (all other are default):
```
opcache.enable = On
opcache.enable_cli = Off
opcache.memory_consumption = 512
opcache.interned_strings_buffer = 128
opcache.max_wasted_percentage = 15
opcache.max_accelerated_files = 130987
opcache.use_cwd = On
opcache.validate_timestamps = Off
opcache.file_update_protection = 0
opcache.save_comments = On
opcache.jit = tracing
opcache.jit_buffer_size = 64M
```
Versions:
```
PHP 8.2.8
Apache 2.4.41
Twig 3.5.1
```
The most apparent symptom was that patient dashboards would not render at all (all blank under the navbar), while other system functions seemed to work.
The problem went away after another server reboot, so perhaps it is related to launch order of operations.