latte icon indicating copy to clipboard operation
latte copied to clipboard

Coroutine safety: PHP Fatal error: Cannot redeclare class

Open wilaak opened this issue 2 months ago • 0 comments

Version 3.1.0

Hi, I'm running into issues when using latte in Swoole coroutines with runtime hooks enabled.

Trace

PHP Fatal error:  Cannot redeclare class Template_389fc8e4d9 (previously declared in [REDACTED_PATH]/.latte-cache/template-index.latte--389fc8e4d9.php:10) in [REDACTED_PATH]/.latte-cache/template-index.latte--389fc8e4d9.php on line 10
[REDACTED_TIMESTAMP] ERROR   php_swoole_server_rshutdown() (ERRNO 503): Fatal error: Cannot redeclare class Template_389fc8e4d9 (previously declared in [REDACTED_PATH]/.latte-cache/template-index.latte--389fc8e4d9.php:10) in [REDACTED_PATH]/.latte-cache/template-index.latte--389fc8e4d9.php on line 10

Steps to reproduce

The issue seems to occur when under high load and seemingly at random. Maybe an issue of using eval() without proper isolation, two coroutines may try to load or compile the same template at the same time. (I'm only guessing here)

// This will make built-in functions coroutine friendly
\Swoole\Runtime::enableCoroutine();

Then just use the templating engine as normal

wilaak avatar Sep 29 '25 15:09 wilaak