diff --git a/Cargo.lock b/Cargo.lock index f8e704c..2e761a1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2827,16 +2827,6 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" -[[package]] -name = "signal-hook-registry" -version = "1.4.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b" -dependencies = [ - "errno", - "libc", -] - [[package]] name = "simd-adler32" version = "0.3.9" @@ -3154,9 +3144,7 @@ dependencies = [ "bytes", "libc", "mio", - "parking_lot", "pin-project-lite", - "signal-hook-registry", "socket2", "tokio-macros", "windows-sys 0.61.2", diff --git a/README.md b/README.md index be0d1ce..1b109cf 100644 --- a/README.md +++ b/README.md @@ -231,6 +231,12 @@ The `Wreq::Response` object provides: | `client_error?` | Boolean | Status 4xx? | | `server_error?` | Boolean | Status 5xx? | +## Forking (Puma `preload_app!`, `fork_worker`, Unicorn) + +The gem is safe to load before `fork`. Each process gets its own Tokio runtime, built on the first request in that process, so a forked worker can make requests even after the parent already has. + +A `Wreq::Client` keeps a connection pool that belongs to the process that used it first. Using that client in a forked child raises `Wreq::Error`; create clients in the worker (for example in `on_worker_boot`) or lazily per process. A client created in the parent but never used works in any one child. + ## Building from Source ```bash diff --git a/ext/wreq_rb/Cargo.toml b/ext/wreq_rb/Cargo.toml index 9ddced0..4e68ec3 100644 --- a/ext/wreq_rb/Cargo.toml +++ b/ext/wreq_rb/Cargo.toml @@ -25,7 +25,9 @@ wreq = { path = "../../vendor/wreq", version = "=0.16.1", features = [ "form", ] } wreq-util = { version = "=0.2.0", features = ["emulation", "emulation-serde", "emulation-compression"] } -tokio = { version = "1", features = ["full"] } +# Not "full": its parking_lot feature keeps a process-global lock table that a fork can copy mid-update, +# deadlocking the child on its first request. +tokio = { version = "1", features = ["rt-multi-thread", "macros", "sync", "time", "net"] } tokio-util = "0.7" serde_json = "1.0" bytes = "1" diff --git a/ext/wreq_rb/src/client.rs b/ext/wreq_rb/src/client.rs index 9e76dda..e777beb 100644 --- a/ext/wreq_rb/src/client.rs +++ b/ext/wreq_rb/src/client.rs @@ -3,6 +3,7 @@ use std::panic::{self, AssertUnwindSafe}; use std::ptr; use std::any::Any; use std::sync::Arc; +use std::sync::atomic::{AtomicU32, Ordering}; use std::time::Duration; use magnus::{ @@ -25,15 +26,45 @@ use crate::response::Response; // Shared Tokio runtime // -------------------------------------------------------------------------- +// fork() copies the runtime but not its worker threads, so a child that reused it +// would block forever or panic on the parent's I/O driver. Each process builds its +// own; the inherited one is leaked because dropping it would join threads that +// don't exist in the child. No lock guards this: a lock held by another thread at +// fork time would stay locked in the child forever. +struct ProcessRuntime { + pid: u32, + runtime: Runtime, +} + fn runtime() -> &'static Runtime { - use std::sync::OnceLock; - static RT: OnceLock = OnceLock::new(); - RT.get_or_init(|| { - tokio::runtime::Builder::new_multi_thread() - .enable_all() - .build() - .expect("failed to build tokio runtime") - }) + use std::sync::atomic::AtomicPtr; + static RT: AtomicPtr = AtomicPtr::new(ptr::null_mut()); + + let pid = std::process::id(); + let mut current = RT.load(Ordering::Acquire); + loop { + // SAFETY: published pointers are never freed (see above). + if let Some(installed) = unsafe { current.as_ref() } { + if installed.pid == pid { + return &installed.runtime; + } + } + let built = Box::into_raw(Box::new(ProcessRuntime { + pid, + runtime: tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + .expect("failed to build tokio runtime"), + })); + match RT.compare_exchange(current, built, Ordering::AcqRel, Ordering::Acquire) { + Ok(_) => return unsafe { &(*built).runtime }, + Err(winner) => { + // Another thread of this process installed one first; ours never ran a task. + drop(unsafe { Box::from_raw(built) }); + current = winner; + } + } + } } // -------------------------------------------------------------------------- @@ -243,6 +274,10 @@ fn build_emulation_option( struct Client { inner: wreq::Client, cancel_token: std::sync::Mutex, + // Process that sent the first request (0 = none yet). Pooled connections + // belong to that process's runtime and sockets, so a forked child must not + // reuse them. + owner_pid: AtomicU32, } impl Client { @@ -409,7 +444,7 @@ impl Client { } let client = builder.build().map_err(to_magnus_error)?; - Ok(Client { inner: client, cancel_token: std::sync::Mutex::new(CancellationToken::new()) }) + Ok(Client { inner: client, cancel_token: std::sync::Mutex::new(CancellationToken::new()), owner_pid: AtomicU32::new(0) }) } /// client.get(url) or client.get(url, opts) @@ -453,7 +488,20 @@ impl Client { old_token.cancel(); } + fn claim_for_current_process(&self) -> Result<(), magnus::Error> { + let pid = std::process::id(); + match self.owner_pid.compare_exchange(0, pid, Ordering::AcqRel, Ordering::Acquire) { + Ok(_) => Ok(()), + Err(owner) if owner == pid => Ok(()), + Err(owner) => Err(generic_error(format!( + "Wreq::Client was used in process {} and cannot be reused after fork in process {}; create a new client in this process", + owner, pid + ))), + } + } + fn execute_method(&self, method_str: &str, args: &[Value]) -> Result { + self.claim_for_current_process()?; let url: String = if args.is_empty() { return Err(generic_error("url is required")); } else { @@ -505,6 +553,7 @@ impl Client { /// Wreq::Client#request_batch(specs) or #request_batch(specs, options) fn request_batch(&self, args: &[Value]) -> Result { + self.claim_for_current_process()?; if args.is_empty() { return Err(generic_error("an array of requests is required")); } diff --git a/test/fork_test.rb b/test/fork_test.rb new file mode 100644 index 0000000..c6e26ff --- /dev/null +++ b/test/fork_test.rb @@ -0,0 +1,104 @@ +# frozen_string_literal: true + +require_relative "test_helper" +require "socket" + +class ForkTest < Minitest::Test + def setup + skip "fork is not available" unless Process.respond_to?(:fork) + @server = TCPServer.new("127.0.0.1", 0) + @url = "http://127.0.0.1:#{@server.addr[1]}/" + @server_thread = Thread.new { loop { serve(@server.accept) } } + end + + def teardown + @server_thread&.kill + @server&.close + end + + def test_new_client_works_in_child_after_parent_used_the_runtime + assert_equal 200, new_client.get(@url).status + + assert_equal "200", in_child { new_client.get(@url).status.to_s } + end + + def test_unused_client_from_parent_works_in_child + client = new_client + + assert_equal "200", in_child { client.get(@url).status.to_s } + end + + def test_used_client_from_parent_raises_in_child + client = new_client + assert_equal 200, client.get(@url).status + + message = in_child do + client.get(@url) + "no error" + rescue Wreq::Error => e + e.message + end + + assert_match(/cannot be reused after fork/, message) + assert_equal 200, client.get(@url).status + end + + def test_used_client_from_parent_raises_on_batch_in_child + client = new_client + assert_equal 200, client.get(@url).status + + message = in_child do + client.request_batch([@url]) + "no error" + rescue Wreq::Error => e + e.message + end + + assert_match(/cannot be reused after fork/, message) + end + + private + + def new_client + Wreq::Client.new(emulation: false, http1_only: true, no_proxy: true, timeout: 5) + end + + # Keep-alive server so the parent's pool holds an idle connection across fork. + def serve(connection) + Thread.new do + while connection.gets("\r\n\r\n") + connection.write("HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok") + end + rescue IOError, SystemCallError + nil + ensure + connection.close + end + end + + def in_child + reader, writer = IO.pipe + pid = fork do + reader.close + result = begin + yield + rescue Exception => e # rubocop:disable Lint/RescueException + "#{e.class}: #{e.message}" + end + writer.write(result) + writer.close + exit!(0) + end + writer.close + deadline = Time.now + 15 + sleep 0.05 until Process.waitpid(pid, Process::WNOHANG) || Time.now > deadline + if Time.now > deadline + Process.kill(:KILL, pid) + Process.wait(pid) + flunk "child process hung" + end + reader.read + ensure + reader&.close + end +end