When the token expires, refreshing the token can result in an infinite loop. #6646

Closed
opened 2026-02-21 18:16:41 -05:00 by yindo · 5 comments
Owner

Originally created by @wangxm345566462 on GitHub (Nov 9, 2024).

Self Checks

  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

v0.10.0

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

image
Steps:

Create a new chat.
Configure any custom tool.
Engage in normal conversation.
Wait for some time until the token expires.
Continue the conversation, and the system will start excessively calling the /console/api/apps/4e2a312e-cb06-41b0-a191-a94d42d85523/chat-messages endpoint, resulting in an infinite loop.

✔️ Expected Behavior

ok

Actual Behavior

loop

Originally created by @wangxm345566462 on GitHub (Nov 9, 2024). ### Self Checks - [X] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general). - [X] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [X] I confirm that I am using English to submit this report (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). - [X] [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:) - [X] Please do not modify this template :) and fill in all the required fields. ### Dify version v0.10.0 ### Cloud or Self Hosted Self Hosted (Docker) ### Steps to reproduce ![image](https://github.com/user-attachments/assets/2af80424-627b-4f9b-b109-1ee130858089) Steps: Create a new chat. Configure any custom tool. Engage in normal conversation. Wait for some time until the token expires. Continue the conversation, and the system will start excessively calling the /console/api/apps/4e2a312e-cb06-41b0-a191-a94d42d85523/chat-messages endpoint, resulting in an infinite loop. ### ✔️ Expected Behavior ok ### ❌ Actual Behavior loop
yindo added the 🐞 bug🤔 cant-reproduce labels 2026-02-21 18:16:41 -05:00
yindo closed this issue 2026-02-21 18:16:41 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Nov 9, 2024):

To prevent an infinite loop when refreshing the token after it expires in a self-hosted Dify v10.10.0 setup, ensure that the token refresh logic does not use the baseFetch function. This function might attempt to refresh the token itself upon receiving a 401 response, leading to an infinite loop. Instead, handle the token refresh in a dedicated function that does not call baseFetch and uses a single retry mechanism. Here's a code snippet that demonstrates this approach:

// Do not use baseFetch to refresh tokens.
// If a 401 response occurs and baseFetch itself attempts to refresh the token,
// it can lead to an infinite loop if the refresh attempt also returns 401.
// To avoid this, handle token refresh separately in a dedicated function
// that does not call baseFetch and uses a single retry mechanism.
const [error, ret] = await fetchWithRetry(globalThis.fetch(`${apiPrefix}/refresh-token`, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json;utf-8',
  },
  body: JSON.stringify({ refresh_token }),
}))
if (error) {
  return Promise.reject(error)
}
else {
  if (ret.status === 401)
    return Promise.reject(ret)
  const { data } = await ret.json()
  globalThis.localStorage.setItem('console_token', data.access_token)
  globalThis.localStorage.setItem('refresh_token', data.refresh_token)
}

This approach ensures that the token refresh logic is isolated and does not inadvertently cause an infinite loop by retrying on 401 responses [1].

To continue talking to Dosu, mention @dosu.

@dosubot[bot] commented on GitHub (Nov 9, 2024): <!-- Answer --> To prevent an infinite loop when refreshing the token after it expires in a self-hosted Dify v10.10.0 setup, ensure that the token refresh logic does not use the `baseFetch` function. This function might attempt to refresh the token itself upon receiving a 401 response, leading to an infinite loop. Instead, handle the token refresh in a dedicated function that does not call `baseFetch` and uses a single retry mechanism. Here's a code snippet that demonstrates this approach: ```ts // Do not use baseFetch to refresh tokens. // If a 401 response occurs and baseFetch itself attempts to refresh the token, // it can lead to an infinite loop if the refresh attempt also returns 401. // To avoid this, handle token refresh separately in a dedicated function // that does not call baseFetch and uses a single retry mechanism. const [error, ret] = await fetchWithRetry(globalThis.fetch(`${apiPrefix}/refresh-token`, { method: 'POST', headers: { 'Content-Type': 'application/json;utf-8', }, body: JSON.stringify({ refresh_token }), })) if (error) { return Promise.reject(error) } else { if (ret.status === 401) return Promise.reject(ret) const { data } = await ret.json() globalThis.localStorage.setItem('console_token', data.access_token) globalThis.localStorage.setItem('refresh_token', data.refresh_token) } ``` This approach ensures that the token refresh logic is isolated and does not inadvertently cause an infinite loop by retrying on 401 responses <sup>[[1]](https://github.com/langgenius/dify/blob/main/web/service/refresh-token.ts)</sup>. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
Author
Owner

@crazywoola commented on GitHub (Nov 9, 2024):

This should been fixed in v0.11.0. If you can reproduce this problem on latest version, please update the version in the description.

@crazywoola commented on GitHub (Nov 9, 2024): This should been fixed in v0.11.0. If you can reproduce this problem on latest version, please update the version in the description.
Author
Owner

@wangxm345566462 commented on GitHub (Nov 9, 2024):

image
The version I mentioned in my question is actually 0.10.0. I made an error when writing it.

@wangxm345566462 commented on GitHub (Nov 9, 2024): ![image](https://github.com/user-attachments/assets/852f7339-b0ad-42ff-a1d8-8f74127132aa) The version I mentioned in my question is actually 0.10.0. I made an error when writing it.
Author
Owner

@wangxm345566462 commented on GitHub (Nov 9, 2024):

This should been fixed in v0.11.0. If you can reproduce this problem on latest version, please update the version in the description.

I cannot consistently reproduce the issue either, but it occurred twice in one day during my conversations. The symptom is that a message gets stuck and does not receive a reply, with the backend logs indicating that it entered an infinite loop. Clicking "Stop Output" on the interface will cause the backend loop to exit, and the next message input might then respond normally. I believe this is a potential bug.

@wangxm345566462 commented on GitHub (Nov 9, 2024): > This should been fixed in v0.11.0. If you can reproduce this problem on latest version, please update the version in the description. I cannot consistently reproduce the issue either, but it occurred twice in one day during my conversations. The symptom is that a message gets stuck and does not receive a reply, with the backend logs indicating that it entered an infinite loop. Clicking "Stop Output" on the interface will cause the backend loop to exit, and the next message input might then respond normally. I believe this is a potential bug.
Author
Owner

@crazywoola commented on GitHub (Nov 13, 2024):

Well, it could be, but we need some details to reproduce this, please keep an eye on this issue, you could open another issue later if you encountered it again, we seems to have some related fixes regarding the tokens.

@crazywoola commented on GitHub (Nov 13, 2024): Well, it could be, but we need some details to reproduce this, please keep an eye on this issue, you could open another issue later if you encountered it again, we seems to have some related fixes regarding the tokens.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#6646