Vaillant: FATAL "could not get code" on startup with two myVaillant chargers (regression 0.307.2 → 0.308.0, race between parallel logins)
Describe the bug
With two vaillant chargers configured (same myVaillant account, realm: germany), evcc fails to start on 0.308.0 and 0.308.1. During startup one of the two chargers aborts with:
FATAL ... could not get code
Which of the two chargers fails alternates between restarts (sometimes the first, sometimes the second). evcc does not come up fully and logs will attempt restart in 15m; it eventually self-heals once the cooldown passes, only to hit the same race on the next start.
On 0.307.2 this does not happen — both chargers initialize cleanly (power ✓) and evcc reports healthy. Pinning the image back to 0.307.2 is the current workaround.
Steps to reproduce
- Configure two
vaillantchargers using the same myVaillant credentials (e.g. one loadpoint for hot water, one for heating). - Run on
evcc/evcc:0.308.0(or0.308.1). - Start the container → one Vaillant charger fails with
could not get code, evcc aborts and schedules a restart in 15 min. - Downgrade to
0.307.2→ both chargers start, evcc healthy.
Trace log analysis
With log: trace the two chargers log in concurrently at startup:
- Login A succeeds: OIDC token obtained,
/homesand/mpcreturn OK. - Login B fails: the Keycloak step errors with "cookie could not be found" → no authorization
codeis returned →could not get code.
This points to a race between the two parallel Vaillant OIDC logins introduced in 0.308.0. In 0.307.2 the (token-sharing) login path tolerates two concurrent charger inits; in 0.308.0 the concurrent Keycloak auth-code flows appear to clobber each other's session/cookie state.
Configuration (redacted)
chargers:
- name: vaillant_ww
type: template
template: vaillant
realm: germany
user: <email>
password: <password>
- name: vaillant_heizung
type: template
template: vaillant
realm: germany
user: <email> # same account
password: <password>
# heating circuit (zone/setpoint params)
Expected behaviour
Two Vaillant chargers on the same account start reliably (as in 0.307.2), i.e. the concurrent logins are serialized or share the token/session instead of racing on the Keycloak auth-code flow.
Version
- Broken: 0.308.0, 0.308.1
- Last known good: 0.307.2
- Deployment: Docker (
evcc/evcc), Linux/amd64
Interesting. I don't think there were any changed regarding Vaillant. We should still serialise/combine the logins into a shared token source.
@Johannes-del could you share a trace log? That would speed up the fix.
Last known good: 0.307.2
oc := sensonet.Oauth2ConfigForRealm(cc.Realm)
token, err := oc.PasswordCredentialsToken(logCtx, cc.User, cc.Password)
if err != nil {
return nil, err
}
It's unclear to me why this shouldn't work concurrently.
Reproduced with log: trace. Two template: vaillant chargers on the same myVaillant account, created concurrently at startup. Not 0.308-specific - happens on 0.307.2 too, just masked there by the Docker restart policy occasionally winning the race.
Mechanism: both logins share one cookie jar. Each fires its own GET .../openid-connect/auth (different PKCE challenge/state). Keycloak's auth session lives in the AUTH_SESSION_ID cookie from that GET; the second login's Set-Cookie overwrites the first in the shared jar. The losing charger then POSTs to /login-actions/authenticate and Keycloak returns its cookieNotFound page instead of a 302 with ?code=, so PasswordCredentialsToken gets no code → could not get code → charger FATAL.
Scrubbed trace (tokens/PII removed, timestamps Europe/Berlin):
# two CONCURRENT authorize requests — note the two different PKCE challenges/states
[vaillant] TRACE 08:56:15 GET .../openid-connect/auth?client_id=myvaillant&code_challenge=IqTRc5EZ…A…&state=FvSIRC0w…A
[vaillant] TRACE 08:56:15 GET .../openid-connect/auth?client_id=myvaillant&code_challenge=lY9XnUCA…B…&state=M77IEIjE…B
# losing charger POSTs credentials (evcc already masks user/pass) …
[vaillant] TRACE 08:56:15 POST .../login-actions/authenticate?session_code=…&execution=…&tab_id=…
[vaillant] TRACE 08:56:15 credentialId=&password=***&username=***
# … Keycloak returns cookieNotFound instead of a 302 with ?code= :
[vaillant] TRACE 08:56:15 <div class="cookie-notice">Cookie konnte nicht gefunden werden. …</div>
[main] FATAL 08:56:18 charger [vaillant_wp] … type 'vaillant': could not get code
Fix direction matches your comment: share a single oauth2.TokenSource across chargers with identical credentials (one login for N devices), or at minimum give each login its own cookie jar so the concurrent Keycloak auth sessions don't collide.
Please don't post ai garbage. Afaikt the Vaillant charger doesn't use a cookie jar at all and it's not clear (to me) why it would be needed unless something has changed.
Sry, and yes, no shared jar, each charger has its own client. I don't actually know why the concurrency breaks it then. All I can say is it only happens with two chargers (Warmwasser & Heizung) on the same account at startup, and never with one. Your shared-TokenSource-per-account idea would avoid the parallel login entirely. I'd be glad to test a patch.
Edit: After reading the posts again, I think my comment is not matching this specific issue I also had a race condition problem with the version that is released right now my issue was with my cot counter that is reachable via solar edge Modbus and during startup this wasn’t possible so evcc didn’t work and before that I had a similar issue with the load balancing because that wasn’t there the wall boxes didn’t initialize. It seems that since the switch to the database conflict sometimes with race condition issues doing startup.
Sure, give it a try.
PR never got test feedback :(
I finally tested the fix from #30666 (fix/vaillant-shared-tokensource, 578d83a). Sorry it took so long!
Test setup: I built the branch as a static binary and ran it against my real configuration with both Vaillant chargers on the same myVaillant account (vaillant_wp for DHW and a second vaillant_heizung for the heating zone). I tested it in isolation using a separate throwaway database with my production instance stopped, so only these two logins were accessing the account.
Result: It works. Both loadpoints came up, both chargers authenticated successfully (with a car connected to each), and there were no FATAL could not get code, 401/403/invalid_grant errors, or restart loops. The instance stayed up cleanly for the entire test. This is exactly the two-charger, same-account, parallel-startup scenario that previously caused startup to fail reliably (#30625), so the shared, mutex-serialized vaillant.Identity() token source appears to solve the problem.
One caveat: this was only a single test run. Since the race condition is timing-dependent, I didn't want to repeatedly hammer the Keycloak endpoint with rapid login attempts and risk triggering anti-abuse throttling. However, the test did reproduce the previously failing scenario and passed successfully.
Could you reopen #30666 and rebase it onto the current master branch? The branch is based on an early June commit (pre-0.310.x), so I'd like to test the rebased version against my current production setup before enabling it permanently. I'm happy to test it again after the rebase. Thanks again for the fix! 🙏