SMTP: Tokio worker stuck in a busy loop after EHLO when two connections with the same EHLO name arrive concurrently (v0.16.27) - server stops responding once all workers are stuck

Issue Description

Twice within ~15 hours, Stalwart v0.16.27 got a Tokio worker thread stuck at 100 % CPU right after two simultaneous inbound SMTP connections from the same client with the same EHLO name. The stuck worker never recovers. Once every worker is stuck (this host has 2 vCPUs → 2 workers), the whole server stops responding – SMTP, scheduled tasks and logging – until restarted. It looks like a race: the same client opened such concurrent pairs 573 times between 2026-10-16 and 2026-10-23 and only 2 of them triggered it (~1 in 290).

Expected Behavior

Two concurrent SMTP sessions from the same client with the same EHLO name are handled independently: each gets its SPF EHLO check result (smtp.spf-ehlo-fail / spf.none for this domain), proceeds to STARTTLS and finishes. This is what happens in 571 of 573 cases:

2026-10-23T07:32:37Z INFO SMTP EHLO command (smtp.ehlo) listenerId = "smtp", localPort = 25, remoteIp = 161.35.22.X, remotePort = 43304, domain = "data.X.cz"
2026-10-23T07:32:37Z INFO SMTP EHLO command (smtp.ehlo) listenerId = "smtp", localPort = 25, remoteIp = 161.35.22.X, remotePort = 43316, domain = "data.X.cz"
2026-10-23T07:32:37Z INFO SPF EHLO check failed (smtp.spf-ehlo-fail) listenerId = "smtp", localPort = 25, remoteIp = 161.35.22.X, remotePort = 43316, domain = "data.X.cz", result = No SPF record (spf.none), elapsed = 0ms
2026-10-23T07:32:37Z INFO SPF EHLO check failed (smtp.spf-ehlo-fail) listenerId = "smtp", localPort = 25, remoteIp = 161.35.22.X, remotePort = 43304, domain = "data.X.cz", result = No SPF record (spf.none), elapsed = 5ms
2026-10-23T07:32:37Z INFO TLS handshake (tls.handshake) listenerId = "smtp", localPort = 25, remoteIp = 161.35.22.X, remotePort = 43316, version = "TLSv1_3", details = "TLS13_AES_256_GCM_SHA384"

Even if one session runs into a problem, it should time out or fail without blocking a runtime worker and without affecting other sessions.

Actual Behavior

Occasionally (2 of 573 concurrent pairs) both sessions stop right after smtp.ehlo – no SPF result, no TLS handshake – and one tokio-runtime-worker thread goes to ~100 % CPU permanently.

  • Incident 1 (2026-10-22): first worker stuck at 17:24:37Z; the server kept working on the second worker until the next probe pair at ~17:40:37Z, after which nothing was logged at all. The server then accepted TCP (via the proxy) but never sent the 220 banner (tested from two different networks, waited up to 190 s); the 5-minute calculateMetrics task stopped too. top -H showed both worker threads at ~96 % CPU each, main thread idle, load average 2.0, RSS ~128 MB, no kernel/OOM messages. This lasted 12.5 hours until a manual restart.
  • Incident 2 (2026-10-23), ~1.5 h after that restart: first worker stuck at 07:48:37Z; caught at 07:55Z with one worker at 95 % and the other idle (server still answering). Restarted before the next probe pair.

Reproduction Steps

The hang is probabilistic (~1 in 290 attempts here), so these steps need to be repeated:

  1. Use a client host whose EHLO name has no SPF record (A record + matching PTR, no TXT/SPF, DNSSEC-signed zone).
  2. Open two TCP connections to the Stalwart SMTP listener on port 25 at the same instant (in our setup they pass through a PROXY-protocol-v2).
  3. On both, wait for the banner and send EHLO at the same time.
  4. On both, send STARTTLS and do a TLS handshake (one RSA-only, one ECDSA-only signature algorithms - probably irrelevant, as the hang happens before the SPF result is logged), then QUIT.
  5. Repeat every few seconds and watch top -H -p <stalwart pid> for a tokio-runtime-worker thread that stays at 100 %, and the log for an EHLO pair without the following smtp.spf-ehlo-* lines.

Opening the two connections one after the other instead of concurrently avoids the problem.

Relevant Log Output

Incident 1 – complete log around the event (nothing omitted):

2026-10-22T17:24:37Z INFO SMTP EHLO command (smtp.ehlo) listenerId = "smtp", localPort = 25, remoteIp = 161.35.22.X, remotePort = 42878, domain = "data.X.cz"
2026-10-22T17:24:37Z INFO SMTP EHLO command (smtp.ehlo) listenerId = "smtp", localPort = 25, remoteIp = 161.35.22.X, remotePort = 42872, domain = "data.X.cz"
2026-10-22T17:25:17Z INFO Task scheduled for future execution (task-manager.task-scheduled) due = 2026-10-22T17:30:16Z, id = "calculateMetrics"
2026-10-22T17:25:17Z INFO Metrics collected (telemetry.metrics-collected) elapsed = 3ms
[... normal operation on the remaining worker ...]
2026-10-22T17:39:22Z INFO Blocked IP address (security.ip-blocked) listenerId = "submissions", localPort = 465, remoteIp = 62.60.134.142, remotePort = 32702
2026-10-22T17:40:17Z INFO Task scheduled for future execution (task-manager.task-scheduled) due = 2026-10-22T17:45:16Z, id = "calculateMetrics"
2026-10-22T17:40:17Z INFO Metrics collected (telemetry.metrics-collected) elapsed = 2ms
[no further log output until the restart at 2026-10-23T06:19Z; next probe pair was due at 17:40:37Z]

Incident 2:

2026-10-23T07:48:37Z INFO SMTP EHLO command (smtp.ehlo) listenerId = "smtp", localPort = 25, remoteIp = 161.35.22.X, remotePort = 39342, domain = "data.X.cz"
2026-10-23T07:48:37Z INFO SMTP EHLO command (smtp.ehlo) listenerId = "smtp", localPort = 25, remoteIp = 161.35.22.X, remotePort = 39358, domain = "data.X.cz"
2026-10-23T07:48:38Z INFO Blocked IP address (security.ip-blocked) listenerId = "submissions", localPort = 465, remoteIp = 62.60.134.142, remotePort = 61254
2026-10-23T07:49:09Z INFO Task scheduled for future execution (task-manager.task-scheduled) due = 2026-10-23T07:54:08Z, id = "calculateMetrics"
2026-10-23T07:49:09Z INFO Metrics collected (telemetry.metrics-collected) elapsed = 2ms

Trace-level logs are not available for these incidents (log level was INFO); I can enable trace and report back if it reoccurs.

Stalwart Version

v0.16.x

Installation Method

Docker

Database Backend

PostgreSQL

Blob Storage

S3-compatible

Search Engine

PostgreSQL

Directory Backend

SQL

Additional Context

  • Stalwart v0.16.27, image ghcr.io/stalwartlabs/stalwart:v0.16 (digest sha256:c429b311c93db5606598abc1c68225a85a6953183848ae01bee9c6a98885836c), aarch64, 2 vCPU, 4 GB RAM (Hetzner)
  • Podman 5.4.2 (Quadlet), rootful; container in a podman network
  • Data store: PostgreSQL (external)
  • DNS: container /etc/resolv.conf → podman aardvark-dns (172.20.0.1)
  • SMTP arrives through a TCP proxy that adds PROXY protocol v2
  • Log level INFO; no recent configuration changes; image unchanged since 2026-10-19

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

Thanks for the very detailed repro, this looks like a real concurrency bug rather than a misconfiguration, so it needs engineering follow up rather than a config fix.

What the source confirms so far: the EHLO handler’s SPF check (crates/smtp/src/inbound/ehlo.rs) calls verify_spf with DNS lookups routed through a shared, process-wide TTL cache (CacheWithTtl in crates/utils/src/cache.rs, built on quick_cache::sync::Cache). Lookups use get_value_or_guard_async, which for a cache miss hands out a “placeholder guard” to the first caller for a given key (e.g. the SPF TXT record for a given domain/IP) and makes any other concurrent lookup for that same key wait on that guard until it resolves or is dropped. Your reproduction (two simultaneous connections, same EHLO domain, same remote IP) is exactly the scenario that produces two concurrent lookups on the same cache key, which is consistent with a race in that guard-resolution path (e.g., a guard never being fulfilled/dropped, or a wakeup being lost) leading to one task spinning. The actual SPF record walk and the quick_cache crate itself live outside this repository, so I can’t point you to the exact faulting line, but the shared-cache-key-on-concurrent-EHLO theory matches your logs (stall occurs right after smtp.ehlo, before any spf.result is emitted, and only when the same domain is used in parallel).

This is not something documented or addressed anywhere I can find; it reads as a genuine bug in the concurrent DNS/SPF cache lookup path. A staff member will need to look at the quick_cache guard handling and the SPF/DNS cache in crates/common/src/config/inner.rs / crates/utils/src/cache.rs to confirm and fix it. If you’re able to capture a trace-level log or a stack trace of the stuck worker next time it happens, that would help narrow it down further.

This is an automated reply from the Stalwart Help Bot. Other community members may follow up if this answer is incomplete or wrong.

Going by the log order, both sessions stop inside the TXT lookup for the EHLO SPF check. I went through the resolver code on that path and didn’t find anything that can spin without yielding, so I’d like to narrow down where the loop is before guessing further.

Can you bypass aardvark-dns by pointing Stalwart’s resolver straight at an upstream server (DNS Resolver | Stalwart) and see whether it still happens over a few thousand connections? Also tell me which aardvark-dns version the Podman host runs. If it stops, that already works around it, and it tells me the trigger is in how the resolver handles that server’s replies.

As a second test, setting spfEhloVerify to disable (SPF | Stalwart) skips that particular lookup. MAIL FROM does its own TXT lookup, so it won’t fully take DNS out of the picture, but it shows whether the concurrent EHLO lookups are the trigger.

Follow-up with results and one correction.

Version. The server is Stalwart v0.16.27 (image Package stalwart · GitHub , revision aa47d8ef7bfba7200d3293198ac2592747b20e73), which is the latest release — so this is in current code, nothing has superseded it.

Correction about DNS / aardvark-dns. My original report noted the container’s /etc/resolv.conf points at podman’s aardvark-dns. That is true for the container’s libc resolver, but Stalwart’s own resolver is configured to Quad9 (DNS resolver = Quad9), so the EHLO/SPF TXT lookups in question go straight to Quad9 over the internet — aardvark-dns is not in that path. The aardvark-dns timeouts I mentioned came from a different container and are unrelated. So “which aardvark version” and “bypass aardvark” don’t apply here; the resolver during the incidents was Quad9.

Reproduction attempt (negative so far). I wrote a minimal standalone tool that mirrors the monitoring probe: it opens two connections at the same instant, sends the same EHLO (a domain with no SPF record), then STARTTLS, in a loop. I ran well over a two thousand such concurrent pairs, both against the normal Quad9 resolver and against a deliberately slowed DNS forwarder to widen any race window. Observations:

  • The two concurrent identical EHLO lookups are deduplicated into a single DNS query — both connections return at the same instant (identical latency), so the in-flight dedup path is definitely being exercised.
  • No hang. The concurrent EHLO pair alone, with DNS answering normally (even if slowly), does not trigger it.

Interpretation. The concurrent pair seems necessary but not sufficient. The original incidents clustered — two consecutive probe pairs 16 min apart both hung, then another the next morning, after days with none — which points to a transient gating condition
rather than a fixed per-pair probability. My leading guess is that the spin is in the DNS error/timeout path of the concurrent lookup (the spf.temp-error branch), not the clean-answer path I exercised; during the incident window Quad9 may have been transiently slow or failing for that name.

Next time it happens I’ll capture /proc//maps together with eu-stack (so the addresses symbolize) and switch that connection to trace logging. Happy to share the reproducer if useful.

Good to know the lookups go straight to Quad9, that takes aardvark out of the picture.

The deduplication you saw happens inside hickory, the DNS library Stalwart uses: when two identical queries are in flight, the second one waits on the first one’s lookup and doesn’t send its own. That would also explain why both sessions go quiet at the same moment. They’re waiting on a single lookup, and if the worker driving it gets stuck, the other session waits along with it. So the concurrent pair is probably what makes the hang show up on both connections, not necessarily what causes the spin. I went through that lookup path in hickory 0.26.3 again, including the retry and timeout branches you suspect, and every loop there either waits on the network or stops at the timeout, so I can’t point to a line yet.

The stack capture you’re planning is what will settle it. Take two or three eu-stack snapshots of the busy thread a second or so apart, so we can see which frames repeat. 1.0 came out yesterday, but it ships the same hickory version, so upgrading on its own won’t change anything here.