fix(test): 根治 test_github_webhook.py segfault — 改用最小化 app
Some checks failed
CD Pipeline / build-and-deploy (push) Has been cancelled

根本原因:
  from src.main import app
  → import 整個 FastAPI 應用所有路由
  → src.api.v1.knowledge → knowledge_service → knowledge_repository
  → sqlalchemy.ext.asyncio (C extension) → asyncpg.protocol.protocol
  → CI runner (catthehacker/ubuntu:act-22.04) segfault (exit 139)

修復:
  改用只掛載 github_webhook router 的最小化 FastAPI app
  github_webhook 的 import chain: config → redis_client → structlog
  完全不走 DB / sqlalchemy / asyncpg,無 C extension segfault 風險

結果:
  - test_github_webhook.py 恢復進入 CI 測試
  - 移除 cd.yaml 中 --ignore=tests/test_github_webhook.py
  - HMAC 簽章、whitelist、事件類型等 8 個測試全部覆蓋

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
OG T
2026-04-05 13:36:24 +08:00
parent b663d5ef69
commit b5905ae283
2 changed files with 13 additions and 5 deletions

View File

@@ -106,9 +106,10 @@ jobs:
# 2026-04-05 Claude Code: 加 --ignore=tests/integration 排除需 asyncpg 連線的 DB 測試
# integration tests 在 prod K8s 部署後由 E2E Smoke Test 覆蓋
# PYTHONFAULTHANDLER=1: 若 C extension segfault輸出完整 Python stacktrace
# test_github_webhook.py: import src.main → 觸發 asyncpg C ext → CI segfault (exit 139)
# src.main 在 collection 時初始化 SQLAlchemy async engine (asyncpg C extension)
# 此測試在 prod E2E Smoke Test 覆蓋 (awoooi.wooo.work)
# 2026-04-05 Claude Code: test_github_webhook.py 已根治
# 原問題: import src.main → asyncpg C ext segfault (exit 139)
# 修復: 改用最小化 app只掛載 github_webhook router不走 DB import chain
# 現在可安全加入 CI 測試
PYTHONFAULTHANDLER=1 python3.11 -m pytest tests/ -v --tb=short -x \
--ignore=tests/integration \
--ignore=tests/test_anomaly_counter.py \
@@ -116,7 +117,6 @@ jobs:
--ignore=tests/test_redis_multisig.py \
--ignore=tests/test_model_regression.py \
--ignore=tests/test_prompt_validation.py \
--ignore=tests/test_github_webhook.py \
--ignore=tests/e2e_network_test.py \
2>&1 | tee /tmp/pytest-output.txt; PYTEST_EXIT=${PIPESTATUS[0]}
tail -60 /tmp/pytest-output.txt

View File

@@ -17,9 +17,17 @@ import json
import httpx
import pytest
from fastapi import FastAPI
from httpx import ASGITransport
from src.main import app
# 2026-04-05 Claude Code: 改用最小化 app只掛載 github_webhook router
# 原 `from src.main import app` 會 import 整個應用,觸發 sqlalchemy.ext.asyncio
# C extension (asyncpg.protocol.protocol) 在 CI runner 上 segfault (exit 139)
# github_webhook router 的 import chain 不走 DB可獨立測試
from src.api.v1.github_webhook import router as github_webhook_router
app = FastAPI()
app.include_router(github_webhook_router, prefix="/api/v1")
# 環境變數設定已移至 conftest.py (解決 E402)