Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion ext/wreq_rb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
67 changes: 58 additions & 9 deletions ext/wreq_rb/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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<Runtime> = 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<ProcessRuntime> = 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;
}
}
}
}

// --------------------------------------------------------------------------
Expand Down Expand Up @@ -243,6 +274,10 @@ fn build_emulation_option(
struct Client {
inner: wreq::Client,
cancel_token: std::sync::Mutex<CancellationToken>,
// 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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<Response, magnus::Error> {
self.claim_for_current_process()?;
let url: String = if args.is_empty() {
return Err(generic_error("url is required"));
} else {
Expand Down Expand Up @@ -505,6 +553,7 @@ impl Client {

/// Wreq::Client#request_batch(specs) or #request_batch(specs, options)
fn request_batch(&self, args: &[Value]) -> Result<RArray, magnus::Error> {
self.claim_for_current_process()?;
if args.is_empty() {
return Err(generic_error("an array of requests is required"));
}
Expand Down
104 changes: 104 additions & 0 deletions test/fork_test.rb
Original file line number Diff line number Diff line change
@@ -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
Loading