xihaian251 opened a new issue, #999:
URL: https://github.com/apache/burr/issues/999

   ## Bug description
   
   When `Application.astream_result()` executes a non-streaming action that 
raises an
   exception, attaching the built-in local tracker can replace the original 
exception with
   an unrelated `KeyError`.
   
   The issue occurs because the error-handling path invokes `post_end_stream` 
even though
   `pre_start_stream` was never invoked for that action.
   
   As a result, enabling tracking changes the exception observed by the caller. 
This can
   break exception-type handling, retry logic, and error reporting.
   
   ## Steps to reproduce
   
   ```python
   import asyncio
   
   from burr.core import ApplicationBuilder, State, action
   
   
   @action(reads=[], writes=[])
   async def fail(state: State) -> State:
       raise ValueError("original failure")
   
   
   async def reproduce(tracked: bool):
       builder = (
           ApplicationBuilder()
           .with_actions(fail)
           .with_entrypoint("fail")
       )
       if tracked:
           builder = builder.with_tracker("local")
   
       app = builder.build()
   
       try:
           await app.astream_result(halt_after=["fail"])
       except Exception as exc:
           print(f"tracked={tracked}: {type(exc).__name__}: {exc}")
   
   
   async def main():
       await reproduce(False)
       await reproduce(True)
   
   
   asyncio.run(main())
   ```
   
   **Observed behavior** on `8bfd7268` (verbatim output):
   
   ```text
   tracked=False: ValueError: original failure
   tracked=True: KeyError: ('a053bdd5-67a0-482a-bd71-113d80992ac8', 'fail', 
None)
   ```
   
   The uuid is a fresh `app_id` per run; the tuple is
   `(app_id, action_name, partition_key)`. The same run also writes
   `~/.burr/default/<app_id>/`, so the first tuple element doubles as that 
directory name.
   
   Without the tracker, the caller receives the original `ValueError("original 
failure")`.
   
   **Expected behavior**
   
   The original action exception should propagate in both cases. A 
non-streaming action
   should not trigger `post_end_stream` without a corresponding 
`pre_start_stream`.
   
   ## Root cause
   
   In `burr/core/application.py`, `astream_result()` supports non-streaming 
actions
   (docstring: *"it can also be used with non-streaming actions"*). For such 
actions it
   delegates to `_astep()` without invoking `pre_start_stream`.
   
   However, its later exception handler unconditionally invokes 
`post_end_stream`.
   
   In `burr/tracking/client.py`, `LocalTrackingClient.pre_start_stream()` 
initializes the
   corresponding `stream_state` entry, while `post_end_stream()` directly looks 
up that
   entry:
   
   ```python
   stream_state = self.stream_state[app_id, action, partition_key]
   ```
   
   Because the entry does not exist in this failure path, `post_end_stream()` 
raises
   `KeyError` while the original exception is being handled.
   
   ## Verification
   
   Reproduced independently on:
   
   | Revision | Result |
   |---|---|
   | `main` — `8bfd7268` | Reproduced (`ValueError` → `KeyError`) |
   | PR #980 — `5065380` | Reproduced |
   | PR #995 — `3ff4570` | Reproduced |
   
   Also reproduced when input validation fails, when a reserved `__`-prefixed 
key is passed,
   and through `astream_iterate`. Not reproduced without a tracker, nor through
   `stream_result`, `step`, `astep`.
   
   Local environment: Windows 11, Python 3.13.1, pytest 9.1.1.
   
   A targeted patch that conditions `post_end_stream` on successful stream 
initialization
   was tested locally:
   
   - new regression tests: 2 passed (both fail on unpatched `8bfd7268`)
   - `tests/core/test_application.py`: 143 passed
   - `tests/tracking/test_local_tracking_client.py`: 18 passed
   - black / isort / flake8 (repo-pinned versions): clean
   
   These results are local verification; Linux and additional supported Python 
versions have
   not yet been tested.
   
   ## Suggested fix
   
   Only invoke `post_end_stream` in this exception path when the corresponding 
stream
   initialization has actually completed. The change should preserve existing 
cleanup
   behavior for streams that have started successfully.
   
   One edge case worth confirming is partial initialization when multiple 
`pre_start_stream`
   adapters are registered and one raises. The appropriate cleanup contract for 
partially
   initialized adapters may require separate consideration.
   
   ## Related work
   
   PR #968 addressed a different lifecycle problem: missing 
`post_run_execute_call` cleanup
   when streaming execution fails before the streaming phase.
   
   This issue concerns unmatched `pre_start_stream` / `post_end_stream` 
callbacks and the
   resulting masking of an application exception. The two concerns should 
remain separate so
   that the fix can stay focused.
   
   A targeted regression test covering a failing non-streaming action with the 
built-in
   local tracker would help prevent recurrence.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to