Skip to content

Fix flaky DirSingletonTest GC failures in test_fchdir and test_for_fd#3004

Merged
soutaro merged 2 commits into
ruby:masterfrom
hsbt:fix-dir-fchdir-flaky
Jun 18, 2026
Merged

Fix flaky DirSingletonTest GC failures in test_fchdir and test_for_fd#3004
soutaro merged 2 commits into
ruby:masterfrom
hsbt:fix-dir-fchdir-flaky

Conversation

@hsbt

@hsbt hsbt commented Jun 18, 2026

Copy link
Copy Markdown
Member

The Dir created by Dir.new(Dir.pwd).fileno in test_fchdir and test_for_fd was only referenced long enough to read its fileno, so it became collectable right away. When GC ran before the descriptor was used it got closed, making the later Dir.fchdir(fd) and Dir.for_fd(fd) calls fail with Errno::EBADF.

This surfaced on ruby/ruby ZJIT CI running make test-bundled-gems with --enable-zjit=dev --zjit-call-threshold=1, where allocation and GC timing shifted enough to expose it. Keep the Dir in a local and close it in ensure so the descriptor stays valid for the whole test.

https://github.com/ruby/ruby/actions/runs/27735384476/job/82051097840

hsbt and others added 2 commits June 18, 2026 13:13
The Dir created by `Dir.new(Dir.pwd).fileno` was only referenced long
enough to read its fileno, so it became collectable immediately. When
GC ran between the assert_send_type calls the fd was closed, making the
later `Dir.fchdir(fd)` fail with Errno::EBADF. Keep the Dir in a local
and close it in ensure. Surfaced on ruby/ruby ZJIT CI with
--enable-zjit=dev --zjit-call-threshold=1.

https://github.com/ruby/ruby/actions/runs/27735384476/job/82051097840

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
test_for_fd has the same pattern as test_fchdir: the Dir from
`Dir.new(Dir.pwd).fileno` is collectable before `Dir.for_fd(fd)` runs,
so an ill-timed GC closes the fd and the call fails with Errno::EBADF.
Keep the Dir alive in a local and close it in ensure.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

@soutaro soutaro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🙏

@soutaro soutaro added this to the RBS 4.1 milestone Jun 18, 2026
@soutaro soutaro merged commit a8f8f27 into ruby:master Jun 18, 2026
24 of 27 checks passed
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.

2 participants