Skip to content

Commit e1752bc

Browse files
fix: don't crash the derper (#1110)
* add more debugging to TLS acceptor * fix(derp) client conn: ensure flushing on shutdown * `ClientConn` task should close on EOF --------- Co-authored-by: Kasey <klhuizinga@gmail.com>
1 parent 23baf7d commit e1752bc

2 files changed

Lines changed: 37 additions & 26 deletions

File tree

src/hp/derp/client_conn.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,16 @@ where
294294
loop {
295295
trace!("tick");
296296
tokio::select! {
297+
biased;
298+
297299
_ = done.cancelled() => {
298300
trace!("cancelled");
301+
// final flush
302+
self.io.flush().await?;
299303
return Ok(());
300304
}
301305
read_res = read_frame(&mut self.io, MAX_FRAME_SIZE, &mut read_buf) => {
306+
trace!("handle read");
302307
self.handle_read(read_res, &mut read_buf).await?;
303308
}
304309
peer = self.peer_gone.recv() => {
@@ -496,14 +501,7 @@ where
496501
}
497502
Ok(())
498503
}
499-
Err(err) => {
500-
if let Some(io_err) = err.downcast_ref::<std::io::Error>() {
501-
if io_err.kind() == std::io::ErrorKind::UnexpectedEof {
502-
return Ok(());
503-
}
504-
}
505-
Err(err)
506-
}
504+
Err(err) => Err(err),
507505
}
508506
}
509507

src/hp/derp/http/server.rs

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -365,21 +365,26 @@ where
365365
// Note: This can't possibly be fulfilled until the 101 response
366366
// is returned below, so it's better to spawn this future instead
367367
// waiting for it to complete to then return a response.
368-
tokio::task::spawn(async move {
369-
match hyper::upgrade::on(&mut req).await {
370-
Ok(upgraded) => {
371-
if let Err(e) =
372-
derp_connection_handler(&closure_conn_handler, upgraded).await
373-
{
374-
tracing::warn!(
375-
"server \"{HTTP_UPGRADE_PROTOCOL}\" io error: {:?}",
376-
e
377-
)
378-
};
368+
tokio::task::spawn(
369+
async move {
370+
match hyper::upgrade::on(&mut req).await {
371+
Ok(upgraded) => {
372+
if let Err(e) =
373+
derp_connection_handler(&closure_conn_handler, upgraded).await
374+
{
375+
tracing::warn!(
376+
"server \"{HTTP_UPGRADE_PROTOCOL}\" io error: {:?}",
377+
e
378+
);
379+
} else {
380+
tracing::info!("server \"{HTTP_UPGRADE_PROTOCOL}\" success");
381+
};
382+
}
383+
Err(e) => tracing::warn!("upgrade error: {:?}", e),
379384
}
380-
Err(e) => tracing::warn!("upgrade error: {:?}", e),
381385
}
382-
});
386+
.instrument(tracing::debug_span!("derp_connection_handler")),
387+
);
383388

384389
// Now return a 101 Response saying we agree to the upgrade to the
385390
// HTTP_UPGRADE_PROTOCOL
@@ -514,6 +519,7 @@ impl DerpService {
514519
match tls_config {
515520
Some(tls_config) => self.tls_serve_connection(stream, tls_config).await,
516521
None => {
522+
debug!("HTTP: serve connection");
517523
self.serve_connection(MaybeTlsStreamServer::Plain(stream))
518524
.await
519525
}
@@ -526,18 +532,25 @@ impl DerpService {
526532
match acceptor {
527533
TlsAcceptor::LetsEncrypt(a) => match a.accept(stream).await? {
528534
None => {
529-
info!("received TLS-ALPN-01 validation request");
535+
info!("TLS[acme]: received TLS-ALPN-01 validation request");
530536
}
531537
Some(start_handshake) => {
532-
let tls_stream = start_handshake.into_stream(config).await?;
538+
debug!("TLS[acme]: start handshake");
539+
let tls_stream = start_handshake
540+
.into_stream(config)
541+
.await
542+
.context("TLS[acme] handshake")?;
533543
self.serve_connection(MaybeTlsStreamServer::Tls(tls_stream))
534-
.await?;
544+
.await
545+
.context("TLS[acme] serve connection")?;
535546
}
536547
},
537548
TlsAcceptor::Manual(a) => {
538-
let tls_stream = a.accept(stream).await?;
549+
debug!("TLS[manual]: accept");
550+
let tls_stream = a.accept(stream).await.context("TLS[manual] accept")?;
539551
self.serve_connection(MaybeTlsStreamServer::Tls(tls_stream))
540-
.await?;
552+
.await
553+
.context("TLS[manual] serve connection")?;
541554
}
542555
}
543556
Ok(())

0 commit comments

Comments
 (0)