File: fork_handler.rb

package info (click to toggle)
ruby-async 2.36.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 400 kB
  • sloc: ruby: 1,938; makefile: 4
file content (32 lines) | stat: -rw-r--r-- 766 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2025, by Shopify Inc.
# Copyright, 2025, by Samuel Williams.

module Async
	# Private module that hooks into Process._fork to handle fork events.
	#
	# If `Scheduler#process_fork` hook is adopted in Ruby 4, this code can be removed after Ruby < 4 is no longer supported.
	module ForkHandler
		def _fork(&block)
			result = super
			
			if result.zero?
				# Child process:
				if Fiber.scheduler.respond_to?(:process_fork)
					Fiber.scheduler.process_fork
				end
			end
			
			return result
		end
	end
	
	private_constant :ForkHandler
	
	# Hook into Process._fork to handle fork events automatically:
	unless RUBY_VERSION > "4"
		::Process.singleton_class.prepend(ForkHandler)
	end
end