Repository navigation
Spawn a background task to accept TLS handshakes - #3785
Conversation
| let server = self.clone(); | ||
| let acceptor = acceptor.clone(); | ||
|
|
||
| tokio::task::spawn(async move { |
There was a problem hiding this comment.
Since we spawn here, I think we can remove the spawn inside the server.serve_connection right?
spin/crates/trigger-http/src/server.rs
Lines 558 to 564 in d85283b
There was a problem hiding this comment.
No we can't just remove it because serve_http also calls it. What we could do though is make serve_connection async and spawn at each call site. The value is nominal though.
| tokio::task::spawn(async move { | ||
| match tokio::time::timeout(TLS_HANDSHAKE_TIMEOUT, acceptor.accept(stream)).await { | ||
| Ok(Ok(stream)) => server.serve_connection(stream, Scheme::HTTPS, client_addr), | ||
| Ok(Err(err)) => tracing::error!(?err, "Failed to start TLS session"), |
There was a problem hiding this comment.
Maybe these tracing::error!s should be warn!. Reason being, clients cause handshake failures and timeouts, not the server. Logging them at error! lets anyone fill the logs with errors by opening sockets and sending nothing.
Additionally, ?terr only prints Elapsed(()) which tells us nothing. We should use %client_addr in both branches. I.e.:
Err(_) => tracing::warn!(%client_addr, "TLS handshake timed out"),
There was a problem hiding this comment.
I agree the timeout one should be warn. The "failed to TLS" one was already an error, and I guess could maybe indicate a misconfiguration that you'd want to know about? So I'm inclined to leave that one as is. Open to being persuaded otherwise of course.
There was a problem hiding this comment.
I think misconfigurations should be caught earlier than this. At the very least, i'd change to %client_addr to make log search useful.
| }; | ||
|
|
||
| pub const MAX_RETRIES: u16 = 10; | ||
| const TLS_HANDSHAKE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); |
There was a problem hiding this comment.
Already imported in the prelude.
| const TLS_HANDSHAKE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); | |
| const TLS_HANDSHAKE_TIMEOUT: Duration = Duration::from_secs(10); |
| let server = self.clone(); | ||
| let acceptor = acceptor.clone(); | ||
|
|
||
| tokio::task::spawn(async move { |
There was a problem hiding this comment.
tokio::task is already imported in the prelude.
| tokio::task::spawn(async move { | |
| task::spawn(async move { |
|
I think a good regression test would:
Before this PR, step 2 hangs. After it, the request succeeds. Optionally, we could check that the idle socket is closed after the timeout. |
Signed-off-by: itowlson <ivan.towlson@fermyon.com>
df58426 to
e24d00e
Compare
Signed-off-by: itowlson <ivan.towlson@fermyon.com>
| @@ -302,12 +303,21 @@ impl<F: RuntimeFactors> HttpServer<F> { | |||
| let acceptor = tls_config.server_config()?; | |||
| loop { | |||
| let (stream, client_addr) = listener.accept().await?; | |||
There was a problem hiding this comment.
Though less common, this accept can fail for individual connections too. I believe the fix here would be to make std::io::Error::kind -> ErrorKind::ConnectionReset (and maybe ConnectionAborted?) non-fatal.
There was a problem hiding this comment.
Okay I have made both of those non-fatal (loop back and try to accept again). I think I am dependent on you or other networking gurus for confirmation of the exact set that should be continuable.
Signed-off-by: itowlson <ivan.towlson@fermyon.com>
I am not sure what to test for this - I have verified that I can still make TLS requests to the thing, but I dunno if there are error cases or load concerns that make this a bad solution. @lann I'd value your eyes on this please!
I got the proposed handshake timeout of 10 seconds by the extremely scientific art of googling "what's a typical TLS timeout" so yeah you folks may have opinions on that too.