Repository navigation
[Data] Attach retry context as an exception note instead of rewriting exception args - #67882
Conversation
… args Signed-off-by: Ayush Kumar <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request introduces the add_exception_note utility to attach retry context to exceptions using add_note on Python 3.11+, preventing unpickling issues caused by modifying exception args. Feedback suggests adding a defensive check to ensure the exception is an instance of BaseException before calling add_note, and refactoring test_iterate_with_retry_annotates_s3_permissions to run on Python 3.10 while conditionally asserting __notes__ on Python 3.11+ to preserve test coverage.
| if sys.version_info >= (3, 11): | ||
| exc.add_note(note) |
There was a problem hiding this comment.
Add a defensive check to ensure exc is an instance of BaseException before calling add_note to prevent potential AttributeError or other exceptions if a non-exception or None is passed.
| if sys.version_info >= (3, 11): | |
| exc.add_note(note) | |
| if sys.version_info >= (3, 11) and isinstance(exc, BaseException): | |
| exc.add_note(note) |
| @pytest.mark.skipif(sys.version_info < (3, 11), reason="add_note is Python 3.11+") | ||
| @pytest.mark.parametrize("retryable", [False, True]) | ||
| def test_iterate_with_retry_annotates_s3_permissions(monkeypatch, retryable): |
There was a problem hiding this comment.
Instead of skipping the entire test on Python < 3.11, we can run the test on all Python versions and conditionally assert the __notes__ attribute only on Python 3.11+. This ensures we maintain test coverage for the S3 permissions error formatting and handling on Python 3.10.
| @pytest.mark.skipif(sys.version_info < (3, 11), reason="add_note is Python 3.11+") | |
| @pytest.mark.parametrize("retryable", [False, True]) | |
| def test_iterate_with_retry_annotates_s3_permissions(monkeypatch, retryable): | |
| @pytest.mark.parametrize("retryable", [False, True]) | |
| def test_iterate_with_retry_annotates_s3_permissions(monkeypatch, retryable): |
MengjinYan
left a comment
There was a problem hiding this comment.
util change under _common looks good
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
Reviewed by Cursor Bugbot for commit 71c2e63. Configure here.
| note: The text to attach. | ||
| """ | ||
| if sys.version_info >= (3, 11): | ||
| exc.add_note(note) |
There was a problem hiding this comment.
Exception notes do not survive pickling
Medium Severity
add_exception_note stores the retry hint via add_note, but BaseException.__reduce__ only pickles args and __dict__, not the separate __notes__ field. After a cloudpickle round trip the unpickled exception has no __notes__, so the new pickling tests fail on 3.11+ and the hint does not travel with the reconstructed cause.
Additional Locations (2)
Reviewed by Cursor Bugbot for commit 71c2e63. Configure here.


Earlier, we used to annotate retry exceptions on
iterate_with_retryin Ray Data by mutating the exception args ourselves, adding a string suffix and rewriting the args. Rewritingargsbroke unpickling for exceptions whose constructor doesn't take its ownargs, so a failed task could reach the driver as anUnserializableExceptionthat hides the real error.Related issue: #67886
Before
_annotate_exception_with_retry_context(added in #66033) appended the retry hint toexc.args._map_tasksends every failed map task throughiterate_with_retry, so this ran on every map task failure, not just UDF errors. The worker pickles the exception, and the driver rebuilds it by callingtype(exc)(*exc.args):ActorDiedError's constructor only accepts anActorDiedErrorContext, so unpickling hit anAssertionErrorand the driver raisedUnserializableException: Failed to deserialize exception. A parquet datasource V2 read whoseFooterReaderactor dies fails this way, and the actor death only appears in the stringified "Original exception" text.OSError(errno, strerror)treats a third arg as the filename, so it came back as[Errno 2] No such file or directory: 'Failed to ... after 1/1 attempts ...'.After
The hint is attached with
add_exception_note, a new helper inray._common.utilsthat callsexc.add_noteon Python 3.11+ and does nothing on 3.10. The exception keeps its type,argsandstr(), and pickles normally. The hint travels in__notes__and appears in the task traceback the driver shows.Observable differences:
RayTaskError(ActorDiedError), instead ofUnserializableException.str(exc).add_note, no hint is attached.flowchart TD subgraph Before subgraph BS[Subsystem: execution] subgraph BC[Component: _map_task in the worker] B1[iterate_with_retry] --> B2[_annotate_exception_with_retry_context<br/>rewrites exc.args] end end B2 --> B3[Ray Core pickles the task error] B3 --> B4[Driver unpickles with type of exc called on exc.args] B4 --> B5[UnserializableException<br/>or corrupted OSError] end subgraph After subgraph AS[Subsystem: execution] subgraph AC[Component: _map_task in the worker] A1[iterate_with_retry] --> A2[_annotate_exception_with_retry_context<br/>calls add_exception_note] end end A2 --> A3[Ray Core pickles the task error] A3 --> A4[Driver unpickles with the original args<br/>and restores __notes__] A4 --> A5[Original exception<br/>hint shown in the traceback] end classDef removed fill:#ffcdd2,stroke:#c62828,color:#000 classDef added fill:#c8e6c9,stroke:#2e7d32,color:#000 classDef changed fill:#fff9c4,stroke:#f9a825,color:#000 class B2,B5 removed class A2 changed class A5 addedSubsystems
Execution (
_internal/util.py, theiterate_with_retrypath used by_map_task) carries the behavior change. Theray._common.utils.add_exception_notehelper is a small shared addition outside Ray Data.Testing
pytest python/ray/data/tests/test_util.py -k iterate_with_retry: 6 passed, 2 skipped. The 2 skips are the S3 hint test, which now checks__notes__and runs on Python 3.11+ only (local env is 3.10).test_iterate_with_retry_raises_picklable_exception(ValueError,OSError(2, ...),ActorDiedError()) fails on all 3 cases against the old code and passes with this change.pytest python/ray/_common/tests/test_utils.py -k AddExceptionNote: 1 passed, 1 skipped. The skipped case is 3.11+ only.FooterReaderactor mid-listing. Before this change the driver gotUnserializableException. With it, the driver getsRayTaskError(ActorDiedError). The dataset still fails, which is expected because this change is about error reporting, not fault tolerance.pre-commit runpasses on all changed files.Notes
_annotate_exception_with_retry_contextanditerate_with_retryand found only [data] Make iterate_with_retry more descriptive when throwing exceptions #66033, which introduced the annotation. This follows up on a review comment there about rewriting exceptionargs.