Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Ensure that forked process do not see invalid blocking operations. #13343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 15, 2025

Conversation

ioquatix
Copy link
Member

@ioquatix ioquatix commented May 15, 2025

When forking a child process, while simultaneously there is a thread which is blocking on an IO operation, if that IO instance is closed in the child process, it will try and interrupt the blocking operation which no longer exists (in the child process).

r, w = IO.pipe

thread = Thread.new do
  r.read
end

Thread.pass until thread.status == "sleep"

fork do
  r.close
end

w.close

Process.wait
thread.join

For a little bit of background: when forking, all native threads except for the one doing the fork operation go away. That means stack allocated data structures on those threads become invalid, e.g. struct rb_io_blocking_operation. We track the fork generation to know whether the entries in the blocking_operations list are valid or not, and if the fork generation mismatches, we clear the list. This is the same technique used by Queue.

Copy link

launchable-app bot commented May 15, 2025

Tests Failed

✖️no tests failed ✔️61828 tests passed(1 flake)

@ioquatix ioquatix force-pushed the ruby-thread-io-close-fix branch from 34ae8a3 to e588c57 Compare May 15, 2025 05:51
@ioquatix ioquatix force-pushed the ruby-thread-io-close-fix branch from e588c57 to e6910ac Compare May 15, 2025 05:58
@ioquatix ioquatix merged commit 87261c2 into ruby:master May 15, 2025
81 of 82 checks passed
@ioquatix ioquatix deleted the ruby-thread-io-close-fix branch May 15, 2025 10:52
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