kino 0.1.3 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -21,6 +21,8 @@ and a threaded fallback mode runs everything else, Rails included.
21
21
  small process.
22
22
  * **Production plumbing included.** Graceful drain, crash supervision
23
23
  and respawn, bounded queues with 503 backpressure, request timeouts,
24
+ hardened intake (slowloris and TLS-handshake deadlines, connection
25
+ and body-size caps), an `on_error` hook for your error tracker,
24
26
  TLS (rustls), live stats, async access and app logging.
25
27
  * **Tells you why.** `kino --check` lists exactly what blocks your app
26
28
  from ractor mode, finding by finding, so you do not have to decode
@@ -181,7 +183,7 @@ bundle add kino # or: gem install kino (outside a bundle)
181
183
  or put it in the `Gemfile` yourself:
182
184
 
183
185
  ```ruby
184
- gem "kino", "~> 0.1"
186
+ gem "kino"
185
187
  ```
186
188
 
187
189
  Then generate a config and serve:
@@ -224,6 +226,9 @@ server = Kino::Server.new(app,
224
226
  queue_depth: 1024, # bounded queue; overflow → 503
225
227
  queue_timeout: 5.0, # seconds before 503 on a full queue
226
228
  request_timeout: nil, # seconds before a slow response becomes a 504 (nil = off)
229
+ max_connections: 8192, # cap concurrent connections; default: most of ulimit -n
230
+ max_body_size: 50 * 1024 * 1024, # bytes before a 413; nil = let a proxy handle it
231
+ on_error: ->(e, env) { ErrorTracker.capture(e) }, # after the client got its 500
227
232
  shutdown_timeout: 30, # drain deadline
228
233
  tls: { cert: "cert.pem", key: "key.pem" }, # file paths or inline PEM
229
234
  )
@@ -303,6 +308,18 @@ is unsafe. A stuck handler still occupies its worker slot until it
303
308
  returns, so set the deadline above your slowest legitimate endpoint and
304
309
  watch `stats[:timeouts]`.
305
310
 
311
+ Timeouts guard your app; the network intake guards itself. New
312
+ connections past `max_connections` (default: most of `ulimit -n`) wait
313
+ in the kernel backlog; request bodies past `max_body_size` (default
314
+ 50 MB, `nil` delegates to a fronting proxy) get a **413**; and fixed
315
+ deadlines drop slow-header clients (15 s), stalled TLS handshakes
316
+ (10 s), and uploads stalled mid-body (30 s). When a worker catches an
317
+ app or delivery error,
318
+ `on_error ->(error, env) { ErrorTracker.capture(error) }` is called
319
+ after the client got its 500—the only place a tracker sees errors
320
+ raised while the response was being written (in `:ractor` mode, build
321
+ the handler with `Ractor.shareable_proc`).
322
+
306
323
  ## Stats
307
324
 
308
325
  `server.stats` returns a live snapshot: the configuration plus counters
@@ -415,6 +432,14 @@ bundle exec rake # compile, Rust tests, specs, RBS, lint
415
432
  RB_SYS_CARGO_PROFILE=dev bundle exec rake compile # fast dev rebuilds
416
433
  ```
417
434
 
435
+ ## Acknowledgements
436
+
437
+ Thanks to [Mat Sadler](https://github.com/matsadler) for [magnus](https://github.com/matsadler/magnus).
438
+
439
+ For ractors, thanks to [Koichi Sasada](https://github.com/ko1), [John Hawthorn](https://github.com/jhawthorn), [Jean Boussier](https://github.com/byroot), [Luke Gruber](https://github.com/luke-gruber), and other Ruby core contributors.
440
+
441
+ For the Rust network stack, thanks to [Sean McArthur](https://github.com/seanmonstar) for [hyper](https://github.com/hyperium/hyper), and to [Carl Lerche](https://github.com/carllerche), [Alice Ryhl](https://github.com/Darksonn), and the other [Tokio](https://github.com/tokio-rs/tokio) maintainers for the runtime underneath it. Thanks to [Joshua Barretto](https://github.com/zesterer) for [flume](https://github.com/zesterer/flume)—its channels carry every request between the network side and the workers.
442
+
418
443
  ## Assisted by
419
444
 
420
445
  Claude Code (Mythos, Opus).
data/ext/kino/Cargo.toml CHANGED
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "kino"
3
- version = "0.1.3"
3
+ version = "0.2.1"
4
4
  edition = "2021"
5
5
  authors = ["Yaroslav Markin <yaroslav@markin.net>"]
6
6
  license = "MIT"
@@ -14,11 +14,11 @@ crate-type = ["cdylib", "rlib"]
14
14
  [dependencies]
15
15
  magnus = { version = "0.8.2", features = ["rb-sys"] }
16
16
  rb-sys = { version = "0.9", features = ["stable-api-compiled-fallback"] }
17
- flume = "0.11"
17
+ flume = "0.12"
18
18
  parking_lot = "0.12"
19
19
  ahash = "0.8"
20
20
  smallvec = "1"
21
- lru = "0.12"
21
+ lru = "0.18"
22
22
  mimalloc = { version = "0.1", default-features = false }
23
23
  tokio = { version = "1.45", features = ["rt-multi-thread", "net", "time", "sync", "io-util", "macros"] }
24
24
  hyper = { version = "1.6", features = ["http1", "server"] }
data/ext/kino/src/gvl.rs CHANGED
@@ -5,7 +5,11 @@
5
5
  //! body reads/writes) must go through `without_gvl` so other Ruby threads
6
6
  //! in the same ractor keep running and the VM can interrupt us via the UBF.
7
7
 
8
+ use std::any::Any;
8
9
  use std::ffi::c_void;
10
+ use std::panic::{catch_unwind, AssertUnwindSafe};
11
+
12
+ use magnus::{Error, Ruby};
9
13
 
10
14
  /// An unblock function: called by the Ruby VM from another thread when it
11
15
  /// needs to interrupt the blocking region (Thread#kill, VM shutdown, ...).
@@ -20,23 +24,48 @@ unsafe extern "C" fn trampoline<F, R>(arg: *mut c_void) -> *mut c_void
20
24
  where
21
25
  F: FnOnce() -> R,
22
26
  {
23
- let slot = unsafe { &mut *(arg as *mut (Option<F>, Option<R>)) };
27
+ let slot = unsafe { &mut *(arg as *mut (Option<F>, Option<std::thread::Result<R>>)) };
24
28
  let f = slot.0.take().expect("without_gvl trampoline called twice");
25
- slot.1 = Some(f());
29
+ // A panic must not unwind into the VM's C frame beneath us (that
30
+ // aborts the process); catch it here and let without_gvl rethrow it
31
+ // as a Ruby exception once the GVL is held again.
32
+ slot.1 = Some(catch_unwind(AssertUnwindSafe(f)));
26
33
  std::ptr::null_mut()
27
34
  }
28
35
 
36
+ /// The caught panic payload as a plain RuntimeError, built with the GVL
37
+ /// held. Deliberately not magnus's own panic conversion: that raises
38
+ /// `fatal`, which still ends the process; a RuntimeError flows through
39
+ /// the worker's normal rescue path (500, on_error, error log, respawn).
40
+ fn panic_error(payload: Box<dyn Any + Send>) -> Error {
41
+ let msg = if let Some(m) = payload.downcast_ref::<&'static str>() {
42
+ (*m).to_string()
43
+ } else if let Some(m) = payload.downcast_ref::<String>() {
44
+ m.clone()
45
+ } else {
46
+ "opaque panic payload".to_string()
47
+ };
48
+ let ruby = Ruby::get().expect("without_gvl requires a Ruby thread");
49
+ Error::new(
50
+ ruby.exception_runtime_error(),
51
+ format!("Kino: panic in native blocking call: {msg}"),
52
+ )
53
+ }
54
+
29
55
  /// Run `f` with the GVL released. Blocks the current Ruby thread but lets
30
56
  /// every other Ruby thread (in this ractor) run in parallel.
31
57
  ///
32
58
  /// `f` MUST NOT touch any Ruby API. If `ubf` fires, `f` is responsible for
33
59
  /// noticing (e.g. a message on an interrupt channel) and returning promptly;
34
60
  /// pending VM interrupts are then delivered once we're back in Ruby.
35
- pub fn without_gvl<F, R>(f: F, ubf: Option<Ubf>) -> R
61
+ ///
62
+ /// A panic in `f` comes back as `Err` (RuntimeError with the panic
63
+ /// message); the default panic hook still reports it to stderr first.
64
+ pub fn without_gvl<F, R>(f: F, ubf: Option<Ubf>) -> Result<R, Error>
36
65
  where
37
66
  F: FnOnce() -> R,
38
67
  {
39
- let mut slot: (Option<F>, Option<R>) = (Some(f), None);
68
+ let mut slot: (Option<F>, Option<std::thread::Result<R>>) = (Some(f), None);
40
69
  let (ubf_func, ubf_data) = match ubf {
41
70
  Some(u) => (Some(u.func), u.data),
42
71
  None => (None, std::ptr::null_mut()),
@@ -49,7 +78,10 @@ where
49
78
  ubf_data,
50
79
  );
51
80
  }
52
- slot.1.expect("without_gvl block did not run")
81
+ match slot.1.expect("without_gvl block did not run") {
82
+ Ok(value) => Ok(value),
83
+ Err(payload) => Err(panic_error(payload)),
84
+ }
53
85
  }
54
86
 
55
87
  /// The standard UBF used across this crate: `data` points at an `AtomicBool`
@@ -64,12 +96,13 @@ pub unsafe extern "C" fn ubf_interrupt(data: *mut c_void) {
64
96
  /// The crate's one blocking idiom: release the GVL and run `attempt` in a
65
97
  /// loop (`Some(T)` finishes, `None` means "tick elapsed, go around"), and
66
98
  /// wake within a tick when `flag` is raised (by the VM's UBF or by
67
- /// interrupt_all_workers). Returns None on interruption. `attempt` must
68
- /// bound each wait (recv_timeout-style) and must not touch the Ruby API.
99
+ /// interrupt_all_workers). Returns Ok(None) on interruption, Err when
100
+ /// `attempt` panicked. `attempt` must bound each wait (recv_timeout-style)
101
+ /// and must not touch the Ruby API.
69
102
  pub fn interruptible<T>(
70
103
  flag: &std::sync::atomic::AtomicBool,
71
104
  mut attempt: impl FnMut() -> Option<T>,
72
- ) -> Option<T> {
105
+ ) -> Result<Option<T>, Error> {
73
106
  use std::sync::atomic::Ordering;
74
107
 
75
108
  without_gvl(
data/ext/kino/src/lib.rs CHANGED
@@ -7,6 +7,7 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
7
7
  mod env_strings;
8
8
  mod gvl;
9
9
  mod logsink;
10
+ mod pin;
10
11
  mod queue;
11
12
  mod registry;
12
13
  mod request;
@@ -59,6 +60,9 @@ fn init(ruby: &Ruby) -> Result<(), Error> {
59
60
  function!(env_strings::register_defaults, 2),
60
61
  )?;
61
62
 
63
+ native.define_class("PinKeeper", ruby.class_object())?;
64
+ native.define_singleton_method("pin_keeper", function!(server::pin_keeper, 1))?;
65
+
62
66
  let request = native.define_class("Request", ruby.class_object())?;
63
67
  request.define_method("respond_and_take", method!(queue::respond_and_take, 6))?;
64
68
  request.define_method(
@@ -85,6 +89,7 @@ fn init(ruby: &Ruby) -> Result<(), Error> {
85
89
  native.define_singleton_method("_test_push", function!(test_support::push, 2))?;
86
90
  native.define_singleton_method("_test_take", function!(test_support::take, 1))?;
87
91
  native.define_singleton_method("_test_close", function!(test_support::close, 1))?;
92
+ native.define_singleton_method("_test_panic", function!(test_support::panic_in_release, 0))?;
88
93
 
89
94
  Ok(())
90
95
  }
@@ -0,0 +1,223 @@
1
+ //! Zero-copy response bodies: a large body's bytes ride to hyper in
2
+ //! place instead of being copied at the FFI boundary, with the backing
3
+ //! Ruby string kept alive until hyper drops the buffer.
4
+ //!
5
+ //! Buffer stability is the same mechanism io.c uses to hold a string
6
+ //! across a GVL-released write: `rb_str_tmp_frozen_acquire` returns a
7
+ //! frozen string sharing the original's byte buffer (a frozen original
8
+ //! is returned as-is). Mutating the original afterwards copy-on-writes
9
+ //! the ORIGINAL side, so the shared buffer stays byte-stable for as
10
+ //! long as the acquired string lives.
11
+ //!
12
+ //! Liveness is a fixed slab of atomic VALUE slots per server, marked
13
+ //! from a `PinKeeper` TypedData object that the Ruby `Kino::Server`
14
+ //! holds for the server's lifetime (pin.rs never registers global GC
15
+ //! roots: `rb_gc_register_address` is not ractor-safe in Ruby 4.0).
16
+ //! The mark hook uses the pinning `rb_gc_mark`, so compaction never
17
+ //! moves a pinned string either. Because the keeper lives on the main
18
+ //! ractor, pinned buffers survive worker-ractor crashes while hyper is
19
+ //! still flushing them.
20
+ //!
21
+ //! Concurrency: inserts happen on worker threads (any ractor, its own
22
+ //! GVL held), release is a single atomic store from whatever tokio
23
+ //! thread drops the buffer, and the mark hook only loads atomics, so
24
+ //! no path takes a lock. A full slab degrades to the copy path.
25
+
26
+ use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
27
+ use std::sync::Arc;
28
+
29
+ use bytes::Bytes;
30
+ use magnus::rb_sys::AsRawValue;
31
+ use magnus::RString;
32
+
33
+ /// Floor for the zero-copy path. Below it the pin machinery costs more
34
+ /// than the memcpy it saves. It is also a SOUNDNESS bound: it must
35
+ /// exceed the largest embeddable string (a 640-byte GC slot in Ruby
36
+ /// 4.0), so the acquired string's bytes are guaranteed to live in a
37
+ /// stable malloc'd buffer rather than inside a movable RVALUE, and the
38
+ /// string can never be re-embedded by compaction.
39
+ pub const ZERO_COPY_MIN: usize = 4096;
40
+
41
+ /// In-flight pins per server. Bounds slab memory (8 bytes per slot) and
42
+ /// GC mark work; overflow falls back to copying, so the cap is a
43
+ /// throughput heuristic, not a limit on concurrency.
44
+ const SLAB_CAPACITY: usize = 4096;
45
+
46
+ extern "C" {
47
+ // Exported by libruby but absent from the public headers: io.c's
48
+ // buffer-stabilizing primitive (see module docs). Signature per
49
+ // string.c: VALUE rb_str_tmp_frozen_acquire(VALUE orig).
50
+ fn rb_str_tmp_frozen_acquire(orig: rb_sys::VALUE) -> rb_sys::VALUE;
51
+ }
52
+
53
+ /// GC roots for in-flight response buffers. Slot value 0 = empty (a
54
+ /// VALUE of 0 is Qfalse, which can never be a pinned string).
55
+ pub struct PinSlab {
56
+ slots: Box<[AtomicU64]>,
57
+ /// Rotating claim cursor: keeps the free-slot scan O(1) amortized.
58
+ cursor: AtomicUsize,
59
+ }
60
+
61
+ impl PinSlab {
62
+ pub fn new() -> Self {
63
+ PinSlab {
64
+ slots: (0..SLAB_CAPACITY).map(|_| AtomicU64::new(0)).collect(),
65
+ cursor: AtomicUsize::new(0),
66
+ }
67
+ }
68
+
69
+ /// Root `value`; None when the slab is full (caller copies instead).
70
+ fn insert(&self, value: rb_sys::VALUE) -> Option<usize> {
71
+ let start = self.cursor.fetch_add(1, Ordering::Relaxed);
72
+ for offset in 0..self.slots.len() {
73
+ let index = (start + offset) % self.slots.len();
74
+ if self.slots[index]
75
+ .compare_exchange(0, value, Ordering::SeqCst, Ordering::Relaxed)
76
+ .is_ok()
77
+ {
78
+ return Some(index);
79
+ }
80
+ }
81
+ None
82
+ }
83
+
84
+ /// Drop the root. Called from tokio threads: a plain atomic store,
85
+ /// no Ruby API. The string stays alive until the next GC sweep.
86
+ fn release(&self, index: usize) {
87
+ self.slots[index].store(0, Ordering::SeqCst);
88
+ }
89
+
90
+ /// GC mark hook (PinKeeper). Runs during stop-the-world, so it must
91
+ /// not allocate, lock, or panic: it only loads atomics. A slot
92
+ /// concurrently released by a tokio thread is seen as either the
93
+ /// live value (marked one cycle longer, harmless) or 0 (skipped).
94
+ pub fn mark_all(&self) {
95
+ for slot in self.slots.iter() {
96
+ let value = slot.load(Ordering::SeqCst);
97
+ if value != 0 {
98
+ // SAFETY: non-zero slots hold VALUEs inserted by
99
+ // pinned_bytes and not yet released, i.e. live objects;
100
+ // rb_gc_mark is the pinning mark, callable from a mark
101
+ // hook by design.
102
+ unsafe { rb_sys::rb_gc_mark(value) };
103
+ }
104
+ }
105
+ }
106
+
107
+ #[cfg(test)]
108
+ fn occupied(&self) -> usize {
109
+ self.slots
110
+ .iter()
111
+ .filter(|s| s.load(Ordering::SeqCst) != 0)
112
+ .count()
113
+ }
114
+ }
115
+
116
+ /// The GC-visible face of a server's PinSlab: `Kino::Server` holds one
117
+ /// for its lifetime (surviving worker-ractor crashes), and Ruby's GC
118
+ /// marks every pinned string through it.
119
+ #[derive(magnus::TypedData)]
120
+ #[magnus(class = "Kino::Native::PinKeeper", free_immediately, mark)]
121
+ pub struct PinKeeper(pub Arc<PinSlab>);
122
+
123
+ impl magnus::DataTypeFunctions for PinKeeper {
124
+ fn mark(&self, _marker: &magnus::gc::Marker) {
125
+ self.0.mark_all();
126
+ }
127
+ }
128
+
129
+ /// Owns one in-flight buffer: the raw view plus the slab slot rooting
130
+ /// the acquired string.
131
+ struct PinnedBuf {
132
+ slab: Arc<PinSlab>,
133
+ index: usize,
134
+ ptr: *const u8,
135
+ len: usize,
136
+ }
137
+
138
+ // SAFETY: the only operations performed off the Ruby thread are reading
139
+ // the byte buffer (the acquired string is frozen and slab-rooted: no
140
+ // writer exists and the buffer neither moves nor frees while the slot
141
+ // holds it) and Drop's atomic store. No Ruby API is called off-thread.
142
+ unsafe impl Send for PinnedBuf {}
143
+
144
+ impl AsRef<[u8]> for PinnedBuf {
145
+ fn as_ref(&self) -> &[u8] {
146
+ // SAFETY: ptr/len were captured under the GVL from the acquired
147
+ // frozen string, which stays alive and byte-stable until this
148
+ // owner releases its slot (see module docs).
149
+ unsafe { std::slice::from_raw_parts(self.ptr, self.len) }
150
+ }
151
+ }
152
+
153
+ impl Drop for PinnedBuf {
154
+ fn drop(&mut self) {
155
+ self.slab.release(self.index);
156
+ }
157
+ }
158
+
159
+ /// Bytes borrowing `body`'s buffer, with the string rooted until hyper
160
+ /// drops it; None when the body is below ZERO_COPY_MIN or the slab is
161
+ /// full (caller copies). Requires the calling worker's GVL; safe from
162
+ /// any ractor.
163
+ pub fn pinned_bytes(slab: &Arc<PinSlab>, body: RString) -> Option<Bytes> {
164
+ if body.len() < ZERO_COPY_MIN {
165
+ return None;
166
+ }
167
+ // SAFETY: this worker's GVL is held; body is a live RString rooted
168
+ // by the caller. The acquired tmp is rooted by this thread's machine
169
+ // stack (conservatively scanned) until the slab insert publishes it.
170
+ let tmp = unsafe { rb_str_tmp_frozen_acquire(body.as_raw()) };
171
+ let index = slab.insert(tmp)?;
172
+ // SAFETY: tmp is frozen, alive, and slab-rooted; len >= ZERO_COPY_MIN
173
+ // rules out an embedded buffer, so ptr is a stable heap allocation.
174
+ let (ptr, len) = unsafe {
175
+ (
176
+ rb_sys::macros::RSTRING_PTR(tmp) as *const u8,
177
+ rb_sys::macros::RSTRING_LEN(tmp) as usize,
178
+ )
179
+ };
180
+ Some(Bytes::from_owner(PinnedBuf {
181
+ slab: slab.clone(),
182
+ index,
183
+ ptr,
184
+ len,
185
+ }))
186
+ }
187
+
188
+ #[cfg(test)]
189
+ mod tests {
190
+ use super::*;
191
+
192
+ #[test]
193
+ fn insert_release_reuse_cycle() {
194
+ let slab = PinSlab::new();
195
+ let a = slab.insert(0x1000).expect("slot free");
196
+ let b = slab.insert(0x2000).expect("slot free");
197
+ assert_ne!(a, b);
198
+ assert_eq!(slab.occupied(), 2);
199
+
200
+ slab.release(a);
201
+ assert_eq!(slab.occupied(), 1);
202
+
203
+ // The freed slot is claimable again.
204
+ let c = slab.insert(0x3000).expect("slot free");
205
+ assert_eq!(slab.occupied(), 2);
206
+ slab.release(b);
207
+ slab.release(c);
208
+ assert_eq!(slab.occupied(), 0);
209
+ }
210
+
211
+ #[test]
212
+ fn full_slab_refuses_instead_of_evicting() {
213
+ let slab = PinSlab::new();
214
+ let indexes: Vec<usize> = (0..SLAB_CAPACITY)
215
+ .map(|i| slab.insert(0x1000 + i as rb_sys::VALUE).expect("capacity"))
216
+ .collect();
217
+ assert_eq!(slab.occupied(), SLAB_CAPACITY);
218
+ assert!(slab.insert(0x9999).is_none());
219
+
220
+ slab.release(indexes[7]);
221
+ assert_eq!(slab.insert(0x9999), Some(indexes[7]));
222
+ }
223
+ }
@@ -30,7 +30,7 @@ type Taken = Option<BoxedCtx>;
30
30
  /// cost is real (~20% of cycles at saturation, per perf), but a measured
31
31
  /// 20µs spin made things WORSE on oversubscribed cores: spinners steal
32
32
  /// exactly the CPU the tokio threads need.
33
- fn block_take(server: &ServerInner, slot: &Arc<WorkerSlot>) -> Taken {
33
+ fn block_take(server: &ServerInner, slot: &Arc<WorkerSlot>) -> Result<Taken, Error> {
34
34
  if slot.lane_rx.is_some() {
35
35
  return lane_take(server, slot);
36
36
  }
@@ -40,15 +40,16 @@ fn block_take(server: &ServerInner, slot: &Arc<WorkerSlot>) -> Taken {
40
40
  // try_recv never blocks, so the whole GVL release/reacquire (two
41
41
  // scheduler round-trips per request) is skipped entirely.
42
42
  match req_rx.try_recv() {
43
- Ok(ctx) => Some(ctx),
44
- Err(flume::TryRecvError::Disconnected) => None,
43
+ Ok(ctx) => Ok(Some(ctx)),
44
+ Err(flume::TryRecvError::Disconnected) => Ok(None),
45
45
  Err(flume::TryRecvError::Empty) => {
46
- gvl::interruptible(&slot.interrupted, || match req_rx.recv_timeout(TICK) {
47
- Ok(ctx) => Some(Some(ctx)),
48
- Err(flume::RecvTimeoutError::Timeout) => None,
49
- Err(flume::RecvTimeoutError::Disconnected) => Some(None),
50
- })
51
- .flatten()
46
+ let taken =
47
+ gvl::interruptible(&slot.interrupted, || match req_rx.recv_timeout(TICK) {
48
+ Ok(ctx) => Some(Some(ctx)),
49
+ Err(flume::RecvTimeoutError::Timeout) => None,
50
+ Err(flume::RecvTimeoutError::Disconnected) => Some(None),
51
+ })?;
52
+ Ok(taken.flatten())
52
53
  }
53
54
  }
54
55
  }
@@ -56,7 +57,7 @@ fn block_take(server: &ServerInner, slot: &Arc<WorkerSlot>) -> Taken {
56
57
  /// Lane-mode take: own lane first (no wake needed while the dispatcher
57
58
  /// keeps feeding an awake lane), then steal from siblings, then park on
58
59
  /// the own lane with the parked flag raised so the dispatcher avoids it.
59
- fn lane_take(server: &ServerInner, slot: &Arc<WorkerSlot>) -> Taken {
60
+ fn lane_take(server: &ServerInner, slot: &Arc<WorkerSlot>) -> Result<Taken, Error> {
60
61
  let lane_rx = slot.lane_rx.as_ref().expect("lane_take without lane");
61
62
 
62
63
  let steal = || -> Option<BoxedCtx> {
@@ -76,12 +77,12 @@ fn lane_take(server: &ServerInner, slot: &Arc<WorkerSlot>) -> Taken {
76
77
 
77
78
  // Hot path, GVL still held: own lane, then a steal sweep.
78
79
  match lane_rx.try_recv() {
79
- Ok(ctx) => return Some(ctx),
80
- Err(flume::TryRecvError::Disconnected) => return None,
80
+ Ok(ctx) => return Ok(Some(ctx)),
81
+ Err(flume::TryRecvError::Disconnected) => return Ok(None),
81
82
  Err(flume::TryRecvError::Empty) => {}
82
83
  }
83
84
  if let Some(ctx) = steal() {
84
- return Some(ctx);
85
+ return Ok(Some(ctx));
85
86
  }
86
87
 
87
88
  // Park. The flag-then-recheck order closes the race with a dispatcher
@@ -96,10 +97,11 @@ fn lane_take(server: &ServerInner, slot: &Arc<WorkerSlot>) -> Taken {
96
97
  Err(flume::RecvTimeoutError::Timeout) => steal().map(Some),
97
98
  Err(flume::RecvTimeoutError::Disconnected) => Some(None),
98
99
  }
99
- })
100
- .flatten();
100
+ });
101
+ // Unpark before propagating any panic, or the dispatcher shuns this
102
+ // lane for good.
101
103
  slot.parked.store(false, Ordering::SeqCst);
102
- taken
104
+ Ok(taken?.flatten())
103
105
  }
104
106
 
105
107
  /// Wrap a ctx into its env Hash, with the Ruby request handle embedded
@@ -137,7 +139,7 @@ fn checkout(ruby: &Ruby, server_id: u64, worker_id: usize) -> Result<Option<Chec
137
139
  slot.current.lock().clear();
138
140
  slot.interrupted.store(false, Ordering::SeqCst);
139
141
 
140
- Ok(block_take(&server, &slot).map(|ctx| (server, slot, ctx)))
142
+ Ok(block_take(&server, &slot)?.map(|ctx| (server, slot, ctx)))
141
143
  }
142
144
 
143
145
  /// Take one request; returns its env Hash (request handle inside under
@@ -52,6 +52,9 @@ pub struct ServerInner {
52
52
  pub lanes: bool,
53
53
  /// Round-robin cursor for lane dispatch.
54
54
  pub lane_cursor: AtomicUsize,
55
+ /// GC roots for zero-copy response buffers (pin.rs). The Ruby Server
56
+ /// object holds the marking PinKeeper for this slab.
57
+ pub pin_slab: Arc<crate::pin::PinSlab>,
55
58
  }
56
59
 
57
60
  /// One per worker *thread* (slot count = workers × threads). The interrupt
@@ -189,6 +192,7 @@ pub fn test_server(lanes: bool, queue_depth: usize) -> Arc<ServerInner> {
189
192
  access_log: None,
190
193
  lanes,
191
194
  lane_cursor: AtomicUsize::new(0),
195
+ pin_slab: Arc::new(crate::pin::PinSlab::new()),
192
196
  })
193
197
  }
194
198
 
@@ -37,6 +37,9 @@ pub struct RequestCtx {
37
37
  /// The owning worker slot (set at admit time, queue.rs); its interrupt
38
38
  /// flag makes blocked body reads/writes interruptible like the queue pop.
39
39
  pub slot: Option<Arc<crate::registry::WorkerSlot>>,
40
+ /// The server's zero-copy roots: large response bodies are pinned
41
+ /// here and ride to hyper without a copy (pin.rs).
42
+ pub pin_slab: Arc<crate::pin::PinSlab>,
40
43
  pub responder: Arc<Responder>,
41
44
  }
42
45
 
@@ -52,12 +55,12 @@ impl Drop for RequestCtx {
52
55
 
53
56
  /// Block on a channel operation with the GVL released, waking on the
54
57
  /// slot's interrupt flag. `attempt` performs one bounded tick and returns
55
- /// Some when done; None on timeout. Outer None = interrupted. Every Ruby
58
+ /// Some when done; None on timeout. Ok(None) = interrupted. Every Ruby
56
59
  /// handle is created by `admit` (queue.rs), which always sets the slot.
57
60
  fn block_on<T>(
58
61
  slot: &Option<Arc<crate::registry::WorkerSlot>>,
59
62
  attempt: impl FnMut() -> Option<T>,
60
- ) -> Option<T> {
63
+ ) -> Result<Option<T>, Error> {
61
64
  let slot = slot.as_ref().expect("slot set by admit");
62
65
  gvl::interruptible(&slot.interrupted, attempt)
63
66
  }
@@ -228,13 +231,23 @@ pub fn respond_simple(
228
231
  ) -> Result<bool, Error> {
229
232
  let ctx = request.0.borrow();
230
233
  let builder = build_head(status, headers)?;
231
- let bytes = Bytes::copy_from_slice(unsafe { body.as_slice() });
234
+ let bytes = body_bytes(&ctx, body);
232
235
  let response = builder
233
236
  .body(full_body(bytes))
234
237
  .map_err(|e| invalid_response(ruby, e))?;
235
238
  Ok(ctx.responder.send_response(response))
236
239
  }
237
240
 
241
+ /// Body bytes for hyper: large bodies are pinned in place (zero-copy),
242
+ /// everything else is copied out of the Ruby string, which may be
243
+ /// mutated or collected the moment we return.
244
+ fn body_bytes(ctx: &RequestCtx, body: RString) -> Bytes {
245
+ if let Some(bytes) = crate::pin::pinned_bytes(&ctx.pin_slab, body) {
246
+ return bytes;
247
+ }
248
+ Bytes::copy_from_slice(unsafe { body.as_slice() })
249
+ }
250
+
238
251
  impl Request {
239
252
  /// Next chunk of the request body, at most `max_len` bytes; nil at EOF.
240
253
  /// Blocks (GVL released) until the client sends more.
@@ -256,7 +269,7 @@ impl Request {
256
269
  Err(flume::RecvTimeoutError::Timeout) => None,
257
270
  Err(flume::RecvTimeoutError::Disconnected) => Some(None),
258
271
  }
259
- });
272
+ })?;
260
273
  match outcome {
261
274
  Some(Some(bytes)) => bytes,
262
275
  Some(None) => {
@@ -306,7 +319,7 @@ impl Request {
306
319
  "Kino: response stream not started or already finished",
307
320
  ));
308
321
  };
309
- let bytes = Bytes::copy_from_slice(unsafe { chunk.as_slice() });
322
+ let bytes = body_bytes(&ctx, chunk);
310
323
  let mut pending = Some(Ok(BodyFrame::data(bytes)));
311
324
 
312
325
  let outcome = block_on(&ctx.slot, || {
@@ -319,7 +332,7 @@ impl Request {
319
332
  }
320
333
  Err(flume::SendTimeoutError::Disconnected(_)) => Some(false),
321
334
  }
322
- });
335
+ })?;
323
336
  match outcome {
324
337
  // Receiver dropped = client went away; the app keeps writing
325
338
  // into the void harmlessly (Rack has no error contract here).
@@ -410,6 +423,7 @@ pub fn test_ctx() -> crate::registry::BoxedCtx {
410
423
  body_timeout: Arc::new(std::sync::atomic::AtomicBool::new(false)),
411
424
  leftover: None,
412
425
  slot: None,
426
+ pin_slab: Arc::new(crate::pin::PinSlab::new()),
413
427
  responder: Arc::new(Responder::new(head_tx)),
414
428
  })
415
429
  }
@@ -112,6 +112,7 @@ pub fn server_start(ruby: &Ruby, config: magnus::RHash) -> Result<(u64, u16), Er
112
112
  access_log: log_requests.then(|| crate::logsink::Sink::new(std::io::stdout())),
113
113
  lanes,
114
114
  lane_cursor: std::sync::atomic::AtomicUsize::new(0),
115
+ pin_slab: Arc::new(crate::pin::PinSlab::new()),
115
116
  });
116
117
 
117
118
  let tokio_listener = {
@@ -350,6 +351,7 @@ async fn handle_request(
350
351
  body_timeout,
351
352
  leftover: None,
352
353
  slot: None,
354
+ pin_slab: server.pin_slab.clone(),
353
355
  responder,
354
356
  });
355
357
 
@@ -589,6 +591,16 @@ pub fn shutdown_runtime(_ruby: &Ruby, server_id: u64, timeout_ms: u64) -> Result
589
591
  Ok(())
590
592
  }
591
593
 
594
+ /// The GC anchor for this server's zero-copy pins: the Ruby Server holds
595
+ /// it for its lifetime, so pinned buffers survive worker-ractor crashes.
596
+ pub fn pin_keeper(
597
+ ruby: &Ruby,
598
+ server_id: u64,
599
+ ) -> Result<magnus::typed_data::Obj<crate::pin::PinKeeper>, Error> {
600
+ let server = registry::get(ruby, server_id)?;
601
+ Ok(ruby.obj_wrap(crate::pin::PinKeeper(server.pin_slab.clone())))
602
+ }
603
+
592
604
  /// Errors print in red on color terminals. Covers worker errors,
593
605
  /// supervisor crash reports, and everything apps write to rack.errors.
594
606
  pub fn log_error(message: String) {
@@ -71,10 +71,22 @@ pub fn take(ruby: &magnus::Ruby, id: u64) -> Result<Option<String>, magnus::Erro
71
71
  Err(flume::RecvTimeoutError::Timeout) => None,
72
72
  Err(flume::RecvTimeoutError::Disconnected) => Some(None),
73
73
  }
74
- });
74
+ })?;
75
75
  Ok(taken.flatten())
76
76
  }
77
77
 
78
+ /// Panic inside a GVL-released block: proves it surfaces as a Ruby
79
+ /// exception instead of unwinding into the VM and killing the process.
80
+ /// The default panic hook is silenced around the intentional panic so
81
+ /// spec output stays clean.
82
+ pub fn panic_in_release() -> Result<(), magnus::Error> {
83
+ let hook = std::panic::take_hook();
84
+ std::panic::set_hook(Box::new(|_| {}));
85
+ let result = gvl::without_gvl(|| panic!("intentional test panic"), None);
86
+ std::panic::set_hook(hook);
87
+ result
88
+ }
89
+
78
90
  pub fn close(ruby: &magnus::Ruby, id: u64) -> Result<(), magnus::Error> {
79
91
  let chan = lookup(ruby, id)?;
80
92
  chan.tx.lock().take();