Skip to content

Spawn a background task to accept TLS handshakes - #3785

Merged
itowlson merged 3 commits into
spinframework:mainfrom
itowlson:begone-stilton-you-are-taking-up-a-thread-which-i-require-for-other-purposes
Nov 18, 2026
Merged

itowlson merged 3 commits into
spinframework:mainfrom
itowlson:begone-stilton-you-are-taking-up-a-thread-which-i-require-for-other-purposes

Conversation

@itowlson

Copy link
Copy Markdown
Contributor

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.

Comment thread crates/trigger-http/src/server.rs Outdated
let server = self.clone();
let acceptor = acceptor.clone();

tokio::task::spawn(async move {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we spawn here, I think we can remove the spawn inside the server.serve_connection right?

fn serve_connection<S: AsyncRead + AsyncWrite + Unpin + Send + 'static>(
self: Arc<Self>,
stream: S,
server_scheme: Scheme,
client_addr: SocketAddr,
) {
task::spawn(async move {

@fibonacci1729 fibonacci1729 Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread crates/trigger-http/src/server.rs Outdated
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"),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think misconfigurations should be caught earlier than this. At the very least, i'd change to %client_addr to make log search useful.

Comment thread crates/trigger-http/src/server.rs Outdated
};

pub const MAX_RETRIES: u16 = 10;
const TLS_HANDSHAKE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already imported in the prelude.

Suggested change
const TLS_HANDSHAKE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);
const TLS_HANDSHAKE_TIMEOUT: Duration = Duration::from_secs(10);

Comment thread crates/trigger-http/src/server.rs Outdated
let server = self.clone();
let acceptor = acceptor.clone();

tokio::task::spawn(async move {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tokio::task is already imported in the prelude.

Suggested change
tokio::task::spawn(async move {
task::spawn(async move {

@fibonacci1729

Copy link
Copy Markdown
Contributor

I think a good regression test would:

  1. Open a raw TCP connection to HTTPS listener and send nothing.
  2. While that connection stays open, make a normal TLS request and check that is succeeds.

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>
@itowlson
itowlson force-pushed the begone-stilton-you-are-taking-up-a-thread-which-i-require-for-other-purposes branch from df58426 to e24d00e Compare November 17, 2026 20:40
Signed-off-by: itowlson <ivan.towlson@fermyon.com>
Comment thread crates/trigger-http/src/server.rs Outdated
@@ -302,12 +303,21 @@ impl<F: RuntimeFactors> HttpServer<F> {
let acceptor = tls_config.server_config()?;
loop {
let (stream, client_addr) = listener.accept().await?;

@lann lann Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@itowlson
itowlson requested a review from lann November 18, 2026 19:25
@itowlson
itowlson merged commit beeb80a into spinframework:main Nov 18, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants