connectrpc-hyper 0.3.0 → 0.3.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.
- checksums.yaml +4 -4
- data/README.md +110 -11
- data/ext/connectrpc_hyper/src/body.rs +88 -2
- data/ext/connectrpc_hyper/src/call.rs +396 -49
- data/ext/connectrpc_hyper/src/failure.rs +7 -0
- data/ext/connectrpc_hyper/src/lib.rs +57 -1
- data/ext/connectrpc_hyper/src/pool.rs +17 -6
- data/ext/connectrpc_hyper/src/runtime.rs +230 -24
- data/lib/connectrpc/hyper/errors.rb +8 -4
- data/lib/connectrpc/hyper/exchange.rb +3 -3
- data/lib/connectrpc/hyper/transport.rb +5 -1
- data/lib/connectrpc/hyper/version.rb +1 -1
- metadata +2 -2
|
@@ -55,6 +55,20 @@ fn failure_error(ruby: &Ruby, failure: Failure) -> Error {
|
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
+
/// One streaming write's limit (`Call::write`). h2 refuses a DATA chunk
|
|
59
|
+
/// above its 2^31 - 1 byte window outright, which would drop the request
|
|
60
|
+
/// body while the response still waits for it.
|
|
61
|
+
const MAX_WRITE: usize = 1 << 31;
|
|
62
|
+
|
|
63
|
+
/// Rejects a streaming write of `len` bytes, the string's own length, at or
|
|
64
|
+
/// above `MAX_WRITE`.
|
|
65
|
+
fn check_write_len(len: usize) -> Result<(), &'static str> {
|
|
66
|
+
if len >= MAX_WRITE {
|
|
67
|
+
return Err("A streaming write must be smaller than 2 GiB");
|
|
68
|
+
}
|
|
69
|
+
Ok(())
|
|
70
|
+
}
|
|
71
|
+
|
|
58
72
|
fn arg_error(ruby: &Ruby, message: &'static str) -> Error {
|
|
59
73
|
Error::new(ruby.exception_arg_error(), message)
|
|
60
74
|
}
|
|
@@ -340,6 +354,13 @@ impl Driver {
|
|
|
340
354
|
let core = rb_self.owned(ruby)?.clone();
|
|
341
355
|
let http2 = core.config.settings.http2;
|
|
342
356
|
let spec = request_spec(ruby, "POST", &url, pairs, kind, grpc, proxy, http2)?;
|
|
357
|
+
// Before the runtime starts: Ruby measured `timeout` before this call,
|
|
358
|
+
// so a slow cold start must not extend the deadline sent to the peer.
|
|
359
|
+
let deadline = instant(timeout);
|
|
360
|
+
// Each operation on the call takes a stream half before it drives. A
|
|
361
|
+
// runtime start that panicked there would drop the half and leave the
|
|
362
|
+
// exchange unusable, so the runtime starts here, before admission.
|
|
363
|
+
runtime::handle();
|
|
343
364
|
let inflight = core
|
|
344
365
|
.admit()
|
|
345
366
|
.map_err(|failure| failure_error(ruby, failure))?;
|
|
@@ -347,7 +368,7 @@ impl Driver {
|
|
|
347
368
|
core,
|
|
348
369
|
spec,
|
|
349
370
|
require_h2,
|
|
350
|
-
|
|
371
|
+
deadline,
|
|
351
372
|
cancel.signal.clone(),
|
|
352
373
|
inflight,
|
|
353
374
|
);
|
|
@@ -521,6 +542,9 @@ impl Call {
|
|
|
521
542
|
data: RString,
|
|
522
543
|
timeout: Option<f64>,
|
|
523
544
|
) -> Result<Value, Error> {
|
|
545
|
+
// Checked first, before any copy, on the string's own length, so a
|
|
546
|
+
// `String#bytesize` override cannot move the check either way.
|
|
547
|
+
check_write_len(data.len()).map_err(|message| arg_error(ruby, message))?;
|
|
524
548
|
let state = rb_self.state(ruby)?;
|
|
525
549
|
let mut send = state
|
|
526
550
|
.send
|
|
@@ -608,6 +632,14 @@ fn wait_bodies() -> (usize, usize) {
|
|
|
608
632
|
gvl::wait_bodies()
|
|
609
633
|
}
|
|
610
634
|
|
|
635
|
+
fn fail_runtime_starts(count: usize) {
|
|
636
|
+
runtime::fail_starts(count);
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
fn runtime_start_bound(bound: f64) {
|
|
640
|
+
runtime::start_bound(seconds(bound));
|
|
641
|
+
}
|
|
642
|
+
|
|
611
643
|
#[magnus::init]
|
|
612
644
|
fn init(ruby: &Ruby) -> Result<(), Error> {
|
|
613
645
|
let connectrpc = ruby.define_module("ConnectRPC")?;
|
|
@@ -619,6 +651,8 @@ fn init(ruby: &Ruby) -> Result<(), Error> {
|
|
|
619
651
|
native.define_singleton_method("active_waits", function!(active_waits, 0))?;
|
|
620
652
|
native.define_singleton_method("resumed_waits", function!(resumed_waits, 0))?;
|
|
621
653
|
native.define_singleton_method("wait_bodies", function!(wait_bodies, 0))?;
|
|
654
|
+
native.define_singleton_method("fail_runtime_starts", function!(fail_runtime_starts, 1))?;
|
|
655
|
+
native.define_singleton_method("runtime_start_bound", function!(runtime_start_bound, 1))?;
|
|
622
656
|
|
|
623
657
|
let cancel: RClass = native.define_class("Cancel", ruby.class_object())?;
|
|
624
658
|
cancel.define_singleton_method("new", function!(Cancel::new, 0))?;
|
|
@@ -650,3 +684,25 @@ fn init(ruby: &Ruby) -> Result<(), Error> {
|
|
|
650
684
|
call.define_method("finished?", method!(Call::is_finished, 0))?;
|
|
651
685
|
Ok(())
|
|
652
686
|
}
|
|
687
|
+
|
|
688
|
+
#[cfg(test)]
|
|
689
|
+
mod tests {
|
|
690
|
+
use super::*;
|
|
691
|
+
|
|
692
|
+
// h2's largest DATA chunk, its 2^31 - 1 byte window, and one byte more.
|
|
693
|
+
#[test]
|
|
694
|
+
fn a_write_of_h2s_largest_chunk_passes() {
|
|
695
|
+
assert_eq!(check_write_len(2_147_483_647), Ok(()));
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
#[test]
|
|
699
|
+
fn a_write_of_2_gib_or_more_fails() {
|
|
700
|
+
for len in [2_147_483_648, usize::MAX] {
|
|
701
|
+
assert_eq!(
|
|
702
|
+
check_write_len(len),
|
|
703
|
+
Err("A streaming write must be smaller than 2 GiB"),
|
|
704
|
+
"a write of {len} bytes"
|
|
705
|
+
);
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
}
|
|
@@ -1087,11 +1087,16 @@ impl DriverCore {
|
|
|
1087
1087
|
/// It is a runtime task outside the tracker, which `force` seals and waits
|
|
1088
1088
|
/// for, so no closer's interruption ends it part way. Every call returns
|
|
1089
1089
|
/// the signal that fires once the outcome is published.
|
|
1090
|
+
///
|
|
1091
|
+
/// The runtime starts before teardown is claimed: a start that fails
|
|
1092
|
+
/// panics with nothing claimed, so a later close starts teardown instead
|
|
1093
|
+
/// of waiting for a signal that no task would fire.
|
|
1090
1094
|
pub fn start_teardown(self: &Arc<Self>, grace: Duration) -> Arc<Signal> {
|
|
1095
|
+
let runtime = runtime::handle();
|
|
1091
1096
|
if !self.teardown.swap(true, Ordering::SeqCst) {
|
|
1092
1097
|
self.begin_close();
|
|
1093
1098
|
let core = self.clone();
|
|
1094
|
-
runtime
|
|
1099
|
+
runtime.spawn(async move {
|
|
1095
1100
|
let expires = tokio::time::Instant::now() + grace;
|
|
1096
1101
|
let drained = tokio::time::timeout_at(expires, core.wait_drained())
|
|
1097
1102
|
.await
|
|
@@ -1119,13 +1124,19 @@ impl DriverCore {
|
|
|
1119
1124
|
}
|
|
1120
1125
|
|
|
1121
1126
|
/// Ends every call's waits and every owned task, then waits for the
|
|
1122
|
-
/// tasks.
|
|
1123
|
-
///
|
|
1124
|
-
///
|
|
1125
|
-
/// that observes
|
|
1127
|
+
/// tasks. Spawns are refused first, so a call mid-poll registers no task
|
|
1128
|
+
/// (such as a dial's dispatcher, which would send its request) once
|
|
1129
|
+
/// teardown started. `shutdown` fires before the seal aborts any task,
|
|
1130
|
+
/// so a site that observes an abort's effect, such as a write that finds
|
|
1131
|
+
/// its request body dropped with its connection, also sees `shutdown`
|
|
1132
|
+
/// and maps it to a forced phrase.
|
|
1126
1133
|
pub async fn force(&self) {
|
|
1127
|
-
self.tracker.
|
|
1134
|
+
self.tracker.refuse();
|
|
1128
1135
|
self.shutdown.fire();
|
|
1136
|
+
// Spec hold point: it parks forced teardown after `shutdown` fired and
|
|
1137
|
+
// before any task is aborted.
|
|
1138
|
+
self.holds.reach("force:shutdown").await;
|
|
1139
|
+
self.tracker.seal();
|
|
1129
1140
|
// Spec hold point: a spec armed with a zero bound observes the seal.
|
|
1130
1141
|
self.holds.reach("force:sealed").await;
|
|
1131
1142
|
self.tracker.wait_empty().await;
|
|
@@ -6,14 +6,20 @@
|
|
|
6
6
|
//! runtime is ever dropped or used in a child: `ForkGuard` leaks it, the Ruby
|
|
7
7
|
//! transport builds a new driver, and the runtime slot is replaced. No state
|
|
8
8
|
//! here is guarded by a lock a fork can inherit.
|
|
9
|
+
use std::any::Any;
|
|
9
10
|
use std::collections::HashMap;
|
|
10
11
|
use std::future::Future;
|
|
12
|
+
use std::io;
|
|
11
13
|
use std::mem::ManuallyDrop;
|
|
14
|
+
use std::panic::{AssertUnwindSafe, catch_unwind, resume_unwind};
|
|
12
15
|
use std::pin::Pin;
|
|
13
16
|
use std::ptr;
|
|
14
|
-
use std::sync::atomic::{AtomicPtr, Ordering};
|
|
17
|
+
use std::sync::atomic::{AtomicPtr, AtomicU64, AtomicUsize, Ordering};
|
|
18
|
+
use std::sync::mpsc::{self, RecvTimeoutError};
|
|
15
19
|
use std::sync::{Arc, Mutex};
|
|
16
20
|
use std::task::{Context, Poll};
|
|
21
|
+
use std::thread;
|
|
22
|
+
use std::time::Duration;
|
|
17
23
|
|
|
18
24
|
use tokio::runtime::{Builder, Handle, Runtime};
|
|
19
25
|
use tokio::sync::Notify;
|
|
@@ -31,13 +37,43 @@ struct Slot {
|
|
|
31
37
|
/// The current process's runtime, or a leaked parent's, or null.
|
|
32
38
|
static RUNTIME: AtomicPtr<Slot> = AtomicPtr::new(ptr::null_mut());
|
|
33
39
|
|
|
40
|
+
/// Cold starts a spec asked to fail (`fail_starts`).
|
|
41
|
+
static FAIL_STARTS: AtomicUsize = AtomicUsize::new(0);
|
|
42
|
+
|
|
43
|
+
/// How long a cold caller waits for a start attempt, in nanoseconds.
|
|
44
|
+
static START_BOUND: AtomicU64 = AtomicU64::new(5_000_000_000);
|
|
45
|
+
|
|
46
|
+
/// For specs: the next `count` cold starts in this process panic as tokio
|
|
47
|
+
/// does when the OS refuses a worker thread.
|
|
48
|
+
pub fn fail_starts(count: usize) {
|
|
49
|
+
FAIL_STARTS.store(count, Ordering::SeqCst);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/// For specs: how long later cold callers wait for a start attempt.
|
|
53
|
+
pub fn start_bound(bound: Duration) {
|
|
54
|
+
let nanos = u64::try_from(bound.as_nanos()).unwrap_or(u64::MAX);
|
|
55
|
+
START_BOUND.store(nanos, Ordering::SeqCst);
|
|
56
|
+
}
|
|
57
|
+
|
|
34
58
|
/// The process runtime, created on first use (never when a transport is
|
|
35
|
-
/// built) and recreated in a forked child.
|
|
36
|
-
///
|
|
59
|
+
/// built) and recreated in a forked child.
|
|
60
|
+
///
|
|
61
|
+
/// A cold caller builds the runtime on a short-lived `hyper-rt-start`
|
|
62
|
+
/// thread and waits for it, holding the GVL, for at most the start bound
|
|
63
|
+
/// (5 s). tokio 1.54's `build` never returns when the OS starts the first
|
|
64
|
+
/// worker thread but refuses the second (tokio-rs/tokio#8408; the unwind
|
|
65
|
+
/// joins the running first worker), so the bound is what returns control to
|
|
66
|
+
/// Ruby. A failed or late start unwinds as a panic that reaches Ruby as
|
|
67
|
+
/// `fatal`, with nothing published, so a later caller starts a fresh attempt.
|
|
68
|
+
/// An attempt stuck in tokio keeps its start thread, its first worker and its
|
|
69
|
+
/// I/O driver for the life of the process; if it finishes after all, its
|
|
70
|
+
/// runtime serves later callers. Call this before claiming anything an unwind
|
|
71
|
+
/// would leave claimed, such as a lock or a one-shot flag, so a later caller
|
|
72
|
+
/// can retry the start.
|
|
37
73
|
pub fn handle() -> Handle {
|
|
38
74
|
let me = pid();
|
|
39
|
-
let mut current = RUNTIME.load(Ordering::Acquire);
|
|
40
75
|
loop {
|
|
76
|
+
let current = RUNTIME.load(Ordering::Acquire);
|
|
41
77
|
// SAFETY: a published slot is never freed: slots are leaked for the
|
|
42
78
|
// life of the process, including inherited ones in a child.
|
|
43
79
|
if let Some(slot) = unsafe { current.as_ref() } {
|
|
@@ -45,29 +81,109 @@ pub fn handle() -> Handle {
|
|
|
45
81
|
return slot.runtime.handle().clone();
|
|
46
82
|
}
|
|
47
83
|
}
|
|
48
|
-
let
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
.
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
84
|
+
let bound = Duration::from_nanos(START_BOUND.load(Ordering::SeqCst));
|
|
85
|
+
if let Err(message) = start(&RUNTIME, me, bound, build) {
|
|
86
|
+
// Not `panic!`: the panic hook writes to stderr synchronously,
|
|
87
|
+
// which can block (on a full pipe, say) while this caller holds
|
|
88
|
+
// the GVL. magnus raises a `String` payload as `fatal`.
|
|
89
|
+
resume_unwind(Box::new(message));
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
fn build() -> io::Result<Runtime> {
|
|
95
|
+
if FAIL_STARTS
|
|
96
|
+
.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |n| n.checked_sub(1))
|
|
97
|
+
.is_ok()
|
|
98
|
+
{
|
|
99
|
+
panic!("OS can't spawn worker thread: failure injected by a spec");
|
|
100
|
+
}
|
|
101
|
+
Builder::new_multi_thread()
|
|
102
|
+
.worker_threads(2)
|
|
103
|
+
.thread_name("hyper-runtime")
|
|
104
|
+
.enable_all()
|
|
105
|
+
.build()
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/// One start attempt: `build` runs on a fresh start thread, which publishes
|
|
109
|
+
/// the runtime to `slot` itself (`RUNTIME`, except in unit tests), and the
|
|
110
|
+
/// caller waits for its report for at most `bound`. Each attempt has its own
|
|
111
|
+
/// channel, so no shared state outlives it and a forked child inherits none
|
|
112
|
+
/// mid-use. An abandoned attempt's report is dropped.
|
|
113
|
+
fn start<F>(
|
|
114
|
+
slot: &'static AtomicPtr<Slot>,
|
|
115
|
+
me: u32,
|
|
116
|
+
bound: Duration,
|
|
117
|
+
build: F,
|
|
118
|
+
) -> Result<(), String>
|
|
119
|
+
where
|
|
120
|
+
F: FnOnce() -> io::Result<Runtime> + Send + 'static,
|
|
121
|
+
{
|
|
122
|
+
let (report, outcome) = mpsc::sync_channel(1);
|
|
123
|
+
let attempt = thread::Builder::new()
|
|
124
|
+
.name("hyper-rt-start".into())
|
|
125
|
+
.spawn(move || {
|
|
126
|
+
let result = match catch_unwind(AssertUnwindSafe(build)) {
|
|
127
|
+
Ok(Ok(runtime)) => {
|
|
128
|
+
publish(slot, me, runtime);
|
|
129
|
+
Ok(())
|
|
130
|
+
}
|
|
131
|
+
Ok(Err(error)) => Err(format!("tokio runtime did not start: {error}")),
|
|
132
|
+
Err(payload) => Err(panic_message(payload.as_ref())),
|
|
133
|
+
};
|
|
134
|
+
let _ = report.send(result);
|
|
135
|
+
})
|
|
136
|
+
.map_err(|error| format!("OS can't spawn runtime start thread: {error}"))?;
|
|
137
|
+
match outcome.recv_timeout(bound) {
|
|
138
|
+
// The start thread sent its last message and is exiting: joining it
|
|
139
|
+
// is short and leaves no extra thread behind a returned start.
|
|
140
|
+
Ok(result) => {
|
|
141
|
+
let _ = attempt.join();
|
|
142
|
+
result
|
|
143
|
+
}
|
|
144
|
+
// Abandoned, not joined: the start may be stuck in tokio for good.
|
|
145
|
+
Err(RecvTimeoutError::Timeout) => {
|
|
146
|
+
Err(format!("tokio runtime did not start within {bound:?}"))
|
|
147
|
+
}
|
|
148
|
+
Err(RecvTimeoutError::Disconnected) => {
|
|
149
|
+
let _ = attempt.join();
|
|
150
|
+
Err("tokio runtime start thread ended without a result".into())
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/// Publishes a built runtime, unless another attempt already published one
|
|
156
|
+
/// for this process: then this one is shut down. An inherited slot, if any,
|
|
157
|
+
/// is leaked: its threads do not exist here.
|
|
158
|
+
fn publish(slot: &AtomicPtr<Slot>, me: u32, runtime: Runtime) {
|
|
159
|
+
let fresh = Box::into_raw(Box::new(Slot { pid: me, runtime }));
|
|
160
|
+
let mut current = slot.load(Ordering::Acquire);
|
|
161
|
+
loop {
|
|
162
|
+
// SAFETY: a published slot is never freed (see `handle`).
|
|
163
|
+
let owned = unsafe { current.as_ref() }.is_some_and(|published| published.pid == me);
|
|
164
|
+
if owned {
|
|
165
|
+
// SAFETY: `fresh` was never published, so this thread owns it.
|
|
166
|
+
let lost = unsafe { Box::from_raw(fresh) };
|
|
167
|
+
lost.runtime.shutdown_background();
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
match slot.compare_exchange(current, fresh, Ordering::AcqRel, Ordering::Acquire) {
|
|
171
|
+
Ok(_) => return,
|
|
172
|
+
Err(actual) => current = actual,
|
|
67
173
|
}
|
|
68
174
|
}
|
|
69
175
|
}
|
|
70
176
|
|
|
177
|
+
fn panic_message(payload: &(dyn Any + Send)) -> String {
|
|
178
|
+
if let Some(message) = payload.downcast_ref::<&'static str>() {
|
|
179
|
+
(*message).to_owned()
|
|
180
|
+
} else if let Some(message) = payload.downcast_ref::<String>() {
|
|
181
|
+
message.clone()
|
|
182
|
+
} else {
|
|
183
|
+
"tokio runtime start panicked".to_owned()
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
71
187
|
/// A process-scoped value in a lock-free slot: the parent's value is leaked
|
|
72
188
|
/// in a child, and concurrent initialisers elect one value.
|
|
73
189
|
pub struct PidCell<T: 'static> {
|
|
@@ -272,6 +388,9 @@ impl Tracker {
|
|
|
272
388
|
where
|
|
273
389
|
F: Future<Output = ()> + Send + 'static,
|
|
274
390
|
{
|
|
391
|
+
// Before the lock: a failed runtime start unwinds, and unwinding with
|
|
392
|
+
// the lock held would deadlock in `Finished::drop`.
|
|
393
|
+
let runtime = handle();
|
|
275
394
|
// The lock spans the spawn, so a task that ends at once removes an
|
|
276
395
|
// entry that already exists, and `seal` waits for a registration in
|
|
277
396
|
// progress, then aborts it.
|
|
@@ -289,7 +408,7 @@ impl Tracker {
|
|
|
289
408
|
tracker: self.clone(),
|
|
290
409
|
id,
|
|
291
410
|
};
|
|
292
|
-
let join =
|
|
411
|
+
let join = runtime.spawn(Tracked {
|
|
293
412
|
future: Box::pin(future),
|
|
294
413
|
_finished: finished,
|
|
295
414
|
});
|
|
@@ -298,6 +417,12 @@ impl Tracker {
|
|
|
298
417
|
Task(Some(abort))
|
|
299
418
|
}
|
|
300
419
|
|
|
420
|
+
/// Refuses every later spawn, aborting nothing yet: forced teardown
|
|
421
|
+
/// refuses spawns before it fires `shutdown`, and aborts after (`seal`).
|
|
422
|
+
pub fn refuse(&self) {
|
|
423
|
+
self.tasks.lock().unwrap_or_else(|e| e.into_inner()).sealed = true;
|
|
424
|
+
}
|
|
425
|
+
|
|
301
426
|
/// Refuses every later spawn and aborts every registered task.
|
|
302
427
|
pub fn seal(&self) {
|
|
303
428
|
let mut state = self.tasks.lock().unwrap_or_else(|e| e.into_inner());
|
|
@@ -350,3 +475,84 @@ where
|
|
|
350
475
|
self.0.spawn(future);
|
|
351
476
|
}
|
|
352
477
|
}
|
|
478
|
+
|
|
479
|
+
#[cfg(test)]
|
|
480
|
+
mod tests {
|
|
481
|
+
use super::*;
|
|
482
|
+
|
|
483
|
+
// Released tokio panics when it cannot spawn a worker; tokio-rs/tokio#8752
|
|
484
|
+
// (unreleased) makes `build` return the error instead.
|
|
485
|
+
#[test]
|
|
486
|
+
fn a_build_error_fails_the_attempt_and_publishes_nothing() {
|
|
487
|
+
let before = RUNTIME.load(Ordering::SeqCst);
|
|
488
|
+
let result = start(&RUNTIME, pid(), Duration::from_secs(60), || {
|
|
489
|
+
Err(io::Error::other("worker refused"))
|
|
490
|
+
});
|
|
491
|
+
assert_eq!(
|
|
492
|
+
result,
|
|
493
|
+
Err("tokio runtime did not start: worker refused".to_owned())
|
|
494
|
+
);
|
|
495
|
+
assert_eq!(RUNTIME.load(Ordering::SeqCst), before);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
#[test]
|
|
499
|
+
fn a_start_still_running_at_the_bound_fails_the_attempt() {
|
|
500
|
+
let (release, released) = mpsc::channel::<()>();
|
|
501
|
+
let (ended, end) = mpsc::channel();
|
|
502
|
+
let result = start(&RUNTIME, pid(), Duration::from_millis(10), move || {
|
|
503
|
+
let _ = released.recv();
|
|
504
|
+
let _ = ended.send(());
|
|
505
|
+
Err(io::Error::other("released"))
|
|
506
|
+
});
|
|
507
|
+
assert_eq!(
|
|
508
|
+
result,
|
|
509
|
+
Err("tokio runtime did not start within 10ms".to_owned())
|
|
510
|
+
);
|
|
511
|
+
release.send(()).expect("the abandoned start is waiting");
|
|
512
|
+
end.recv().expect("the abandoned start ends");
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
// An abandoned start that succeeds later still publishes, and a start that
|
|
516
|
+
// succeeds after that loses the election: the first runtime stays.
|
|
517
|
+
#[test]
|
|
518
|
+
fn a_late_start_publishes_its_runtime_and_a_later_start_loses() {
|
|
519
|
+
static SLOT: AtomicPtr<Slot> = AtomicPtr::new(ptr::null_mut());
|
|
520
|
+
let runtime = || {
|
|
521
|
+
Builder::new_multi_thread()
|
|
522
|
+
.worker_threads(1)
|
|
523
|
+
.enable_all()
|
|
524
|
+
.build()
|
|
525
|
+
};
|
|
526
|
+
let (release, released) = mpsc::channel::<()>();
|
|
527
|
+
let late = start(&SLOT, pid(), Duration::from_millis(10), move || {
|
|
528
|
+
let _ = released.recv();
|
|
529
|
+
runtime()
|
|
530
|
+
});
|
|
531
|
+
assert_eq!(
|
|
532
|
+
late,
|
|
533
|
+
Err("tokio runtime did not start within 10ms".to_owned())
|
|
534
|
+
);
|
|
535
|
+
assert!(SLOT.load(Ordering::SeqCst).is_null());
|
|
536
|
+
release.send(()).expect("the abandoned start is waiting");
|
|
537
|
+
let deadline = std::time::Instant::now() + Duration::from_secs(10);
|
|
538
|
+
let winner = loop {
|
|
539
|
+
let published = SLOT.load(Ordering::SeqCst);
|
|
540
|
+
if !published.is_null() {
|
|
541
|
+
break published;
|
|
542
|
+
}
|
|
543
|
+
assert!(
|
|
544
|
+
std::time::Instant::now() < deadline,
|
|
545
|
+
"the late start never published"
|
|
546
|
+
);
|
|
547
|
+
thread::yield_now();
|
|
548
|
+
};
|
|
549
|
+
assert_eq!(
|
|
550
|
+
start(&SLOT, pid(), Duration::from_secs(60), runtime),
|
|
551
|
+
Ok(())
|
|
552
|
+
);
|
|
553
|
+
assert_eq!(SLOT.load(Ordering::SeqCst), winner);
|
|
554
|
+
// SAFETY: published slots are never freed.
|
|
555
|
+
let handle = unsafe { &*winner }.runtime.handle().clone();
|
|
556
|
+
assert_eq!(handle.block_on(handle.spawn(async { 7 })).unwrap(), 7);
|
|
557
|
+
}
|
|
558
|
+
}
|
|
@@ -28,7 +28,7 @@ module ConnectRPC
|
|
|
28
28
|
connect: "connection failed", connect_timeout: "timed out", timeout: "timed out",
|
|
29
29
|
tls_verify: "TLS verification failed", tls: "TLS error", closed: "connection closed",
|
|
30
30
|
goaway: "connection closed", shutdown: "connection closed", protocol: "malformed response",
|
|
31
|
-
alpn: "ALPN negotiation failed", reset: "stream reset",
|
|
31
|
+
alpn: "ALPN negotiation failed", reset: "stream reset", ended: "stream closed",
|
|
32
32
|
# Core NetHTTP's proxy phrases. Proxy reply bytes never reach them.
|
|
33
33
|
proxy_auth: "proxy authentication failed", proxy_refused: "proxy tunnel refused",
|
|
34
34
|
proxy_failed: "proxy tunnel failed", proxy_timeout: "proxy tunnel timed out",
|
|
@@ -64,13 +64,17 @@ module ConnectRPC
|
|
|
64
64
|
|
|
65
65
|
# Runs one native operation for a request: cancellation and deadline
|
|
66
66
|
# first, then lifecycle failure recording (a per-wait timeout included,
|
|
67
|
-
# as the Async transport records it), then the fixed phrase.
|
|
68
|
-
|
|
67
|
+
# as the Async transport records it), then the fixed phrase. A write's
|
|
68
|
+
# timeout keeps the native failure as its cause, so a call that forgives
|
|
69
|
+
# the write also drops the failure recorded here.
|
|
70
|
+
def self.native(request, grpc:, write: false)
|
|
69
71
|
yield
|
|
70
72
|
rescue Native::Failure => e
|
|
71
73
|
request.cancellation ? request.cancellation.check! : request.deadline&.check!
|
|
72
74
|
request.lifecycle&.record_failure(e)
|
|
73
|
-
|
|
75
|
+
if e.kind == :timeout
|
|
76
|
+
raise Error.new(:unavailable, "HTTP transport failed: timed out"), cause: (e if write)
|
|
77
|
+
end
|
|
74
78
|
|
|
75
79
|
translate(e, grpc: grpc)
|
|
76
80
|
end
|
|
@@ -35,7 +35,7 @@ module ConnectRPC
|
|
|
35
35
|
end
|
|
36
36
|
|
|
37
37
|
def write(bytes)
|
|
38
|
-
wait(nil) { |limit| @native.write(bytes, limit) }
|
|
38
|
+
wait(nil, write: true) { |limit| @native.write(bytes, limit) }
|
|
39
39
|
nil
|
|
40
40
|
end
|
|
41
41
|
|
|
@@ -77,8 +77,8 @@ module ConnectRPC
|
|
|
77
77
|
@read_eof = true
|
|
78
78
|
end
|
|
79
79
|
|
|
80
|
-
def wait(timeout)
|
|
81
|
-
Errors.native(@request, grpc: @grpc) do
|
|
80
|
+
def wait(timeout, write: false)
|
|
81
|
+
Errors.native(@request, grpc: @grpc, write: write) do
|
|
82
82
|
@request.cancellation ? @request.cancellation.check! : @request.deadline&.check!
|
|
83
83
|
remaining = @request.deadline&.remaining
|
|
84
84
|
yield [timeout, remaining].compact.min
|
|
@@ -216,7 +216,11 @@ module ConnectRPC
|
|
|
216
216
|
end
|
|
217
217
|
|
|
218
218
|
# [outcome, nil] or [nil, the exception native close raised]. Native
|
|
219
|
-
# close raises only after teardown published its outcome
|
|
219
|
+
# close raises only after teardown published its outcome, except the
|
|
220
|
+
# `fatal` of a runtime that could not start, before teardown started.
|
|
221
|
+
# That `fatal` passes this rescue (Ruby rescues a native `fatal` only
|
|
222
|
+
# after an ensure) and leaves through the ensure, which records an
|
|
223
|
+
# outcome only if another closer's teardown already published one.
|
|
220
224
|
def await_teardown(driver, grace)
|
|
221
225
|
outcome = nil
|
|
222
226
|
begin
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: connectrpc-hyper
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.3.
|
|
4
|
+
version: 0.3.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- jimeh
|
|
8
8
|
bindir: bin
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date: 2026-11-
|
|
10
|
+
date: 2026-11-28 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
12
|
- !ruby/object:Gem::Dependency
|
|
13
13
|
name: connectrpc
|