Repository navigation
Fix sandboxed datetime decoding with replay compatibility #2094
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
VegetarianOrc
wants to merge
4
commits into
main
Choose a base branch
from
amazzeo/fix-sandboxed-datetime-decoding
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d6010dc
Fix sandboxed datetime decoding with replay compatibility
VegetarianOrc 6094440
Fix replay timeout handling on Python 3.10
VegetarianOrc 229921d
Stage datetime decoding flag rollout
VegetarianOrc 15e7c90
Document workflow flag rollout versions
VegetarianOrc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add flag-controlled support for decoding sandboxed datetime type hints. The flag remains disabled by default in this release. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| """Private workflow compatibility flags and their runtime context.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Generator | ||
| from contextlib import contextmanager | ||
| from contextvars import ContextVar | ||
| from dataclasses import dataclass | ||
| from enum import IntEnum | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from temporalio.worker._workflow_instance import _WorkflowInstanceImpl | ||
|
|
||
|
|
||
| class _WorkflowLogicFlag(IntEnum): | ||
| """Flags recorded in workflow task metadata to preserve replay behavior.""" | ||
|
|
||
| # Raise cancellation errors even when the activity result is already available. | ||
| # Added in version 1.13.0. | ||
| # Not enabled by default. | ||
| RAISE_ON_CANCELLING_COMPLETED_ACTIVITY = 1 | ||
| # Process all activation jobs in one batch instead of separate batches by job type. | ||
| # Added in version 1.31.0. | ||
| # Enabled by default in version 1.36.0. | ||
| PROCESS_WORKFLOW_ACTIVATION_JOBS_AS_SINGLE_BATCH = 2 | ||
| # Recognize sandbox proxies for datetime type hints during payload decoding. | ||
| # Added on 2026-11-05. | ||
| # Not enabled by default. | ||
| # TODO: Enable DECODE_DATETIME_PROXIES by default after version 1.36.0 is released. | ||
| # Remove the explicit overrides for this flag from tests/worker/test_workflow_flags.py | ||
| # when enabling it. Then remove this reminder. | ||
| DECODE_DATETIME_PROXIES = 3 | ||
|
|
||
|
|
||
| _DEFAULT_ENABLED_WORKFLOW_LOGIC_FLAGS: frozenset[_WorkflowLogicFlag] = frozenset( | ||
| { | ||
| _WorkflowLogicFlag.PROCESS_WORKFLOW_ACTIVATION_JOBS_AS_SINGLE_BATCH, | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class _WorkflowFlagContext: | ||
| _runtime: _WorkflowInstanceImpl | ||
|
|
||
| def is_flag_enabled(self, flag: _WorkflowLogicFlag) -> bool: | ||
| """Consult live runtime state, not the state at context creation.""" | ||
| return self._runtime._workflow_logic_flag_enabled(flag) | ||
|
|
||
|
|
||
| _current_workflow_flags: ContextVar[_WorkflowFlagContext | None] = ContextVar( | ||
| "temporal_workflow_flags", default=None | ||
| ) | ||
|
|
||
|
|
||
| @contextmanager | ||
| def _workflow_flag_scope( # type:ignore[reportUnusedFunction] | ||
| *, | ||
| runtime: _WorkflowInstanceImpl, | ||
| ) -> Generator[None]: | ||
| """Expose live flag decisions without inheriting another workflow's state.""" | ||
| context = _WorkflowFlagContext( | ||
| _runtime=runtime, | ||
| ) | ||
| token = _current_workflow_flags.set(context) | ||
| try: | ||
| yield | ||
| finally: | ||
| _current_workflow_flags.reset(token) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| from datetime import datetime | ||
| from typing import Any | ||
| from unittest.mock import create_autospec | ||
|
|
||
| import pytest | ||
|
|
||
| import temporalio.converter | ||
| from temporalio._workflow_context import ( | ||
| _current_workflow_flags, | ||
| _workflow_flag_scope, | ||
| _WorkflowLogicFlag, | ||
| ) | ||
| from temporalio.worker._workflow_instance import _WorkflowInstanceImpl | ||
| from temporalio.worker.workflow_sandbox._importer import Importer | ||
| from temporalio.worker.workflow_sandbox._restrictions import ( | ||
| RestrictionContext, | ||
| SandboxRestrictions, | ||
| ) | ||
|
|
||
|
|
||
| class FlagRuntime: | ||
| def __init__(self, enabled: bool) -> None: | ||
| self.enabled = enabled | ||
| self.checked: list[_WorkflowLogicFlag] = [] | ||
| instance = create_autospec(_WorkflowInstanceImpl, instance=True) | ||
| instance._workflow_logic_flag_enabled.side_effect = ( | ||
| self._workflow_logic_flag_enabled | ||
| ) | ||
| self.instance: _WorkflowInstanceImpl = instance | ||
|
|
||
| def _workflow_logic_flag_enabled(self, flag: _WorkflowLogicFlag) -> bool: | ||
| self.checked.append(flag) | ||
| return self.enabled | ||
|
|
||
|
|
||
| def datetime_hint() -> type: | ||
| with Importer(SandboxRestrictions.default, RestrictionContext()).applied(): | ||
| from datetime import datetime as sandboxed_datetime | ||
|
|
||
| return sandboxed_datetime | ||
|
|
||
|
|
||
| def test_workflow_flag_scope_restores_context_after_exception() -> None: | ||
| outer, inner = FlagRuntime(True), FlagRuntime(False) | ||
| assert _current_workflow_flags.get() is None | ||
| with _workflow_flag_scope(runtime=outer.instance): | ||
| context = _current_workflow_flags.get() | ||
| assert context is not None | ||
| assert context.is_flag_enabled(_WorkflowLogicFlag.DECODE_DATETIME_PROXIES) | ||
| with ( | ||
| pytest.raises(ValueError), | ||
| _workflow_flag_scope(runtime=inner.instance), | ||
| ): | ||
| nested = _current_workflow_flags.get() | ||
| assert nested is not None | ||
| assert not nested.is_flag_enabled( | ||
| _WorkflowLogicFlag.DECODE_DATETIME_PROXIES | ||
| ) | ||
| raise ValueError("test cleanup") | ||
| assert _current_workflow_flags.get() is context | ||
| assert _current_workflow_flags.get() is None | ||
|
|
||
|
|
||
| def test_datetime_proxy_conversion_policy() -> None: | ||
| hint = datetime_hint() | ||
| assert hint is not datetime | ||
| value = "2021-01-02T00:00:00+00:00" | ||
| with pytest.raises(TypeError): | ||
| temporalio.converter.value_to_type(hint, value) | ||
|
|
||
| disabled = FlagRuntime(False) | ||
| with _workflow_flag_scope(runtime=disabled.instance): | ||
| with pytest.raises(TypeError): | ||
| temporalio.converter.value_to_type(hint, value) | ||
| assert disabled.checked == [_WorkflowLogicFlag.DECODE_DATETIME_PROXIES] | ||
|
|
||
| enabled = FlagRuntime(True) | ||
| with _workflow_flag_scope(runtime=enabled.instance): | ||
| assert temporalio.converter.value_to_type( | ||
| hint, value | ||
| ) == datetime.fromisoformat(value) | ||
| assert enabled.checked == [_WorkflowLogicFlag.DECODE_DATETIME_PROXIES] | ||
|
|
||
|
|
||
| def test_datetime_proxy_custom_converter_precedence() -> None: | ||
| expected_hint = datetime_hint() | ||
|
|
||
| class CustomConverter(temporalio.converter.JSONTypeConverter): | ||
| def to_typed_value(self, hint: type, value: Any) -> Any: | ||
| assert hint is expected_hint | ||
| return "custom" | ||
|
|
||
| runtime = FlagRuntime(True) | ||
| with _workflow_flag_scope(runtime=runtime.instance): | ||
| assert ( | ||
| temporalio.converter.value_to_type( | ||
| expected_hint, "input", [CustomConverter()] | ||
| ) | ||
| == "custom" | ||
| ) | ||
| assert not runtime.checked |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In other SDKs we also recorded, as comments, what each flag did, when it was added, enabled and the status
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added comments with some explanation!