Skip to content

[Data] Attach retry context as an exception note instead of rewriting exception args - #67882

Merged
iamjustinhsu merged 2 commits into
ray-project:masterfrom
ayushk7102:annotate_exception_note
Nov 7, 2026
Merged

iamjustinhsu merged 2 commits into
ray-project:masterfrom
ayushk7102:annotate_exception_note

Conversation

@ayushk7102

@ayushk7102 ayushk7102 commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Earlier, we used to annotate retry exceptions on iterate_with_retry in Ray Data by mutating the exception args ourselves, adding a string suffix and rewriting the args. Rewriting args broke unpickling for exceptions whose constructor doesn't take its own args, so a failed task could reach the driver as an UnserializableException that hides the real error.

Related issue: #67886

Before

_annotate_exception_with_retry_context (added in #66033) appended the retry hint to exc.args. _map_task sends every failed map task through iterate_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 calling type(exc)(*exc.args):

  • ActorDiedError's constructor only accepts an ActorDiedErrorContext, so unpickling hit an AssertionError and the driver raised UnserializableException: Failed to deserialize exception. A parquet datasource V2 read whose FooterReader actor 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 in ray._common.utils that calls exc.add_note on Python 3.11+ and does nothing on 3.10. The exception keeps its type, args and str(), and pickles normally. The hint travels in __notes__ and appears in the task traceback the driver shows.

Observable differences:

  • The driver gets the real exception, for example RayTaskError(ActorDiedError), instead of UnserializableException.
  • The retry hint now appears in the traceback, not in str(exc).
  • On Python 3.10, which has no 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 added
Loading

Subsystems

Execution (_internal/util.py, the iterate_with_retry path used by _map_task) carries the behavior change. The ray._common.utils.add_exception_note helper 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).
  • The new 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.
  • End to end, on a local multi-node cluster I killed the V2 parquet FooterReader actor mid-listing. Before this change the driver got UnserializableException. With it, the driver gets RayTaskError(ActorDiedError). The dataset still fails, which is expected because this change is about error reporting, not fault tolerance.
  • pre-commit run passes on all changed files.

Notes

  • Not a duplicate. I searched open and closed PRs for _annotate_exception_with_retry_context and iterate_with_retry and 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 exception args.
  • AI assistance (Claude Code) was used to investigate the bug and draft this change. I reviewed every changed line and ran the tests above.

@ayushk7102
ayushk7102 requested review from a team, MengjinYan and edoakes as code owners November 6, 2026 01:09

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +287 to +288
if sys.version_info >= (3, 11):
exc.add_note(note)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
if sys.version_info >= (3, 11):
exc.add_note(note)
if sys.version_info >= (3, 11) and isinstance(exc, BaseException):
exc.add_note(note)

Comment on lines +374 to 376
@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):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
@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):

@ayushk7102 ayushk7102 changed the title [Data] Attach retry context as an exception note instead of rewriting… [Data] Attach retry context as an exception note instead of rewriting exception args Nov 6, 2026
@ray-gardener ray-gardener Bot added the data Ray Data-related issues label Nov 6, 2026
@sampan-s-nayak sampan-s-nayak added the go add ONLY when ready to merge, run all tests label Nov 6, 2026

@sampan-s-nayak sampan-s-nayak left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@MengjinYan MengjinYan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

util change under _common looks good

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 71c2e63. Configure here.

note: The text to attach.
"""
if sys.version_info >= (3, 11):
exc.add_note(note)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 71c2e63. Configure here.

@iamjustinhsu
iamjustinhsu merged commit 4e9895b into ray-project:master Nov 7, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

data Ray Data-related issues go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants