Spaces:
Running
Running
fix(code): Fix compatibility issue in code generator
Browse filesThis commit fixes a runtime AttributeError in the code generator that occurred in the deployment environment due to a Python version mismatch.
- Replaced `is_alive()` with `poll() is None` for `subprocess.Popen` objects to ensure compatibility with Python 3.10+.
- Replaced incorrect `join()` call with `wait()`.
This change is documented in docs/backlog/2025-10-14-11-21-fix-code-generator-compatibility.md.
docs/backlog/2025-10-14-11-21-fix-code-generator-compatibility.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 需求: 修复代码生成器的跨环境兼容性问题
|
| 2 |
+
|
| 3 |
+
**重要性**: 高 (High)
|
| 4 |
+
|
| 5 |
+
**状态**: 已完成
|
| 6 |
+
|
| 7 |
+
---
|
| 8 |
+
|
| 9 |
+
## 1. 问题描述
|
| 10 |
+
|
| 11 |
+
代码生成功能在 Hugging Face Spaces 的线上部署环境中,于生成 Gradio 应用时会触发 `AttributeError: 'Popen' object has no attribute 'is_alive'` 的运行时错误。
|
| 12 |
+
|
| 13 |
+
然而,该功能在本地开发环境中运行正常,这表明本地与线上环境之间存在不一致。
|
| 14 |
+
|
| 15 |
+
## 2. 根本原因分析
|
| 16 |
+
|
| 17 |
+
1. **Python 版本不匹配**:
|
| 18 |
+
- **线上环境**: 从错误日志分析,线上环境运行的是 Python 3.10。
|
| 19 |
+
- **本地环境**: 本地开发环境使用的是 Python 3.12 或更高版本。
|
| 20 |
+
|
| 21 |
+
2. **错误的方法调用**:
|
| 22 |
+
- `subprocess.Popen` 对象的 `.is_alive()` 方法是在 Python 3.12 中才被引入的。因此,在 3.10 环境中调用会直接导致 `AttributeError`。
|
| 23 |
+
- 代码中还使用了 `.join()` 方法,这同样是错误的。该方法属于 `multiprocessing.Process` 对象,而非 `subprocess.Popen` 对象。
|
| 24 |
+
|
| 25 |
+
## 3. 解决方案
|
| 26 |
+
|
| 27 |
+
为了确保代码在不同 Python 版本(尤其是 3.10+)下的兼容性和健壮性,执行了以下两处修改:
|
| 28 |
+
|
| 29 |
+
1. 将检查进程是否存活的 `gradio_process.is_alive()` 调用替换为 `gradio_process.poll() is None`。
|
| 30 |
+
2. 将等待进程结束的 `gradio_process.join(timeout=5)` 调用替换为 `gradio_process.wait(timeout=5)`。
|
| 31 |
+
|
| 32 |
+
## 4. 验证方式
|
| 33 |
+
|
| 34 |
+
1. **本地验证**: 在修复后,于本地完整执行了代码生成功能的测试流程,确保了:
|
| 35 |
+
- 旧的 Gradio 子进程能够被正确检测并终止。
|
| 36 |
+
- 新的代码生成和预览功能一切正常。
|
| 37 |
+
2. **代码审查**: 确认了修改后的代码遵循了 `subprocess` 模块的官方推荐用法,从根本上解决了跨版本兼容性问题。
|
tab_code.py
CHANGED
|
@@ -114,9 +114,9 @@ def generate_code(code_type, model_choice, user_prompt):
|
|
| 114 |
logger.info(f"Code Type: {code_type}, Model: {model_choice}, Prompt: '{user_prompt}'")
|
| 115 |
|
| 116 |
# If a Gradio subprocess is running, terminate it first
|
| 117 |
-
if gradio_process and gradio_process.
|
| 118 |
gradio_process.terminate()
|
| 119 |
-
gradio_process.
|
| 120 |
logger.info("Terminated existing Gradio process.")
|
| 121 |
|
| 122 |
if not user_prompt:
|
|
|
|
| 114 |
logger.info(f"Code Type: {code_type}, Model: {model_choice}, Prompt: '{user_prompt}'")
|
| 115 |
|
| 116 |
# If a Gradio subprocess is running, terminate it first
|
| 117 |
+
if gradio_process and gradio_process.poll() is None:
|
| 118 |
gradio_process.terminate()
|
| 119 |
+
gradio_process.wait(timeout=5)
|
| 120 |
logger.info("Terminated existing Gradio process.")
|
| 121 |
|
| 122 |
if not user_prompt:
|