-
Notifications
You must be signed in to change notification settings - Fork 11
workflow: fix replay failures for disposed hooks #419
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions
1
changes/vercel-workflow/20260925-disposed-hook-replay.bugfix.md
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 @@ | ||
| Fix workflow replay failures when a hook is disposed before its recorded registration is replayed. |
155 changes: 155 additions & 0 deletions
155
src/vercel-workflow/tests/unit/test_workflow_disposed_hook_replay.py
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,155 @@ | ||
| import asyncio | ||
| import dataclasses | ||
| from datetime import datetime, timezone | ||
|
|
||
| import pytest | ||
|
|
||
| from tests.payloads import PLAIN_ENCODER | ||
| from vercel.workflow._internal import core, errors, runtime, world as w | ||
|
|
||
| TOKEN = "disposed-hook" | ||
| registry = core.Workflows(as_vercel_job=False) | ||
|
|
||
|
|
||
| @dataclasses.dataclass | ||
| class Payload(core.BaseHook): | ||
| value: str | ||
|
|
||
|
|
||
| def _context() -> runtime.WorkflowOrchestratorContext: | ||
| return runtime.WorkflowOrchestratorContext( | ||
| [], | ||
| run_id="wrun_test", | ||
| seed="seed", | ||
| started_at=0, | ||
| registry=registry, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("outcome", ["created", "conflict", "legacy_conflict"]) | ||
| async def test_disposal_does_not_abandon_a_pending_registration(outcome: str) -> None: | ||
| context = _context() | ||
| hook_id = context.create_hook(TOKEN, Payload)._correlation_id | ||
| hook = context.hooks[hook_id] | ||
| registration = asyncio.create_task(context.run_hook_conflict(correlation_id=hook_id)) | ||
| await asyncio.sleep(0) | ||
| assert not registration.done() | ||
|
|
||
| try: | ||
| context.dispose_hook(correlation_id=hook_id) | ||
| assert not registration.done() | ||
|
|
||
| context.events.append( | ||
| w.HookCreatedEventData(token=TOKEN).into_event(hook_id) | ||
| if outcome == "created" | ||
| else w.HookConflictEvent( | ||
| correlation_id=hook_id, | ||
| event_data=w.HookConflictEventData( | ||
| token=TOKEN, | ||
| conflicting_run_id="wrun_owner" if outcome == "conflict" else None, | ||
| ), | ||
| ) | ||
| ) | ||
| context.resume() | ||
| await asyncio.sleep(0) | ||
| assert registration.done() | ||
| if outcome == "created": | ||
| assert await registration is None | ||
| assert hook.has_created_event | ||
| assert hook.conflict_error is None | ||
| elif outcome == "conflict": | ||
| owner = await registration | ||
| assert owner is not None | ||
| assert owner.run_id == "wrun_owner" | ||
| assert isinstance(hook.conflict_error, errors.HookConflictError) | ||
| else: | ||
| with pytest.raises(errors.HookConflictError): | ||
| await registration | ||
|
|
||
| assert hook.disposed | ||
| assert hook_id not in context.suspensions | ||
| assert context.replay_index == len(context.events) | ||
| finally: | ||
| registration.cancel() | ||
| await asyncio.gather(registration, return_exceptions=True) | ||
|
|
||
|
|
||
| async def test_disposed_hook_still_discards_recorded_payloads() -> None: | ||
| context = _context() | ||
| hook_id = context.create_hook(TOKEN, Payload)._correlation_id | ||
| context.dispose_hook(correlation_id=hook_id) | ||
| context.events.extend( | ||
| [ | ||
| w.HookCreatedEventData(token=TOKEN).into_event(hook_id), | ||
| w.HookReceivedEventData( | ||
| token=TOKEN, payload=PLAIN_ENCODER.encode({"value": "discarded"}) | ||
| ).into_event(hook_id), | ||
| w.HookDisposedEvent(correlation_id=hook_id), | ||
| ] | ||
| ) | ||
|
|
||
| for _ in context.events: | ||
| context.resume() | ||
|
|
||
| assert not context.hooks[hook_id].buffered_results | ||
| assert context.hooks[hook_id].has_dispose_event | ||
| assert hook_id not in context.suspensions | ||
| with pytest.raises(StopAsyncIteration): | ||
| await context.run_hook(correlation_id=hook_id) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("recorded_kind", ["step", "wait"]) | ||
| async def test_disposed_hook_still_occupies_its_recorded_position(recorded_kind: str) -> None: | ||
| context = _context() | ||
| hook_id = context.create_hook(TOKEN, Payload)._correlation_id | ||
| position = hook_id.split("_", 1)[1] | ||
| event = ( | ||
| w.StepCreatedEventData( | ||
| step_name="previous_step", input=PLAIN_ENCODER.encode([]) | ||
| ).into_event(f"step_{position}") | ||
| if recorded_kind == "step" | ||
| else w.WaitCreatedEventData(resume_at=datetime(2026, 1, 1, tzinfo=timezone.utc)).into_event( | ||
| f"wait_{position}" | ||
| ) | ||
| ) | ||
| context.events.append(event) | ||
| context.dispose_hook(correlation_id=hook_id) | ||
|
|
||
| async def replay() -> None: | ||
| context.resume() | ||
|
|
||
| try: | ||
| runtime._run_isolated(replay(), loop_factory=asyncio.new_event_loop) | ||
| except asyncio.CancelledError: | ||
| pass | ||
|
|
||
| assert isinstance(context.resume_exception, runtime.NondeterminismError) | ||
| assert str(context.resume_exception) == ( | ||
| f"workflow replay diverged at position {position}: recorded a {recorded_kind!r} call, " | ||
| "but the body now issues a 'hook' call. The workflow body is non-deterministic." | ||
| ) | ||
| assert context.suspended | ||
|
|
||
|
|
||
| async def test_cancellation_hook_replays_without_a_user_hook() -> None: | ||
| context = _context() | ||
| cancellation = runtime.Cancellation( | ||
| correlation_id="hook_1", token="abrt_1", step_id="step_1", requested=True | ||
| ) | ||
| context.suspensions[cancellation.correlation_id] = cancellation | ||
| context.events.extend( | ||
| [ | ||
| w.HookCreatedEventData(token=cancellation.token).into_event( | ||
| cancellation.correlation_id | ||
| ), | ||
| w.HookReceivedEventData(payload=PLAIN_ENCODER.encode(None)).into_event( | ||
| cancellation.correlation_id | ||
| ), | ||
| ] | ||
| ) | ||
|
|
||
| context.resume() | ||
| assert cancellation.has_created_event | ||
| assert not context.hooks | ||
| context.resume() | ||
| assert not context.suspensions |
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.
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.
Should we put the step-cancellation hooks in hooks instead/also?
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.
Yes! Let me do that in a new PR.