-
Notifications
You must be signed in to change notification settings - Fork 119
fix: graceful stream shutdown on exceptions in streaming actions #680
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
andreahlert
wants to merge
1
commit into
apache:main
Choose a base branch
from
andreahlert:issue-581-repro
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
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 |
|---|---|---|
|
|
@@ -338,31 +338,35 @@ def _run_single_step_streaming_action( | |
| result = None | ||
| state_update = None | ||
| count = 0 | ||
| for item in generator: | ||
| if not isinstance(item, tuple): | ||
| # TODO -- consider adding support for just returning a result. | ||
| raise ValueError( | ||
| f"Action {action.name} must yield a tuple of (result, state_update). " | ||
| f"For all non-final results (intermediate)," | ||
| f"the state update must be None" | ||
| ) | ||
| result, state_update = item | ||
| count += 1 | ||
| try: | ||
| for item in generator: | ||
| if not isinstance(item, tuple): | ||
| # TODO -- consider adding support for just returning a result. | ||
| raise ValueError( | ||
| f"Action {action.name} must yield a tuple of (result, state_update). " | ||
| f"For all non-final results (intermediate)," | ||
| f"the state update must be None" | ||
| ) | ||
| result, state_update = item | ||
| count += 1 | ||
| if state_update is None: | ||
| if first_stream_start_time is None: | ||
| first_stream_start_time = system.now() | ||
| lifecycle_adapters.call_all_lifecycle_hooks_sync( | ||
| "post_stream_item", | ||
| item=result, | ||
| item_index=count, | ||
| stream_initialize_time=stream_initialize_time, | ||
| first_stream_item_start_time=first_stream_start_time, | ||
| action=action.name, | ||
| app_id=app_id, | ||
| partition_key=partition_key, | ||
| sequence_id=sequence_id, | ||
| ) | ||
| yield result, None | ||
| except Exception: | ||
| if state_update is None: | ||
| if first_stream_start_time is None: | ||
| first_stream_start_time = system.now() | ||
| lifecycle_adapters.call_all_lifecycle_hooks_sync( | ||
| "post_stream_item", | ||
| item=result, | ||
| item_index=count, | ||
| stream_initialize_time=stream_initialize_time, | ||
| first_stream_item_start_time=first_stream_start_time, | ||
| action=action.name, | ||
| app_id=app_id, | ||
| partition_key=partition_key, | ||
| sequence_id=sequence_id, | ||
| ) | ||
| yield result, None | ||
| raise | ||
|
|
||
| if state_update is None: | ||
| raise ValueError( | ||
|
|
@@ -391,31 +395,36 @@ async def _arun_single_step_streaming_action( | |
| result = None | ||
| state_update = None | ||
| count = 0 | ||
| async for item in generator: | ||
| if not isinstance(item, tuple): | ||
| # TODO -- consider adding support for just returning a result. | ||
| raise ValueError( | ||
| f"Action {action.name} must yield a tuple of (result, state_update). " | ||
| f"For all non-final results (intermediate)," | ||
| f"the state update must be None" | ||
| ) | ||
| result, state_update = item | ||
| try: | ||
| async for item in generator: | ||
| if not isinstance(item, tuple): | ||
| # TODO -- consider adding support for just returning a result. | ||
| raise ValueError( | ||
| f"Action {action.name} must yield a tuple of (result, state_update). " | ||
| f"For all non-final results (intermediate)," | ||
| f"the state update must be None" | ||
| ) | ||
| result, state_update = item | ||
| if state_update is None: | ||
| if first_stream_start_time is None: | ||
| first_stream_start_time = system.now() | ||
| await lifecycle_adapters.call_all_lifecycle_hooks_sync_and_async( | ||
| "post_stream_item", | ||
| item=result, | ||
| item_index=count, | ||
| stream_initialize_time=stream_initialize_time, | ||
| first_stream_item_start_time=first_stream_start_time, | ||
| action=action.name, | ||
| app_id=app_id, | ||
| partition_key=partition_key, | ||
| sequence_id=sequence_id, | ||
| ) | ||
| count += 1 | ||
| yield result, None | ||
| except Exception: | ||
| if state_update is None: | ||
| if first_stream_start_time is None: | ||
| first_stream_start_time = system.now() | ||
| await lifecycle_adapters.call_all_lifecycle_hooks_sync_and_async( | ||
| "post_stream_item", | ||
| item=result, | ||
| item_index=count, | ||
| stream_initialize_time=stream_initialize_time, | ||
| first_stream_item_start_time=first_stream_start_time, | ||
| action=action.name, | ||
| app_id=app_id, | ||
| partition_key=partition_key, | ||
| sequence_id=sequence_id, | ||
| ) | ||
| count += 1 | ||
| yield result, None | ||
| raise | ||
|
|
||
| if state_update is None: | ||
| raise ValueError( | ||
| f"Action {action.name} did not return a state update. For async actions, the last yield " | ||
|
|
@@ -450,28 +459,34 @@ def _run_multi_step_streaming_action( | |
| result = None | ||
| first_stream_start_time = None | ||
| count = 0 | ||
| for item in generator: | ||
| # We want to peek ahead so we can return the last one | ||
| # This is slightly eager, but only in the case in which we | ||
| # are using a multi-step streaming action | ||
| next_result = result | ||
| result = item | ||
| if next_result is not None: | ||
| if first_stream_start_time is None: | ||
| first_stream_start_time = system.now() | ||
| lifecycle_adapters.call_all_lifecycle_hooks_sync( | ||
| "post_stream_item", | ||
| item=next_result, | ||
| item_index=count, | ||
| stream_initialize_time=stream_initialize_time, | ||
| first_stream_item_start_time=first_stream_start_time, | ||
| action=action.name, | ||
| app_id=app_id, | ||
| partition_key=partition_key, | ||
| sequence_id=sequence_id, | ||
| ) | ||
| count += 1 | ||
| yield next_result, None | ||
| caught_exc = None | ||
| try: | ||
| for item in generator: | ||
| # We want to peek ahead so we can return the last one | ||
| # This is slightly eager, but only in the case in which we | ||
| # are using a multi-step streaming action | ||
| next_result = result | ||
| result = item | ||
| if next_result is not None: | ||
| if first_stream_start_time is None: | ||
| first_stream_start_time = system.now() | ||
| lifecycle_adapters.call_all_lifecycle_hooks_sync( | ||
| "post_stream_item", | ||
| item=next_result, | ||
| item_index=count, | ||
| stream_initialize_time=stream_initialize_time, | ||
| first_stream_item_start_time=first_stream_start_time, | ||
| action=action.name, | ||
| app_id=app_id, | ||
| partition_key=partition_key, | ||
| sequence_id=sequence_id, | ||
| ) | ||
| count += 1 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| yield next_result, None | ||
| except Exception as e: | ||
| if result is None: | ||
| raise | ||
| caught_exc = e | ||
| state_update = _run_reducer(action, state, result, action.name) | ||
| _validate_result(result, action.name, action.schema) | ||
| _validate_reducer_writes(action, state_update, action.name) | ||
|
|
@@ -494,28 +509,34 @@ async def _arun_multi_step_streaming_action( | |
| result = None | ||
| first_stream_start_time = None | ||
| count = 0 | ||
| async for item in generator: | ||
| # We want to peek ahead so we can return the last one | ||
| # This is slightly eager, but only in the case in which we | ||
| # are using a multi-step streaming action | ||
| next_result = result | ||
| result = item | ||
| if next_result is not None: | ||
| if first_stream_start_time is None: | ||
| first_stream_start_time = system.now() | ||
| await lifecycle_adapters.call_all_lifecycle_hooks_sync_and_async( | ||
| "post_stream_item", | ||
| item=next_result, | ||
| stream_initialize_time=stream_initialize_time, | ||
| item_index=count, | ||
| first_stream_item_start_time=first_stream_start_time, | ||
| action=action.name, | ||
| app_id=app_id, | ||
| partition_key=partition_key, | ||
| sequence_id=sequence_id, | ||
| ) | ||
| count += 1 | ||
| yield next_result, None | ||
| caught_exc = None | ||
| try: | ||
| async for item in generator: | ||
| # We want to peek ahead so we can return the last one | ||
| # This is slightly eager, but only in the case in which we | ||
| # are using a multi-step streaming action | ||
| next_result = result | ||
| result = item | ||
| if next_result is not None: | ||
| if first_stream_start_time is None: | ||
| first_stream_start_time = system.now() | ||
| await lifecycle_adapters.call_all_lifecycle_hooks_sync_and_async( | ||
| "post_stream_item", | ||
| item=next_result, | ||
| stream_initialize_time=stream_initialize_time, | ||
| item_index=count, | ||
| first_stream_item_start_time=first_stream_start_time, | ||
| action=action.name, | ||
| app_id=app_id, | ||
| partition_key=partition_key, | ||
| sequence_id=sequence_id, | ||
| ) | ||
| count += 1 | ||
| yield next_result, None | ||
| except Exception as e: | ||
| if result is None: | ||
| raise | ||
| caught_exc = e | ||
| state_update = _run_reducer(action, state, result, action.name) | ||
| _validate_result(result, action.name, action.schema) | ||
| _validate_reducer_writes(action, state_update, action.name) | ||
|
|
||
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.
Worth discussing: when the generator crashes after yielding some items but before its "final" result, this guard (
if result is None) means the reducer runs on whatever partial item was last received. In contrast, the single-step guard (if state_update is None) verifies the generator actually fulfilled its contract. Should this also log a warning when the exception is caught with a non-None result, so users know the reducer ran on potentially incomplete data?