Skip to content

[Data] Fix read_mcap planning deadlock behind flaky test_mcap_data_column - #68419

Merged
goutamvenkat-anyscale merged 2 commits into
ray-project:masterfrom
AarryaSaraf:aarrya/deflake-test-mcap-data-column
Nov 23, 2026
Merged

goutamvenkat-anyscale merged 2 commits into
ray-project:masterfrom
AarryaSaraf:aarrya/deflake-test-mcap-data-column

Conversation

@AarryaSaraf

Copy link
Copy Markdown
Contributor

Summary

read_mcap planning can deadlock on Python 3.12 when it skips a file that isn't MCAP and then stops early. That is why test_mcap_data_column times out intermittently: postmerge build 21143 shows TIMEOUT in 2 out of 2, both attempts stuck in test_unreadable_sampled_file_is_skipped, while the file normally passes in 3–5 s. read_summaries now shuts its thread pool down without joining the workers.

Before

read_summaries (mcap_summary.py) is a generator that owns a ThreadPoolExecutor in a with block, so closing it joins the workers. Since #67930 ("read neighbouring chunks in one ranged request"), a failed summary read leaves its error in a reference cycle: the section task re-raises the tail read's error, and that error's traceback holds the task's frame, which holds the tail future. The traceback also holds the _summaries frame, whose sections local holds the generator.

So when _FileInspector.walk returns early after skipping such a file, the generator is closed later by the cyclic garbage collector, on whichever thread triggers it. Every new thread calls threading._maintain_shutdown_locks while holding _shutdown_locks_lock. If the collection starts there, the join waits for that same lock, and the thread hangs:

_bootstrap_inner -> _set_tstate_lock -> _maintain_shutdown_locks    # holds _shutdown_locks_lock; GC starts
  -> read_summaries: ThreadPoolExecutor.__exit__ -> shutdown -> join -> _stop    # waits for _shutdown_locks_lock

In the test file, test_walk_skips_unreadable_files_and_stops_at_its_cap leaves such a generator behind, and the next test hangs when it starts threads.

After

read_summaries creates the pool directly and calls pool.shutdown(wait=False, cancel_futures=True) in a finally. Closing the generator never joins a thread, whichever thread or code path closes it. Pending reads are cancelled and in-flight ones finish on their own. Callers consume every future as it is yielded, so none of them relied on the join.

flowchart TD
    subgraph Before
        subgraph BS[IO: read_mcap planning, read_summaries]
            B1[walk returns early after a skipped file] -->|error traceback keeps the generator alive| B2[cyclic GC closes the generator]
            B2 -->|with-block exit| B3["shutdown(wait=True): joins workers"]
        end
    end
    subgraph After
        subgraph AS[IO: read_mcap planning, read_summaries]
            A1[walk returns early after a skipped file] -->|error traceback keeps the generator alive| A2[cyclic GC closes the generator]
            A2 -->|finally| A3["shutdown(wait=False, cancel_futures=True): no join"]
        end
    end
    classDef removed fill:#ffcdd2,stroke:#c62828,color:#000
    classDef added fill:#c8e6c9,stroke:#2e7d32,color:#000
    class B3 removed
    class A3 added
Loading

The real read_summaries, _SectionWindow, _summaries and _FileInspector.walk from each revision, with file I/O stubbed, running 3,000 rounds of "walk past a non-MCAP file, then start new threads" under gc.set_threshold(1, 1, 1):

Revision Pool shut down when walk() returns 3,000 rounds
before #67930 yes completed, 2 of 2 runs
#67930 / master no, later by the GC deadlocked, 2 of 2 runs each
this PR no, later by the GC (without joining) completed, 2 of 2 runs

Subsystems

IO (_internal/datasource_v2/formats/mcap/mcap_summary.py)

Testing

  • ruff check, black --check, pydoclint and git diff --check on the change: clean. Pyrefly in CI's environment: 0 errors.
  • The harness above, on Python 3.12.13.
  • test_mcap_data_column.py was not run locally (no mcap in my environment); CI runs it.

Not a duplicate: no open PR touches read_summaries or this test. #68109 fixed a different failure in this file (struct field order under pyarrow 25).

AI assistance was used; I reviewed every changed line.

Signed-off-by: Aarrya [email protected]

🤖 Generated with Claude Code

@AarryaSaraf
AarryaSaraf marked this pull request as ready for review November 21, 2026 00:05
@AarryaSaraf
AarryaSaraf requested review from a team as code owners November 21, 2026 00:05

@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 refactors the read_summaries function in mcap_summary.py by replacing the with ThreadPoolExecutor context manager with an explicit try...finally block that shuts down the executor with wait=False and cancel_futures=True. This prevents potential deadlocks during garbage collection when the generator is closed while a thread holds a lock needed for joining. I have no feedback to provide as there are no review comments.

@AarryaSaraf AarryaSaraf added data Ray Data-related issues go add ONLY when ready to merge, run all tests labels Nov 21, 2026
@goutamvenkat-anyscale
goutamvenkat-anyscale merged commit 63c2cc2 into ray-project:master Nov 23, 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.

2 participants