Skip to content

[Core] Remove implicit StreamingGeneratorExecutionContext.__dealloc__() - #64333

Merged
edoakes merged 2 commits into
ray-project:masterfrom
sampan-s-nayak:cleanup_implicit_destructor
Jun 25, 2026
Merged

edoakes merged 2 commits into
ray-project:masterfrom
sampan-s-nayak:cleanup_implicit_destructor

Conversation

@sampan-s-nayak

Copy link
Copy Markdown
Contributor

Description

Removing the implicit dealloc() as we fully want to control the object lifecycle. discussion: #63310 (comment)

@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 removes the __dealloc__ method from StreamingGeneratorExecutionContext in python/ray/_raylet.pyx. The reviewer notes that removing this destructor without wrapping the execution logic in try...finally blocks in execute_streaming_generator_sync and execute_streaming_generator_async introduces a critical resource leak or deadlock risk when exceptions are raised, and recommends adding proper exception handling to ensure the backpressure state is always torn down.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

I am having trouble creating individual review comments. Click here to see my feedback.

python/ray/_raylet.pyx (984-985)

critical

Removing __dealloc__ without introducing try...finally blocks in execute_streaming_generator_sync and execute_streaming_generator_async introduces a critical resource leak/deadlock risk.

The Issue

Previously, __dealloc__ acted as a fallback safety net to ensure _teardown_actor_backpressure_state_if_needed() was called when the context object was garbage collected. With __dealloc__ removed, the teardown is skipped in several scenarios:

  1. In execute_streaming_generator_sync:
    • If a BaseException (such as KeyboardInterrupt, SystemExit, or GeneratorExit) is raised during the generator loop, it is not caught by except Exception as e: and propagates out of the function immediately.
    • If check_status(return_status) raises an exception, the function exits early.
  2. In execute_streaming_generator_async:
    • If a BaseException is raised, it is caught, interrupt_signal_event.set() is called, and then it is re-raised, skipping the teardown at the end of the function.
    • If check_status(return_status) raises an exception, the function exits early.

In all these cases, the actor-wide backpressure slot is leaked, which can lead to deadlocks or hanging actors.

Recommended Solution

To safely remove __dealloc__, wrap the execution logic in try...finally blocks within both functions. For example:

cdef execute_streaming_generator_sync(StreamingGeneratorExecutionContext context):
    ...
    try:
        try:
            # generator execution loop...
        except Exception as e:
            report_streaming_generator_exception(context, e, gen_index, None)

        with nogil:
            return_status = context.waiter.get().WaitAllObjectsReported()
        check_status(return_status)
        if completed_normally or context.actor_backpressure_metadata.get() == NULL:
            CCoreWorkerProcess.GetCoreWorker().MarkGeneratorBackpressureTaskFinished(
                context.generator_id)
            if completed_normally and context.actor_backpressure_metadata.get() != NULL:
                context.actor_backpressure_state_owned_by_core_worker = True
    finally:
        context._teardown_actor_backpressure_state_if_needed()

And similarly for execute_streaming_generator_async.

@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 c37281f. Configure here.

Comment thread python/ray/_raylet.pyx Resolved
@sampan-s-nayak sampan-s-nayak added the go add ONLY when ready to merge, run all tests label Jun 25, 2026
@ray-gardener ray-gardener Bot added the core Issues that should be addressed in Ray Core label Jun 25, 2026
@edoakes
edoakes merged commit ba67588 into ray-project:master Jun 25, 2026
4 checks passed
limarkdcunha pushed a commit to limarkdcunha/ray that referenced this pull request Jun 30, 2026
…() (ray-project#64333)

## Description
Removing the implicit __dealloc__() as we fully want to control the
object lifecycle. discussion:
ray-project#63310 (comment)

---------

Signed-off-by: sampan <[email protected]>
Co-authored-by: sampan <[email protected]>
400Ping pushed a commit to 400Ping/ray that referenced this pull request Aug 18, 2026
…() (ray-project#64333)

## Description
Removing the implicit __dealloc__() as we fully want to control the
object lifecycle. discussion:
ray-project#63310 (comment)

---------

Signed-off-by: sampan <[email protected]>
Co-authored-by: sampan <[email protected]>
Signed-off-by: 400Ping <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Issues that should be addressed in Ray Core go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants