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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bfdc69b3ed5eebacbf54541a45714b82fde392e17ec283af9628e9b9ac0ff37a
4
- data.tar.gz: 68318e3a948000e45424ed36c78597fca172d23f8b0d238af500658af2cdbcd9
3
+ metadata.gz: 7410998cb768d5dcf2706c9bbdae448495b8cc08435cec9e54038319a0e9e119
4
+ data.tar.gz: 4f36be6b8c420a0cea57371cf80ba4f3d19db3e838e549e4171421bc23a1f415
5
5
  SHA512:
6
- metadata.gz: 6d3af0b434c30f5511144fd4db022287d8e0692b7d15655353bfb03ef0f21f08ac884d00012484fe5e8668352c6b6763919a130ecf5dcf407a199baf8a9e80be
7
- data.tar.gz: a8c11ad60dfa4e80eebe7284a99b0054702e805f7a74a9d60f0b2f3a2e2afbe7c8ed65cc1d90604f2340f1452f36e8cf8df2bdd98473672d7ab3b243870ac131
6
+ metadata.gz: f39e1152ff0d857039d4a3a1be0986395403ed7c8a320a5c6620283b8a7733be84c2bd0264aa19ed4c314344840575f63349b6c7be3ad232274733485801b574
7
+ data.tar.gz: ccd46956fb21156a636604d853d70f5cc5529f3289708c56a61963b7dcc8a7b563184d2052b283d0b224a33811d2df745ae903e364fe74f546ffac0dc0bae616
data/README.md CHANGED
@@ -198,8 +198,8 @@ an HTTP/1.1 connection ends in an upgrade. The transport detects one once the
198
198
  dispatcher is gone, as Go's `net/http` watches a connection's close while it
199
199
  waits for a response. A Connect unary call treats it as handed back unsent,
200
200
  within the retry above. A streaming call, gRPC unary included, fails with its
201
- connection's failure, since its body cannot be recovered, and a `write`
202
- blocked on that body returns.
201
+ connection's failure, since its body cannot be recovered. A `write` blocked
202
+ on that body returns, and a later `write` raises that failure.
203
203
 
204
204
  Nothing else is replayed, so a handler that started never runs twice. After
205
205
  a GOAWAY, a stream at or below its last stream id is never replayed; a
@@ -266,7 +266,10 @@ connect, TLS, a blocked write and a blocked read. The stream is reset with
266
266
  keeps serving other calls. A token cancels name resolution, connect and TLS,
267
267
  including a wait for the shared dial (which carries on for other callers). The wire timeout headers
268
268
  (`grpc-timeout`, `connect-timeout-ms`) carry the deadline remaining at dispatch,
269
- after connection setup and on a retry.
269
+ after connection setup and on a retry. The one wait that holds the GVL is a
270
+ process's first use waiting for the native runtime to start: normally brief,
271
+ and at most 5 seconds per start attempt when the start fails (see
272
+ [errors](#errors)).
270
273
 
271
274
  ## Fiber schedulers
272
275
 
@@ -297,13 +300,16 @@ ends.
297
300
  ## Fork
298
301
 
299
302
  The tokio runtime has two `hyper-runtime` worker threads plus tokio's blocking
300
- pool, created lazily on the first call in each process. A forked child never
301
- touches its parent's runtime or connections: it builds its own on first use.
302
- The child closes, never shuts down, the inherited connection sockets it can
303
- identify, so the parent's connections keep working and a long-lived child does
304
- not hold them open. A socket still connecting at the moment of fork is not
305
- identified and stays open in the child until it exits. A transport closed
306
- before fork stays closed in the child.
303
+ pool, created lazily on the first call in each process. A short-lived
304
+ `hyper-rt-start` thread builds it and normally exits once that first use has
305
+ its runtime (see [errors](#errors) for a start that never finishes). A forked
306
+ child never touches its parent's runtime or
307
+ connections: it builds its own on first use. The child closes, never shuts
308
+ down, the inherited connection sockets it can identify, so the parent's
309
+ connections keep working and a long-lived child does not hold them open. A
310
+ socket still connecting at the moment of fork is not identified and stays open
311
+ in the child until it exits. A transport closed before fork stays closed in the
312
+ child.
307
313
 
308
314
  ## TLS
309
315
 
@@ -429,12 +435,13 @@ failure as `cause`:
429
435
  | `HTTP transport failed: name resolution failed` | DNS |
430
436
  | `HTTP transport failed: connection refused` | TCP refused |
431
437
  | `HTTP transport failed: host unreachable` / `connection failed` | Other connect errors |
432
- | `HTTP transport failed: timed out` | `connect_timeout` |
438
+ | `HTTP transport failed: timed out` | `connect_timeout`, or a streaming write's wait timing out before the call's deadline. A response wait's own `timeout` raises it with no `cause` |
433
439
  | `HTTP transport failed: TLS verification failed` / `TLS error` | Certificate or handshake failure, or a TLS rejection the server sends after the handshake (such as for a missing client certificate) when the connection that fails the call has read it. A connection whose write fails first reports `connection closed`, as grpc-go reports a write error. A call retried after its first connection read the rejection reports its retry connection's failure |
434
440
  | `HTTP transport failed: ALPN negotiation failed` | Streaming call on an HTTP/1.1 TLS connection |
435
441
  | `HTTP transport failed: connection closed` | The connection closed, retired or received GOAWAY |
436
442
  | `HTTP transport failed: malformed response` | Protocol error |
437
443
  | `HTTP transport failed: stream reset` | A Connect stream reset by the server |
444
+ | `HTTP transport failed: stream closed` | A streaming write after an HTTP/2 stream ended, when the transport cannot tell why, or after its response ended while the upload was open (below) |
438
445
  | `HTTP transport admission is closed` | A call after `close`, or one forced teardown ended after its own setup saw `close`, with no `cause` |
439
446
 
440
447
  A server `RST_STREAM` on a gRPC call maps as connectrpc-async does: codes 3
@@ -442,6 +449,78 @@ and 11 to `resource_exhausted`, 7 to `unavailable`, 8 to `canceled`, 12 to
442
449
  `permission_denied` and anything else to `internal`. Deadlines and cancellation
443
450
  raise core's `deadline_exceeded` and `canceled` errors first.
444
451
 
452
+ A streaming `write` that starts after the server reset the stream, or ended
453
+ its response and the stream, raises instead of discarding the message, as
454
+ connectrpc-async and grpc-go do. The write in flight when the stream ends may
455
+ still return, since its bytes were already queued. The write raises the
456
+ stream's own failure when the response side already knows it: a reset before
457
+ the response head, or after the head with no unread response data, raises the
458
+ reset as mapped above, and an HTTP/1.1 response failure the client already
459
+ received raises that failure. Otherwise the write raises `unavailable` with
460
+ `HTTP transport failed: stream closed` on HTTP/2, and with
461
+ `HTTP transport failed: connection closed` on HTTP/1.1, where the connection
462
+ ending is what usually ends the upload. A core client or bidi stream then stops
463
+ writing and reports the server's status from the response, or raises the
464
+ write's failure when the response carries no error status; a gRPC unary or
465
+ server-streaming call, whose request core writes itself, reports the response's
466
+ outcome, OK included. The response side still reads the head,
467
+ every response byte, and the trailers or the reset.
468
+
469
+ Over HTTP/2, a server that ends its response (END_STREAM) while the upload is
470
+ still open, without resetting the stream, ends the upload too, as grpc-go's
471
+ client does: once the transport sees the response's end, the client resets the
472
+ stream with `RST_STREAM(NO_ERROR)` and drops request bytes it has not sent. A
473
+ write that starts after that, or that still waits to queue its bytes, raises
474
+ `stream closed`; a write that already queued its bytes returns. The head, the
475
+ response bytes and the status stay readable, so core reports the server's
476
+ error status, and the connection stays in use for other calls. A client or bidi
477
+ stream keeps a failed write's `unavailable` over an OK status; a gRPC unary or
478
+ server-streaming call reports the response's outcome, so a server that answered
479
+ OK before it read the whole request still returns its reply, as with grpc-go and
480
+ connect-go. A writer that called
481
+ `close_send` before the transport saw the end sends its queued bytes and
482
+ END_STREAM instead, without a reset. This applies to every protocol; Go's
483
+ net/http client, which connect-go uses, keeps sending a Connect stream's upload
484
+ after a successful response until the caller closes the response. HTTP/1.1 is
485
+ unchanged.
486
+
487
+ A single streaming `write` of 2 GiB or more raises `ArgumentError` before
488
+ anything is queued: h2 refuses a chunk that large, which would end the upload
489
+ while the response waits for it. Core writes at most 16 KiB at a time.
490
+
491
+ hyper does not tell the request body why the stream ended, which limits this:
492
+
493
+ - A gRPC reset behind unread response data, or one that arrives while another
494
+ thread reads the response after its head, makes the write raise
495
+ `stream closed`. Core then reports `unavailable` where connectrpc-async
496
+ reports the reset's mapped code. The reader still receives the reset.
497
+ - Over HTTP/1.1 the write never waits for the response, since hyper also ends
498
+ an upload its own framing finished while the response is pending: a request
499
+ that declares a `Content-Length` once the body reached it (at once for a
500
+ length of 0), and any upload on a reused HTTP/1.0 connection. Such a write
501
+ also raises `connection closed`, although the connection may still be open.
502
+ Core never sends `Content-Length` on a stream.
503
+ - A response head followed by `RST_STREAM(NO_ERROR)` without a valid status
504
+ also raises `stream closed`, where connectrpc-async raises `internal`.
505
+
506
+ hyper reports an END_STREAM that arrives in the response HEADERS (a gRPC
507
+ trailers-only response) at once, but one in DATA or trailers only as the
508
+ response is read, and offers no way to reset a stream while its upload waits
509
+ for send window, which limits the reset:
510
+
511
+ - A write sees an END_STREAM only when no unread response data is ahead of it,
512
+ so an end behind data nobody has read is seen once that data is read. Until
513
+ then writes keep returning, also after the peer's window is spent: hyper's
514
+ upload pipeline (h2's send buffer, the pipe's held chunk and a one-slot
515
+ channel) accepts them, and a write blocks only once that pipeline is full.
516
+ grpc-go resets at once.
517
+ - While the upload waits for send window (a server that stopped reading it),
518
+ hyper does not poll the request body, so the reset goes out only once the
519
+ window opens or the connection ends, also after the call completed. Until
520
+ then the stream keeps its place in the connection's concurrent-stream limit.
521
+ Writes still behave as above: the write that queued returns and the next one
522
+ raises.
523
+
445
524
  `close(grace: 5)` stops admission, lets in-flight calls finish within `grace`,
446
525
  then aborts the rest and waits for every connection task. It returns true when
447
526
  everything drained and false after forced teardown; repeated and concurrent
@@ -468,6 +547,26 @@ completes, and later closers wait for and return the published outcome.
468
547
  Without a fiber scheduler, or on a blocking fiber, a closer defers
469
548
  `Thread#kill` until `close` returns.
470
549
 
550
+ The first close in a process can be the first use of the native runtime. If
551
+ the OS refuses the runtime's first thread then, `close` raises Ruby's `fatal`
552
+ without starting teardown, and the transport stays closing: later calls still
553
+ fail with `HTTP transport admission is closed`, and a later `close` starts
554
+ teardown and returns its outcome. A closer whose helper thread could not start
555
+ either retries once, and if that starts teardown, it raises the `fatal` after
556
+ the outcome is published, as it does with an interrupt. The first use waits
557
+ for the runtime's start for at most 5 seconds, holding the GVL; a start that
558
+ fails or takes longer raises `fatal` the same way. A later use starts the
559
+ runtime afresh, or uses the runtime of a start that finished after its caller
560
+ gave up. A closer whose helper thread could not start, and that retries
561
+ once itself, can wait for the start twice. If the OS refuses only the runtime's
562
+ second worker thread, tokio 1.54 never finishes that start
563
+ ([tokio-rs/tokio#8408](https://github.com/tokio-rs/tokio/pull/8408); the
564
+ unreleased
565
+ [tokio-rs/tokio#8752](https://github.com/tokio-rs/tokio/pull/8752) makes it
566
+ fail instead), so the first close or call raises after the 5 seconds, and
567
+ each stuck start keeps its start thread, first worker thread and I/O driver
568
+ for the life of the process.
569
+
471
570
  ## License
472
571
 
473
572
  MIT. The extension links third-party Rust crates under their own licenses,
@@ -1,11 +1,13 @@
1
1
  //! Request bodies: nothing, a fixed buffer, or an incrementally written
2
2
  //! stream. An aborted stream fails with CANCEL instead of ending, so an
3
3
  //! interrupted upload reaches the peer as RST_STREAM(CANCEL) (HTTP/2) or a
4
- //! broken connection (HTTP/1.1), never as a clean end of the request.
4
+ //! broken connection (HTTP/1.1), never as a clean end of the request. An
5
+ //! HTTP/2 stream whose response ended while the upload was open fails with
6
+ //! NO_ERROR (`Upload`).
5
7
  use std::future::Future;
6
8
  use std::pin::Pin;
7
9
  use std::sync::Arc;
8
- use std::sync::atomic::{AtomicBool, Ordering};
10
+ use std::sync::atomic::{AtomicBool, AtomicU8, Ordering};
9
11
  use std::task::{Context, Poll};
10
12
 
11
13
  use bytes::Bytes;
@@ -17,6 +19,63 @@ use crate::wire::Mark;
17
19
 
18
20
  type AbortWait = Pin<Box<dyn Future<Output = ()> + Send + Sync>>;
19
21
 
22
+ const OPEN: u8 = 0;
23
+ const CLOSED: u8 = 1;
24
+ const RESET: u8 = 2;
25
+
26
+ /// How an HTTP/2 upload ends, shared by a streaming call and its request
27
+ /// bodies. Whichever comes first decides: the writer closing its side
28
+ /// (`close`) commits a clean END_STREAM after the queued bytes; the
29
+ /// transport observing the response's end (`end_response`) while the upload
30
+ /// is open resets the stream with NO_ERROR and drops the unsent bytes, as
31
+ /// grpc-go's client resets a stream it still sends on once the server ended
32
+ /// it. hyper's pipe resets the stream with the reason of the body's error.
33
+ #[derive(Default)]
34
+ pub struct Upload {
35
+ state: AtomicU8,
36
+ response_ended: Signal,
37
+ }
38
+
39
+ impl Upload {
40
+ pub fn new() -> Arc<Self> {
41
+ Arc::new(Self::default())
42
+ }
43
+
44
+ /// The writer closed its side.
45
+ pub fn close(&self) {
46
+ let _ = self
47
+ .state
48
+ .compare_exchange(OPEN, CLOSED, Ordering::SeqCst, Ordering::SeqCst);
49
+ }
50
+
51
+ /// The transport observed the response's end.
52
+ pub fn end_response(&self) {
53
+ let _ = self
54
+ .state
55
+ .compare_exchange(OPEN, RESET, Ordering::SeqCst, Ordering::SeqCst);
56
+ self.response_ended.fire();
57
+ }
58
+
59
+ pub fn response_ended(&self) -> bool {
60
+ self.response_ended.fired()
61
+ }
62
+
63
+ pub async fn wait(&self) {
64
+ self.response_ended.wait().await;
65
+ }
66
+
67
+ fn is_reset(&self) -> bool {
68
+ self.state.load(Ordering::SeqCst) == RESET
69
+ }
70
+ }
71
+
72
+ /// A body's hold on its call's `Upload`, with a wait that wakes hyper's pipe
73
+ /// once the response ended.
74
+ struct UploadEnd {
75
+ upload: Arc<Upload>,
76
+ wait: Option<AbortWait>,
77
+ }
78
+
20
79
  pub struct CallBody {
21
80
  kind: Kind,
22
81
  /// The request's place in its HTTP/2 connection's stream order.
@@ -34,6 +93,8 @@ enum Kind {
34
93
  /// Takes the receiver back when hyper drops a body that never
35
94
  /// yielded a frame, so a replay can carry the writer's bytes.
36
95
  home: Option<oneshot::Sender<mpsc::Receiver<Bytes>>>,
96
+ /// HTTP/2 only.
97
+ upload: Option<UploadEnd>,
37
98
  yielded: bool,
38
99
  },
39
100
  }
@@ -52,12 +113,21 @@ impl CallBody {
52
113
  ended: Arc<AtomicBool>,
53
114
  abort: Arc<Signal>,
54
115
  home: oneshot::Sender<mpsc::Receiver<Bytes>>,
116
+ upload: Option<Arc<Upload>>,
55
117
  ) -> Self {
118
+ let upload = upload.map(|upload| UploadEnd {
119
+ wait: Some(Box::pin({
120
+ let upload = upload.clone();
121
+ async move { upload.wait().await }
122
+ })),
123
+ upload,
124
+ });
56
125
  Self::from(Kind::Stream {
57
126
  rx: Some(rx),
58
127
  ended,
59
128
  abort: Box::pin(async move { abort.wait().await }),
60
129
  home: Some(home),
130
+ upload,
61
131
  yielded: false,
62
132
  })
63
133
  }
@@ -100,6 +170,7 @@ impl Body for CallBody {
100
170
  rx,
101
171
  ended,
102
172
  abort,
173
+ upload,
103
174
  yielded,
104
175
  ..
105
176
  } => {
@@ -108,6 +179,21 @@ impl Body for CallBody {
108
179
  *yielded = true;
109
180
  return Poll::Ready(Some(Err(h2::Error::from(h2::Reason::CANCEL))));
110
181
  }
182
+ // A response that ended the open upload wins over its queued
183
+ // bytes; the reset is never a clean end (`ended`).
184
+ if let Some(end) = upload {
185
+ if end
186
+ .wait
187
+ .as_mut()
188
+ .is_some_and(|wait| wait.as_mut().poll(cx).is_ready())
189
+ {
190
+ end.wait = None;
191
+ }
192
+ if end.upload.is_reset() {
193
+ *yielded = true;
194
+ return Poll::Ready(Some(Err(h2::Error::from(h2::Reason::NO_ERROR))));
195
+ }
196
+ }
111
197
  let rx = rx.as_mut().expect("receiver until drop");
112
198
  let polled = match rx.poll_recv(cx) {
113
199
  Poll::Ready(Some(bytes)) => Poll::Ready(Some(Ok(Frame::data(bytes)))),