Skip to content

fix(bot): refresh Feishu tenant token after invalid-token reply - #1524

Open
wcy12378 wants to merge 1 commit into
Tencent:mainfrom
wcy12378:pi/lf1485-feishu-tenant-token
Open

wcy12378 wants to merge 1 commit into
Tencent:mainfrom
wcy12378:pi/lf1485-feishu-tenant-token

Conversation

@wcy12378

Copy link
Copy Markdown

fix(bot): refresh Feishu tenant token after invalid-token reply

This is the #1485 slice: recover the Feishu bot after its tenant access token expires.

Problem

The Feishu long-connection channel keeps a feishuConn alive with one Lark client. The SDK's default token cache is process-global, so an expired tenant token stayed cached. Reply attempts rejected with 99991663 were marked Retryable, but the retry reused the same client and stale token — the bot never recovered until restart.

(The Push path creates a fresh client per call and was unaffected.)

Change

  • internal/botinteraction/channel_feishu.go
    • client() accepts optional extra Lark options (existing callers unchanged).
    • New feishuTokenCache — a client-scoped larkcore.Cache with mutex + TTL.
    • feishuConn stores its credential and guards api with a mutex; freshAPI() builds a client with an empty token cache; resetAPI() swaps it in.
    • Reply resets the client only on invalid/missing access-token codes (99991661/63/64/68). Rate-limit (99991400) and 5xx/transport failures leave the client untouched.
  • Tests cover: invalid token → next Reply uses a fresh token (token endpoint hit twice) and succeeds; rate-limit/server errors do not rebuild the client.

No credential-model change; no front-end change.

Verification (Linux/amd64, Docker python:3.12-slim)

  • 2 targeted tests RED→GREEN.
  • internal/botinteraction full package: PASS.
  • go vet (including copylocks), git diff --check, secret scan: clean. Windows cannot compile (Linux-specific syscalls); Go tests were cross-compiled and run in the container.

Scope / notes

  • The SDK's WithTokenCache replaces a package-level token manager; keys include appID+secret, so bots do not share tokens. The only side effect is that a later default client may reuse the most recent conn's cache (one extra token fetch at most); documented in the handoff report.
  • Issue timeline API shows no linked PR and no claim. Part of [Bug]: 飞书 Tenant Token 失效后持续复用缓存,导致机器人无法回复 #1485; does not close the issue.

The Lark SDK's default token cache is process-global, so a long-lived
feishuConn kept reusing an expired tenant access token after 99991663. A
retry marked Retryable therefore kept failing with the same stale token
and the bot never recovered (Tencent#1485).

Give each feishuConn client its own token cache and rebuild client+cache when
a reply is rejected with an invalid/missing access-token code (99991661/63/
64/68), so the next retry fetches a fresh token. Rate-limit (99991400) and
server/transport failures leave the client unchanged.

Part of Tencent#1485; does not close the issue.
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