Skip to content

fix(c8y): ask for a new token again when a request goes unanswered - #33

Merged
reubenmiller merged 1 commit into
mainfrom
fix/jwt-refresh
Nov 19, 2026
Merged

reubenmiller merged 1 commit into
mainfrom
fix/jwt-refresh

Conversation

@reubenmiller

Copy link
Copy Markdown
Contributor

Problem: a certificate device asks Cumulocity for a token on s/uat, and the token arrives on s/dat. It uses the token for downloads, log uploads and certificate renewal. The client started its 50-minute refresh timer when it sent the request, not when the token arrived. So:

  • If one refresh reply was lost, the device kept its old token until its MQTT session restarted. Cumulocity's tokens last about an hour by default, so downloads, uploads and certificate renewal would then fail with 401.
  • If the first request after a session started went unanswered, it was never repeated.

Fix:

  • The refresh timer now starts when a token arrives.
  • A request that gets no answer is repeated after a minute, until a token arrives.
  • Every new session still asks for a token straight away.

The decision is a pure helper, core/src/tedge_jwt.c (tedge_jwt_request_due), listed under TEDGE_TRANSPORT_C8Y in core/sources.cmake. It has 6 unit tests in ports/zephyr/tests/unit: the first request, waiting for an answer, a lost first request, keeping a fresh token, the refresh, and a lost refresh.

Checked:

  • The POSIX port builds with -DTEDGE_WERROR=ON, and its tests pass.
  • The unit test cases pass against the helper compiled on the host. CI runs the Zephyr unit tests themselves.

Context: this came up while looking into #32 (the NuttX firmware download failing with -104). It is not that bug's cause: those downloads failed with a connection reset, not 401, and the second attempt had a token only seconds old.

🤖 Generated with Claude Code

The client reset its 50-minute refresh timer when it sent s/uat, not when
the token arrived. One lost reply left a certificate device with its old
token until the session restarted: about an hour after it arrived, the
token expired, and downloads, log uploads and certificate renewal would
get 401. A first request that went unanswered was never repeated at all.

The timer now starts when the token arrives, and a request with no answer
is repeated after a minute. The decision is a pure helper, tedge_jwt.c,
with unit tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@reubenmiller
reubenmiller merged commit f6d0a4e into main Nov 19, 2026
20 checks passed
@reubenmiller
reubenmiller deleted the fix/jwt-refresh branch November 19, 2026 03:34
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.

1 participant