[Bug] Catching the ValueError from upsert_search_attributes (naive datetime) still leaves a workflow stuck retrying its task
Maintainer antworten meist innerhalb von 1 Tag
Bewertung
Dieses Issue wurde noch nicht bewertet.
Beschreibung
What are you really trying to do?
Update search attributes on a best-effort basis from workflow code. For example, an order workflow
sets OrderStatus and ShippedAt for visibility, and should keep going if the update is rejected
locally, for example because ShippedAt arrived without a timezone.
Describe the bug
Since #1882, workflow.upsert_search_attributes raises ValueError("Timezone must be present on all search attribute dates") for a naive datetime, inside the workflow, which is good. But by the time
it raises, the SDK has already added the UpsertWorkflowSearchAttributes command, so catching the
error does not undo anything:
- The command is sent with every key encoded before the bad one, plus an empty
Payload(no
metadata, no data) for the bad key, because the map entry is created before the encode call raises. workflow.info().search_attributes/typed_search_attributesare already updated for the keys
processed before the bad one (untyped form: for all keys, since the dict is updated before any
encoding).
Against the local dev server (CLI 1.9.4-time-skipping, Server 1.33.0) the workflow task completion
is rejected with BadSearchAttributes: invalid value for search attribute ShippedAt of type Datetime: value from <> and retried indefinitely, so a workflow that handled the error never
completes. The TypeError raised for a value of the wrong type for its key goes through the same
code path, and predates #1882.
workflow.upsert_memo, right next to it, already avoids this ("Converting before creating a
command so that we don't leave a partial command in case of conversion failure").
How did you hit this issue?
Reading workflow_upsert_search_attributes next to workflow_upsert_memo after #1882 added the
naive-datetime ValueError, then reproducing it against the local dev server with the script below.
Minimal Reproduction
import asyncio
import uuid
from datetime import datetime
from temporalio import workflow
from temporalio.api.enums.v1 import IndexedValueType
from temporalio.api.operatorservice.v1 import AddSearchAttributesRequest
from temporalio.common import SearchAttributeKey
from temporalio.testing import WorkflowEnvironment
from temporalio.worker import Worker
ORDER_STATUS = SearchAttributeKey.for_keyword("OrderStatus")
SHIPPED_AT = SearchAttributeKey.for_datetime("ShippedAt")
@workflow.defn
class OrderWorkflow:
@workflow.run
async def run(self) -> str:
try:
# Best-effort visibility update. The datetime has no tzinfo, e.g.
# it was parsed from an input that had no UTC offset.
workflow.upsert_search_attributes(
[
ORDER_STATUS.value_set("shipped"),
SHIPPED_AT.value_set(datetime(2026, 11, 26, 12, 0)),
]
)
except ValueError as err:
workflow.logger.warning("Skipping search attribute update: %s", err)
return f"done, info={[(p.key.name, p.value) for p in workflow.info().typed_search_attributes]}"
async def main() -> None:
async with await WorkflowEnvironment.start_local() as env:
await env.client.operator_service.add_search_attributes(
AddSearchAttributesRequest(
namespace=env.client.namespace,
search_attributes={
"OrderStatus": IndexedValueType.INDEXED_VALUE_TYPE_KEYWORD,
"ShippedAt": IndexedValueType.INDEXED_VALUE_TYPE_DATETIME,
},
)
)
async with Worker(env.client, task_queue="tq", workflows=[OrderWorkflow]):
handle = await env.client.start_workflow(
OrderWorkflow.run, id=f"order-{uuid.uuid4()}", task_queue="tq"
)
try:
print(await asyncio.wait_for(handle.result(), timeout=20))
except asyncio.TimeoutError:
print("workflow did not complete within 20s")
history = await handle.fetch_history()
print(
"upsert events:",
sum(e.HasField("upsert_workflow_search_attributes_event_attributes") for e in history.events),
"| workflow task failed events:",
sum(e.HasField("workflow_task_failed_event_attributes") for e in history.events),
)
if __name__ == "__main__":
asyncio.run(main())
Actual (main @ ce8cd481):
Skipping search attribute update: Timezone must be present on all search attribute dates (logged on every task attempt)
WARN temporalio_sdk_core::worker::workflow: Error while completing workflow activation error=code: 'Client specified an invalid argument', message: "BadSearchAttributes: invalid value for search attribute ShippedAt of type Datetime: value from <>"
WARN ... (same, repeated on each retry)
workflow did not complete within 20s
upsert events: 0 | workflow task failed events: 1
Expected: the upsert is all or nothing, like upsert_memo. When it raises, no command is sent and
workflow.info() is unchanged, so the workflow completes. Output with the fix applied:
Skipping search attribute update: Timezone must be present on all search attribute dates
done, info=[]
upsert events: 0 | workflow task failed events: 0
Environment/Versions
- OS and processor: macOS, Apple Silicon
- Temporal version:
temporaliomain @ d0b2bfeb (reproduced @ ce8cd481; the function is unchanged
since; also present in 1.37.0); dev server via
WorkflowEnvironment.start_local()(run withdev_server_download_version="v1.9.4-time-skipping",
the version the SDK's own tests pin: CLI 1.9.4-time-skipping, Server 1.33.0) - Python 3.12
Additional context
Driving the workflow instance directly with an activation (no server) shows the command the SDK
builds for the same keyword-plus-naive-datetime upsert:
indexed_fields={'CustomKeyword': b'"first"', 'CustomDatetime': b''}. The keyword is included and the
datetime is an empty payload.
The fix is to encode all values first and only then add the command and update info, the same
order workflow_upsert_memo uses. I am opening a PR that does this.
- Vorherrschende Sprache
- Python
- Sterne
- 1.3k
- Forks
- 305
- Ø Merge
- 3 T. 23 Std.
- Gemergte PRs (30 T.)
- 44
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Keine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus temporalio/sdk-python
-
[Bug] Python: replayed Local Activity timeout causes NDE (TimeoutError not wrapped in ActivityError)Evtl. vergeben @mjameswh hat das vor 1 Tag übernommen. Offenbug sdk-python
temporalio/sdk-python#2183 · 1 zugewiesene Person ·
Maintainer antworten meist innerhalb von 1 Tag
-
[Bug] workflow_streams: subscribe() raises RPCError when a publish signal lands during the continue-as-new workflow taskEvtl. vergeben @feiiiiii5 hat das vor 5 Tagen übernommen. Offen
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 52/100
temporalio/sdk-python#2170 ·
Maintainer antworten meist innerhalb von 1 Tag
-
[Bug] Deadlock interruption can leak CPython's import lock and freeze the worker (still on Python 3.13/3.14)Evtl. vergeben @tconley1428 hat das vor 21 Tagen übernommen. Offensdk-python
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 25/100
temporalio/sdk-python#2115 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Support custom peer identity verification while retaining standard TLS authenticationEvtl. vergeben @dzsquared hat das vor 23 Tagen übernommen. Offen
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 35/100
temporalio/sdk-python#2106 · 1 zugewiesene Person ·
Maintainer antworten meist innerhalb von 1 Tag
-
Cloud CI Skips Nexus TestsEvtl. wieder frei @tconley1428 hat das vor 114 Tagen übernommen, und es ist kein Pull Request offen. Offen
temporalio/sdk-python#1704 · 1 zugewiesene Person ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in temporalio/sdk-python
Ähnliche Issues
-
area:print-dispatch
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 92/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Maintainer antworten meist innerhalb von 1 Tag
-
area/geometry good first issue help wanted kind/doc-mismatch severity/4-minor
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
Apich-Organization/rssn#295 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 88/100
-
chore claimed
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
schuyler/corvidae#1424 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag