Skip to content

DDP tests: drop leaked rank env for the child runs, fail a hung child with its output (rest-2 timeouts) - #18180

Merged
danielhanchen merged 2 commits into
mainfrom
ci-ddp-test-clean-dist-env
Nov 17, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
ci-ddp-test-clean-dist-env

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

tests/test_ddp_find_unused_default.py::test_trainable_embeddings_train_under_ddp_reentrant_checkpointing hits pytest-timeout in the CPU rest-2 shard on unrelated PRs (#18135, #18165, #18171 today):

tests/test_ddp_find_unused_default.py:285: in test_trainable_embeddings_train_under_ddp_reentrant_checkpointing
    proc = subprocess.run(
E   Failed: Timeout (>330.0s) from pytest-timeout.

The test takes about 11 s locally and hung only on some workers, with no child output in the log.

Cause

The test copies os.environ into both children: the single-process reference and the torchrun pair. The worker decides it is distributed from WORLD_SIZE. Several tests write rank variables (RANK, WORLD_SIZE, MASTER_PORT, ...) into the pytest process environment, so on a worker that ran one of them first, the single-process reference sees WORLD_SIZE=2, calls init_process_group("gloo") and waits for a second rank that never comes. It never returns, and the child's timeout = 600 is longer than pytest-timeout's 330 s, so CI only showed a bare timeout.

Reproduced on main by running the file with WORLD_SIZE=2 RANK=0 MASTER_ADDR=127.0.0.1 MASTER_PORT=29555 in the environment: it hangs until killed.

Fix

  • _rank_env(tmp_path) builds the child environment without RANK, WORLD_SIZE, LOCAL_RANK, LOCAL_WORLD_SIZE, GROUP_RANK, ROLE_RANK, MASTER_ADDR, MASTER_PORT (torchrun sets its own) and the UNSLOTH_DDP_FIND_UNUSED_PARAMETERS_AUTO override the test already dropped.
  • _run_child runs each child under a timeout below pytest-timeout (150 s each for the two-step test, 300 s for the single torchrun in the VLM test) and, if one still hangs, fails with the tail of its output instead of a bare timeout.
  • Both DDP tests in the file use them.

Verification

  • tests/test_ddp_find_unused_default.py: 63 passed.
  • Same file with WORLD_SIZE=2 RANK=0 MASTER_ADDR=127.0.0.1 MASTER_PORT=29555 exported: 63 passed (main: hangs).

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T16:34:45.691260Z 2c34536 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0664c02914

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tests/test_ddp_find_unused_default.py Outdated
Comment on lines +522 to +523
out = (e.stdout or "") + (e.stderr or "")
out = out.decode(errors = "replace") if isinstance(out, bytes) else out

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Normalize timeout output streams before concatenating them

When a timed-out child writes to only one stream, TimeoutExpired contains bytes for that stream and None for the other, even with text=True. Replacing the missing stream with "" then concatenating raises TypeError before decoding or pytest.fail runs, losing the diagnostic output this helper is intended to preserve. This reproduces with both stdout-only and stderr-only children. Decode each stream separately before concatenating, or use byte fallbacks for both streams.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 2c34536: right, TimeoutExpired keeps raw bytes even with text = True and None for a stream that wrote nothing. Each stream is now decoded on its own before joining. Checked with a child that writes only to stdout and one that writes only to stderr: both now reach pytest.fail with their output.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 2c34536beb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit e1453ef into main Nov 17, 2026
@danielhanchen
danielhanchen deleted the ci-ddp-test-clean-dist-env branch November 17, 2026 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant