Skip to content

Remove fiber scheduler and set fiber to blocking in forked process#15385

Merged
ioquatix merged 4 commits intoruby:masterfrom
noteflakes:post_fork_scheduler_cleanup
Dec 5, 2025
Merged

Remove fiber scheduler and set fiber to blocking in forked process#15385
ioquatix merged 4 commits intoruby:masterfrom
noteflakes:post_fork_scheduler_cleanup

Conversation

@noteflakes
Copy link
Contributor

@noteflakes noteflakes commented Dec 3, 2025

https://bugs.ruby-lang.org/issues/21717

This PR is an alternative to #15342. In a forked process:

  • Remove the fiber scheduler
  • Set the main fiber to blocking, to prevent having the main fiber in non-blocking mode, which may cause unexpected results if a fiber scheduler is set.
  • Set th->blocking to 1 to reflect single blocking fiber.

I think this behavior is better as it removes the problem of scheduler state leaking into the forked process. It also prevents unexpected behavior in the forked process if forked from a non-blocking fiber.

See also #15354.

cc @ioquatix

@launchable-app

This comment has been minimized.

@noteflakes noteflakes marked this pull request as ready for review December 3, 2025 10:42
@ioquatix ioquatix force-pushed the post_fork_scheduler_cleanup branch from 7b4457c to 67d3e2f Compare December 3, 2025 22:00
Copy link
Member

@ioquatix ioquatix left a comment

Choose a reason for hiding this comment

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

LGTM.

@ioquatix ioquatix force-pushed the post_fork_scheduler_cleanup branch from 67d3e2f to 3cf8cd9 Compare December 4, 2025 12:47
@ioquatix ioquatix force-pushed the post_fork_scheduler_cleanup branch from 4e3194f to bf96764 Compare December 5, 2025 01:54
@ioquatix ioquatix force-pushed the post_fork_scheduler_cleanup branch from bf96764 to 4fc796f Compare December 5, 2025 02:10
@ioquatix ioquatix merged commit 5f6b31c into ruby:master Dec 5, 2025
107 of 117 checks passed
@ioquatix
Copy link
Member

ioquatix commented Dec 5, 2025

Thanks, this looks good to me.

In addition, do we need to do something for Process.daemon?

@noteflakes noteflakes deleted the post_fork_scheduler_cleanup branch December 7, 2025 12:43
@noteflakes
Copy link
Contributor Author

In addition, do we need to do something for Process.daemon?

Seems to behave correctly:

$ ruby -e "class C; def respond_to?(sym)=true; end; Fiber.set_scheduler(C.new); f = Fiber.new(blocking: false) { Process.daemon(true, true); p(scheduler: Fiber.scheduler, blocking: f.blocking?) }; f.resume"
$ {scheduler: nil, blocking: true}

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