You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Symfony 5.4渲染含表单Twig模板时触发无限循环问题

升级PHP/Symfony后表单渲染无限循环问题排查与解决

问题背景

将遗留PHP应用从PHP 7.2 + Symfony 3.4升级至PHP 8.2 + Symfony 5.4后,渲染包含Symfony Form的Twig模板时触发无限循环,最终导致内存耗尽(OOM);启用Xdebug后直接检测到栈溢出(深度1024)。

错误日志

OOM错误

Uncaught PHP Exception Symfony\Component\ErrorHandler\Error\OutOfMemoryError: "Error: Out of memory (allocated 3509583872) (tried to allocate 90112 bytes)" at /vendor/symfony/symfony/src/Symfony/Component/Translation/Translator.php line 432 {"exception":"[object] (Symfony\Component\ErrorHandler\Error\OutOfMemoryError(code: 0): Error: Out of memory (allocated 3509583872) (tried to allocate 90112 bytes) at /vendor/symfony/symfony/src/Symfony/Component/Translation/Translator.php:432)"}

Xdebug无限循环检测

Uncaught PHP Exception Error: "Xdebug has detected a possible infinite loop, and aborted your script with a stack depth of '1024' frames" at /vendor/sylius/theme-bundle/src/Translation/Translator.php line 127 {"exception":"[object] (Error(code: 0): Xdebug has detected a possible infinite loop, and aborted your script with a stack depth of '1024' frames at /vendor/sylius/theme-bundle/src/Translation/Translator.php:127)"} []

关键现象

  • 注释掉Twig模板中的form_start()、form_row()、form_end()后页面可正常加载
  • 无表单的页面渲染完全正常
  • 问题仅出现在Amazon Linux 2的AWS EC2实例,Ubuntu 22环境无法复现

排查与解决方向

  1. Sylius主题翻译器循环调用问题
    错误指向Sylius主题包的Translator.php第127行,大概率是翻译器处理表单标签/错误信息时触发了递归调用:

    • 确认Sylius Theme Bundle版本与Symfony 5.4的兼容性,升级至对应兼容版本
    • 查看Translator.php的trans()方法实现,检查是否存在未正确终止的自调用逻辑(比如未处理特定翻译键导致循环回退)
  2. PHP环境差异触发递归逻辑
    Amazon Linux 2与Ubuntu的PHP配置/扩展版本存在差异,重点排查:

    • mbstring、intl扩展的版本和配置,表单翻译依赖这些扩展,不同环境的字符处理逻辑可能触发循环
    • 对比两个环境的php.ini配置,重点关注memory_limit、xdebug.max_nesting_level(虽调整栈深度无效,但需确认其他配置差异)
  3. Symfony Form组件翻译适配问题
    Symfony 3.4到5.4的Form组件翻译逻辑有变化,需检查:

    • 表单类型(如SystemPropertyType.php)是否使用了过时的翻译键或方式,导致翻译器无法正确解析进而进入循环
    • 确认translator服务配置,排查主题环境下是否存在服务循环注入(比如Sylius主题翻译器与Symfony核心翻译器互相依赖)
  4. 定位递归触发源
    在Translator.php第127行附近添加debug_backtrace()打印调用栈,记录每次递归的触发路径,找到循环的起始点(比如特定表单字段的翻译键)

临时规避方案

  • 临时禁用Sylius主题翻译器,改用Symfony核心翻译器验证是否解决循环
  • 对表单字段手动指定翻译文本,避免触发自动翻译逻辑

内容的提问来源于stack exchange,提问作者Quintin Roper

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 09:15:53