Mythic Beasts DNS provider: DnsManagement task fails with HTTP 429 and 400 invalid_request due to concurrent token requests

Issue Description

Automatic DNS management with the Mythic Beasts provider fails when publishing the full record set for a domain. Most record updates fail with HTTP 429 “Rate limited” and the first few fail with HTTP 400 {“error”: “invalid_request”}. The API key itself is valid and works correctly when tested manually from the same host.

The likely cause is a race in token acquisition in src/providers/mythicbeasts.rs (dns-update). ensure_token() checks the cached token under the mutex, releases the lock, requests a new token from https://auth.mythic-beasts.com/login and only then stores it. When the DnsManagement task updates many record sets concurrently on a cold cache, every call misses the cache and performs its own login at the same moment. Around 18 simultaneous logins appear to trip Mythic Beasts’ rate limiter. The 400 invalid_request responses are in the OAuth2 error format returned by the auth service and appear to come from the same burst, as the credentials are valid. resolve_zone() follows the same check-then-fetch pattern and may add to the burst.

Expected Behavior

All records in the domain’s publishRecords set are published to the Mythic Beasts zone and the DnsManagement task completes successfully.

Actual Behavior

The DnsManagement task fails. Of the 18 record sets attempted, 3 fail with HTTP 400 {“error”: “invalid_request”} and 15 fail with HTTP 429 {“message”: “Rate limited”}. No records are published.

Reproduction Steps

  1. Create a DnsServer of type MythicBeasts with a valid DNS API v2 key ID and secret.
  2. Create a Domain with dnsManagement set to Automatic, dnsServerId pointing to that DnsServer and the default publishRecords set.
  3. Allow the initial DnsManagement task to run.
  4. Check Management › Tasks › Failed or the log for the task result.

Relevant Log Output

Note - domain anonymised.

Level: Warning
Event: Task failed during processing
id = 334180496651561472, details = “DnsManagement”, reason = “Failed to set DNS RRSet for _caldavs._tcp.example.com./SRV: Failed to set DNS RRSet: API error: BadRequest {“error”: “invalid_request”}; Failed to set DNS RRSet for _jmap._tcp.example.com./SRV: Failed to set DNS RRSet: API error: BadRequest {“error”: “invalid_request”}; Failed to set DNS RRSet for autodiscover.example.com./CNAME: Failed to set DNS RRSet: API error: BadRequest {“error”: “invalid_request”}; Failed to set DNS RRSet for autoconfig.example.com./CNAME: Failed to set DNS RRSet: API error: HTTP 429: {“message”: “Rate limited”}; Failed to set DNS RRSet for example.com./MX: Failed to set DNS RRSet: API error: HTTP 429: {“message”: “Rate limited”}; Failed to set DNS RRSet for _submissions._tcp.example.com./SRV: Failed to set DNS RRSet: API error: HTTP 429: {“message”: “Rate limited”}; Failed to set DNS RRSet for _ua-auto-config.example.com./TXT: Failed to set DNS RRSet: API error: HTTP 429: {“message”: “Rate limited”}; Failed to set DNS RRSet for _mta-sts.example.com./TXT: Failed to set DNS RRSet: API error: HTTP 429: {“message”: “Rate limited”}; Failed to set DNS RRSet for _imaps._tcp.example.com./SRV: Failed to set DNS RRSet: API error: HTTP 429: {“message”: “Rate limited”}; Failed to set DNS RRSet for v1-rsa-20261004._domainkey.example.com./TXT: Failed to set DNS RRSet: API error: HTTP 429: {“message”: “Rate limited”}; Failed to set DNS RRSet for _pop3s._tcp.example.com./SRV: Failed to set DNS RRSet: API error: HTTP 429: {“message”: “Rate limited”}; Failed to set DNS RRSet for _carddavs._tcp.example.com./SRV: Failed to set DNS RRSet: API error: HTTP 429: {“message”: “Rate limited”}; Failed to set DNS RRSet for example.com./CAA: Failed to set DNS RRSet: API error: HTTP 429: {“message”: “Rate limited”}; Failed to set DNS RRSet for mta-sts.example.com./CNAME: Failed to set DNS RRSet: API error: HTTP 429: {“message”: “Rate limited”}; Failed to set DNS RRSet for ua-auto-config.example.com./CNAME: Failed to set DNS RRSet: API error: HTTP 429: {“message”: “Rate limited”}; Failed to set DNS RRSet for v1-ed25519-20261004._domainkey.example.com./TXT: Failed to set DNS RRSet: API error: HTTP 429: {“message”: “Rate limited”}; Failed to set DNS RRSet for _validation-persist.example.com./TXT: Failed to set DNS RRSet: API error: HTTP 429: {“message”: “Rate limited”}; Failed to set DNS RRSet for example.com./TXT: Failed to set DNS RRSet: API error: HTTP 429: {“message”: “Rate limited”}”

Stalwart Version

v0.16.x

Installation Method

Binary (Linux)

Database Backend

RocksDB

Blob Storage

Filesystem

Search Engine

Internal

Directory Backend

Internal

Additional Context

Credentials verified from the Stalwart host:

References:

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 detailed report. Looking at the Stalwart source for the DnsManagement task (crates/services/src/task_manager/dns.rs), record sets are published sequentially, not concurrently: a single DnsUpdater instance is built once for the task, then RRSets are updated in a plain for loop with each set_rrset awaited before the next starts. So within Stalwart itself there is no fan-out of 18 simultaneous requests for a single DnsManagement run.

let mut errors = String::new();
for ((name, record_type), mut recs) in by_owner {
    ...
    if let Err(err) = dns_updater.set_rrset(origin, &name, record_type, recs).await {
        ...
    }
}

The token caching/acquisition logic you’re pointing to (ensure_token/resolve_zone in src/providers/mythicbeasts.rs) lives in the external dns-update crate (version 0.5) that Stalwart depends on, not in Stalwart’s own codebase, so I can’t confirm from the docs or Stalwart source whether that crate issues parallel login requests per RRSet or has the check-then-fetch race you describe. That would need to be verified/fixed upstream in dns-update itself.

This isn’t covered in the current docs. A community member or staff member will follow up.

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

I don’t think the token race is what’s happening. The DnsManagement task updates records one at a time, each call waiting for the previous one, and after the first login the token is cached, so there’s no burst of concurrent logins. It does make two API calls per record (a list, then a set) back to back, roughly 36 requests for the default record set, and it doesn’t back off when the API returns 429. That fits the rate limiting better.

The 400s are a separate question. They could come from the auth endpoint, or from the records API rejecting the payload for those SRV and CNAME records, and the error body alone doesn’t say which. Could you run the task again with the log level at trace and post the lines around one of the failing SRV records? They include the URL that returned the 400. Also, did more than one domain’s task run at the same time?

Hi,

Thank you for your reply, and for the correction on concurrency.

  • Only one DnsManagement task was running during the trace run (the log shows task-acquired total = 1).
  • The trace log doesn’t include the outbound requests made by the DNS provider, so I couldn’t get the URL that returned the 400. One line does look relevant, though:
DEBUG DNS record lookup failed (dns.record-lookup-failed) hostname = "example.com.", details = "example.com", type = "TXT", reason = "DNS provider failed to list RRSet: API error: BadRequest {"error": "invalid_request"}"

That’s the list call, which sends no record payload, so it seems unlikely that the 400 comes from the records API rejecting the SRV/CNAME data.

The {“error”: “invalid_request”} means the token request itself is malformed. Bad credentials would normally produce invalid_client instead, which is also the example given in Mythic Beasts’ auth documentation. The same API key returned a token when I requested one manually with curl, so the credentials looked fine. That pointed towards how the provider builds the request rather than what it sends.

Reading ensure_token() in dns-update, the body appears to be grant_type=client_credentials as expected, but the HTTP client it uses looks like it adds a default Content-Type: application/json header. If the auth service parsed the body as JSON, it wouldn’t find grant_type, which would match invalid_request. The tests below were designed to check that.

Further testing

I reproduced the provider’s requests manually with curl from the Stalwart host. The server is running 0.16.25, whose Cargo.lock lists dns-update 0.5.9 and reqwest 0.13.5.

Test Request Result
1 Token request, Content-Type: application/x-www-form-urlencoded only 200, token issued
2 Token request, Content-Type: application/json only 400 {"error": "invalid_request"}
3 Token request, both headers (JSON first, then form) 400 {"error": "invalid_request"}
4 Token request, both headers (form first, then JSON) 400 {"error": "invalid_request"}
5 GET /dns/v2/zones/example.com/records/@/TXT with a valid token 200
6 PUT, GET and DELETE of a test TXT record with a valid token 200 for each

In tests 3 and 4, curl’s verbose output confirmed that both Content-Type headers were actually sent.

Commands used (credentials are read interactively and passed to curl on stdin, so they don’t appear on the command line):

read -r -p 'Key ID: ' KEY_ID
read -r -s -p 'Secret: ' SECRET; echo
OUT="$(mktemp)"
CASES=("application/x-www-form-urlencoded" "application/json" "application/json application/x-www-form-urlencoded" "application/x-www-form-urlencoded application/json")
for CASE in "${CASES[@]}"; do
  HDRS=(); for CT in $CASE; do HDRS+=(-H "Content-Type: $CT"); done
  echo "=== $CASE ==="
  printf 'user = "%s:%s"\n' "$KEY_ID" "$SECRET" | curl -s -K - -H 'Accept: application/json' "${HDRS[@]}" --data 'grant_type=client_credentials' -o "$OUT" -w 'HTTP %{http_code}\n' https://auth.mythic-beasts.com/login
  grep -q '"access_token"' "$OUT" && echo "Token received" || { cat "$OUT"; echo; }
done
rm -f "$OUT"
TOKEN="$(printf 'user = "%s:%s"\n' "$KEY_ID" "$SECRET" | curl -s -K - -H 'Content-Type: application/x-www-form-urlencoded' --data 'grant_type=client_credentials' https://auth.mythic-beasts.com/login | grep -o '"access_token" *: *"[^"]*"' | sed 's/.*"\([^"]*\)"$/\1/')"
curl -s -H "Authorization: Bearer $TOKEN" -w '\nHTTP %{http_code}\n' https://api.mythic-beasts.com/dns/v2/zones/example.com/records/@/TXT

What looks like the cause

From reading the dns-update 0.5.9 source:

  • src/http.rs: HttpClientBuilder::new() appends Content-Type: application/json to every request.
  • src/providers/mythicbeasts.rs: ensure_token() adds Content-Type: application/x-www-form-urlencoded with with_header(), which also appends rather than replaces.
  • reqwest 0.13.5: RequestBuilder::headers() appears to keep every value for a header name (its own test_replace_headers test asserts this).

If that reading is right, the token request goes out with two Content-Type headers. Tests 3 and 4 suggest Mythic Beasts’ auth service rejects that with invalid_request whatever the order.

That would seem to account for both errors in the task log. If the login fails, no token is cached, so every record set appears to retry the login, and after a few failed attempts the auth service starts returning 429. The provider also looks like it uses send() rather than send_with_retry(), so there would be no backoff on the 429s.

I haven’t captured Stalwart’s own outbound traffic, so the client side of this is based on reading the source rather than direct observation.

Many thanks,

Fixed in v0.16.27 (yet to be released).

Thank you. Much appreciated.

I have installed 0.16.27 and, unfortunately, the Mythic Beasts DNS provider still doesn’t work. I’ve double-checked the API key and permissions, but I’m still getting exactly the same error :frowning:

Can you submit a PR to the dns-update crate? We do not have an API key to test your provider.

Happy to help however I can, but I have no idea what that is, where it is or how to do it :slight_smile:

If you can point me at some instructions, I’ll do it ASAP.

Thank you.