Repository navigation
[Core] Remove implicit StreamingGeneratorExecutionContext.__dealloc__() - #64333
Conversation
Signed-off-by: sampan <[email protected]>
There was a problem hiding this comment.
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)
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:
- In
execute_streaming_generator_sync:- If a
BaseException(such asKeyboardInterrupt,SystemExit, orGeneratorExit) is raised during the generator loop, it is not caught byexcept Exception as e:and propagates out of the function immediately. - If
check_status(return_status)raises an exception, the function exits early.
- If a
- In
execute_streaming_generator_async:- If a
BaseExceptionis 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.
- If a
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.
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 c37281f. Configure here.
Signed-off-by: sampan <[email protected]>
…() (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]>
…() (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]>

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