Skip to content

[Tune] Make W&B callback cleanup safe during interpreter shutdown - #68617

Open
rangelml wants to merge 2 commits into
ray-project:masterfrom
rangelml:fix/tune-wandb-shutdown
Open

rangelml wants to merge 2 commits into
ray-project:masterfrom
rangelml:fix/tune-wandb-shutdown

Conversation

@rangelml

Copy link
Copy Markdown

Description

Why are these changes needed?

Python can clear the W&B integration's ray module reference before finalizing WandbLoggerCallback. This currently raises an unraisable AttributeError and skips clearing the callback's bookkeeping. Guard access to the Ray runtime so the callback can finish local cleanup.

Related issues

Related issue number

Closes #39332

The issue is unassigned. Searches for the issue number and W&B teardown keywords found no competing open PR.

Additional information

  • The focused regression failed on upstream/master with the reported AttributeError and passed with the fix.

  • The W&B integration test file passed (22 tests). From the checkout, with the matching Ray wheel and the air and tests packages linked to source, the command was:

    ../.venv/bin/python -c 'import sys; import ray; import pytest; sys.exit(pytest.main(sys.argv[1:]))' -q python/ray/air/tests/test_integration_wandb.py
  • Ruff, Black, and the applicable pre-commit hooks passed.

AI assistance was used for triage, implementation, and testing.

@rangelml
rangelml requested a review from a team as a code owner November 28, 2026 03:16

@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 prevents potential errors during teardown by checking if the ray module is not None before calling ray.is_initialized() in WandbLoggerCallback.__del__. It also adds a corresponding unit test and refactors the ray_start_2_cpus fixture to be used explicitly. The reviewer suggested a valuable improvement to also check for the existence of _trial_logging_actors using hasattr to avoid AttributeErrors if the object is only partially initialized due to an exception in __init__.


def __del__(self):
if ray.is_initialized():
if ray is not None and ray.is_initialized():

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

If WandbLoggerCallback.__init__ raises an exception (for example, if wandb is not installed, which raises a RuntimeError at the very beginning of __init__), the object is only partially initialized. When the garbage collector subsequently calls __del__, accessing self._trial_logging_actors or calling self._cleanup_logging_actors will raise an AttributeError if Ray is initialized. Adding a check for hasattr(self, "_trial_logging_actors") prevents this unraisable exception during cleanup.

Suggested change
if ray is not None and ray.is_initialized():
if ray is not None and ray.is_initialized() and hasattr(self, "_trial_logging_actors"):

Address review feedback: if __init__ raises (e.g. wandb is not
installed), __del__ would hit AttributeError on missing attributes.

Signed-off-by: rangelml <[email protected]>
@ray-gardener ray-gardener Bot added tune Tune-related issues community-contribution Contributed by the community labels Nov 28, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community tune Tune-related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[tune] WandbLoggerCallback: AttributeError: 'NoneType' object has no attribute 'is_initialized'

1 participant