Connection closed after a 401 without `Connection: close`

Issue Description

While testing my libraries calendaring-sync and calendaring-jmap against Stalwart, I found that Stalwart sometimes closes the connection right after sending a 401, and the 401 has no Connection: close header. A client that reuses the connection for its next request (for example, to retry with credentials) then fails with “Remote end closed connection without response”.

It only happens on requests with a body, when the body arrives a moment after the headers. My guess is the 401 goes out before the body is read, and then the connection gets dropped. I first saw it on 0.16.27 and it’s the same on 1.0.0.

Expected Behavior

Either Stalwart reads the body and keeps the connection open, or the 401 carries Connection: close when the connection is about to be closed. RFC 9112 section 9.6 says a server should send Connection: close when it’s going to close the connection.

Actual Behavior

The connection is closed after the 401 with no Connection: close. On 1.0.0, tested with a raw socket against /dav/cal/:

  • headers and body in one write: never closed
  • two writes back to back: 2 closed out of 40
  • a 1 ms gap before the body: 20 out of 40 closed
  • a 50 ms gap: closed every time
  • DELETE with no body: never closed

Reproduction Steps

  1. Start Stalwart in Docker (stalwartlabs/stalwart:v1.0.0). A fresh container is enough, since /dav/cal/ answers 401 without a login.
  2. Run this script against it:
import socket, time

body = b'<?xml version="1.0"?><propfind xmlns="DAV:"><prop><resourcetype/></prop></propfind>'
head = (b"PROPFIND /dav/cal/ HTTP/1.1\r\nHost: localhost\r\nDepth: 0\r\n"
        b"Content-Type: application/xml\r\nContent-Length: %d\r\n\r\n" % len(body))

def closed_after_401(gap):
    s = socket.create_connection(("127.0.0.1", 8080))
    s.sendall(head)
    time.sleep(gap)
    s.sendall(body)
    s.settimeout(2)
    reply = s.recv(4096)
    assert reply.startswith(b"HTTP/1.1 401")
    assert b"connection: close" not in reply.lower()
    time.sleep(0.2)
    s.settimeout(0.5)
    try:
        return s.recv(4096) == b""
    except socket.timeout:
        return False

for gap in (0, 0.001, 0.05):
    print(gap, sum(closed_after_401(gap) for _ in range(50)), "of 50 closed")
  1. The longer the gap, the more connections are closed.

Relevant Log Output

From 0.16.27 at debug level, the closed connection:

DEBUG HTTP connection started (http.connection-start) listenerId = "http", localPort = 8080, remoteIp = 172.19.0.1, remotePort = 54480
DEBUG HTTP request URL (http.request-url) listenerId = "http", localPort = 8080, remoteIp = 172.19.0.1, remotePort = 54480, url = "/dav/cal/testuser%40example.org/calendaring-sync-created-and-removed-async/"
DEBUG Authentication failed (auth.failed) listenerId = "http", localPort = 8080, remoteIp = 172.19.0.1, remotePort = 54480, details = "Missing Authorization header.", causedBy = "crates/http/src/auth/authenticate.rs:117"
DEBUG HTTP connection ended (http.connection-end) listenerId = "http", localPort = 8080, remoteIp = 172.19.0.1, remotePort = 54480, elapsed = 1ms

Stalwart Version

v1.x

Installation Method

Docker

Database Backend

SQLite

Blob Storage

SQLite

Search Engine

Internal

Directory Backend

Internal

Additional Context

Seen on 0.16.27 with SQLite and on a fresh 1.0.0 container. It happens before authentication, so the store shouldn’t matter.

Clients that send headers and body as separate writes and first try without credentials (python-caldav does this) hit it a few times in every few hundred connections. I’ve worked around it in my tests by sending the login with the first request.

I have reviewed the documentation and FAQ and confirm that my issue is NOT addressed there.

on

I have searched this support forum (open and closed topics) and confirm this is not a duplicate.

on

I understand that topics in this category are triaged by a bot first but a human reply will follow up. If I’d prefer a human-only reply, I’ll add the no-ai tag to my topic.

on

Hi Sashank,

When a request arrives with a body, Stalwart can send the 401 (or any other early error) before it has read that body. hyper, the HTTP library underneath, then reads whatever part of the body is already in its buffer. If the rest is still on the wire, it gives up on the connection and closes it. By then the response headers have already gone out, which is why the 401 had no Connection: close and why the outcome depended on the gap between your two writes.

The fix makes Stalwart add Connection: close to any non-2xx response sent for a request that carried a body. Your client will see the header, open a fresh connection for the authenticated retry, and won’t hit “Remote end closed connection without response” again. Requests without a body, such as your DELETE test, keep the connection open as before. I also added a regression test that replays your 50 ms gap script.

It ships in the next weekly Stalwart release.