[Bug] MetricBuffer overflow causes PanicException ("You must initialize the reference before using it") on a later retrieve_updates()
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 新手友好度
- 58/100
- Issue 類型
- 缺陷
- 描述清晰度
- 描述清楚
- 活躍度
- 活躍
- 技術堆疊
- python
- 領域
- backend, observability
研究方向
The panic is reported at sdk-core/crates/common/src/telemetry/metrics/core.rs:121:14, while the reproduction uses Python's MetricBuffer and retrieve_updates(). Start by tracing how overflow affects the metrics buffer reference and how retrieve_updates() drains it. Done means the buffer can overflow and later drains return updates without panicking, as described in the reproduction.
由索引模型根據 Issue 內容生成。
描述
What are you really trying to do?
Export SDK and workflow metrics through temporalio.runtime.MetricBuffer, draining it periodically with retrieve_updates() from a background asyncio task.
Describe the bug
When a MetricBuffer fills up between drains, core logs Core's metrics buffer is full! Dropping call to record metrics for each dropped update. That part is expected.
After an overflow:
- The first
retrieve_updates()returns only part of the updates. In the reproduction below, aMetricBuffer(2000)filled by 300 workflows each adding to one counter returns 695 updates containing 3 of the 300 counter increments. - A later
retrieve_updates()raisespyo3_runtime.PanicException: You must initialize the reference before using it, panicking atsdk-core/crates/common/src/telemetry/metrics/core.rs:121:14.
PanicException derives from BaseException, so an except Exception around the drain loop does not catch it. The drain task dies, the buffer is never drained again, and every later metric is dropped for the life of the process, with one ERROR log line per dropped update.
Expected: overflow drops updates (as logged), but later drains keep working and return consistent updates, with no panic.
The MetricBuffer docstring describes overflow as a drop plus a logged error:
It is important that the buffer size is set to a high number and that
retrieve_updatesis called regularly to drain the buffer. If the buffer is full, metric updates will be dropped and an error will be logged.
Minimal Reproduction
The script below uses a small buffer and doesn't drain while the first 300 workflows run, so that it overflows on purpose. We first hit it with MetricBuffer(50_000) drained every 60 seconds, when a burst of workflow activity filled the buffer between two drains.
Run with uv run --with temporalio==1.35.0 python -I repro.py. It uses WorkflowEnvironment.start_local(), so no separate server is needed.
import asyncio
from temporalio import workflow
from temporalio.runtime import MetricBuffer, Runtime, TelemetryConfig
from temporalio.testing import WorkflowEnvironment
from temporalio.worker import UnsandboxedWorkflowRunner, Worker
@workflow.defn
class MyWorkflow:
@workflow.run
async def run(self) -> None:
workflow.metric_meter().create_counter("my_counter").add(1)
async def main() -> None:
buffer = MetricBuffer(2000)
runtime = Runtime(telemetry=TelemetryConfig(metrics=buffer))
async with await WorkflowEnvironment.start_local(runtime=runtime) as env:
async with Worker(
env.client,
task_queue="my-task-queue",
workflows=[MyWorkflow],
workflow_runner=UnsandboxedWorkflowRunner(),
):
async def run_workflows(prefix: str, count: int) -> None:
await asyncio.gather(
*[
env.client.execute_workflow(
MyWorkflow.run,
id=f"{prefix}-{i}",
task_queue="my-task-queue",
)
for i in range(count)
]
)
await run_workflows("first", 300)
for attempt in (1, 2):
updates = buffer.retrieve_updates()
count = sum(1 for u in updates if u.metric.name == "my_counter")
print(f"retrieve_updates #{attempt}: {len(updates)} updates, my_counter={count}")
await run_workflows(f"after-{attempt}", 20)
asyncio.run(main())
Output (the Core's metrics buffer is full! ERROR lines, 8305 in my run, are omitted):
retrieve_updates #1: 695 updates, my_counter=3
thread '<unnamed>' panicked at sdk-core/crates/common/src/telemetry/metrics/core.rs:121:14:
...
pyo3_runtime.PanicException: You must initialize the reference before using it
The panic is raised by the second retrieve_updates(). In this script the first 300 workflows run without any drain, and a further 20 workflows run between the two drains.
Environment/Versions
- OS and processor: macOS arm64
- Temporal Version: temporalio 1.35.0 (Python SDK, from PyPI); Temporal CLI 1.9.1 dev server (Server 1.32.0) started by
WorkflowEnvironment.start_local() - Python: 3.14.2
- Are you using Docker or Kubernetes or building Temporal from source? No, PyPI wheel.
Additional context
None.
- 主要語言
- Python
- 星號
- 1.2k
- 分支
- 285
- 平均合併
- 1 天 21 小時
- 30 天內合併 PR
- 80
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 沒有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
temporalio/sdk-python 的其他 Issue
-
難度 4/5 3-5 天 新手友好度 18/100
temporalio/sdk-python#2080 ·
維護者通常 1 天內回覆
-
[Bug] Sandboxed datetime hints can fail decoding and drop signals可能已有人在做 @VegetarianOrc 於 2 天前認領。 未關閉sdk-python
temporalio/sdk-python#2072 · 已指派 1 人 ·
維護者通常 1 天內回覆
-
[Bug] TaskGroup cancellation of mixed Temporal operations can break replay可能已有人在做 @VegetarianOrc 於 2 天前認領。 未關閉sdk-python
temporalio/sdk-python#2069 · 已指派 1 人 ·
維護者通常 1 天內回覆
-
sdk-python
難度 4/5 3-5 天 新手友好度 22/100
temporalio/sdk-python#2067 · 2 則留言 ·
維護者通常 1 天內回覆
-
[Bug] lambda_worker otel.apply_defaults leaks a span exporter thread and gRPC channel on every warm invocation可能已有人在做 @avanish-garg 於 7 天前認領。 未關閉bug sdk-python
難度 4/5 1-2 天 新手友好度 30/100
temporalio/sdk-python#2040 ·
維護者通常 1 天內回覆
查看 temporalio/sdk-python 的全部 Issue
相似的 Issue
-
community enhancement good first issue
難度 2/5 1-3 小時 新手友好度 86/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 77/100
Hochfrequenz/sapgui.mcp#1039 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 88/100
didi/helixharness#18 ·
-
難度 2/5 1-3 小時 新手友好度 75/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 68/100
dataforgoodfr/democracy-watch#46 · 1 個 reaction ·