Skip to content

fix: properly cleanup resources like redis and background tasks - #6724

Merged
masenf merged 4 commits into
reflex-dev:mainfrom
benedikt-bartscher:close-redis-token-manager
Jul 9, 2026
Merged

masenf merged 4 commits into
reflex-dev:mainfrom
benedikt-bartscher:close-redis-token-manager

Conversation

@benedikt-bartscher

Copy link
Copy Markdown
Contributor

No description provided.

@greptile-apps

greptile-apps Bot commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes leaked Redis connections and uncleaned background tasks on app shutdown by adding a close() lifecycle method to TokenManager and wiring it into the lifespan cleanup with a try/finally guarantee.

  • Adds an abstract close() to TokenManager with a no-op in LocalTokenManager and a full implementation in RedisTokenManager that cancels _socket_record_task/_lost_and_found_task and calls redis.aclose().
  • Wraps disconnect_all() in a try/finally in lifespan.py so close() is always called even when disconnect_all() raises.
  • Extracts the NOTIFY_KEYSPACE_EVENTS constant and enable_keyspace_notifications() helper into redis.py, removing the duplicate inline string from StateManagerRedis and RedisTokenManager.

Confidence Score: 5/5

Safe to merge — the change only adds cleanup paths that were previously missing; no existing logic is altered.

The fix is narrowly scoped: a try/finally in the lifespan teardown path and a new close() that cancels tasks and calls redis.aclose(). Both code paths are covered by new unit tests, and LocalTokenManager.close() is a deliberate no-op so non-Redis deployments are unaffected.

No files require special attention.

Important Files Changed

Filename Overview
reflex/utils/token_manager.py Adds abstract close() to TokenManager, a no-op implementation in LocalTokenManager, and a full implementation in RedisTokenManager that cancels background pub/sub tasks and calls redis.aclose().
reflex/app_mixins/lifespan.py Wraps disconnect_all() in an inner try/finally so close() is always called regardless of whether disconnect_all() raises.
reflex/istate/manager/redis.py Extracts NOTIFY_KEYSPACE_EVENTS constant and enable_keyspace_notifications() helper to reduce duplication between StateManagerRedis and RedisTokenManager.
tests/units/utils/test_token_manager.py Adds two tests covering close(): one that verifies task cancellation and Redis disconnection, and one for the no-tasks-started case.
news/6724.bugfix.md Changelog entry describing the fix for leaked Redis connections on server shutdown.

Reviews (4): Last reviewed commit: "thanks codex!" | Re-trigger Greptile

Comment thread reflex/app_mixins/lifespan.py Resolved
@codspeed

codspeed Bot commented Jul 9, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 26 untouched benchmarks
⏩ 8 skipped benchmarks1


Comparing benedikt-bartscher:close-redis-token-manager (b2eb963) with main (2d446c3)2

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

  2. No successful run was found on main (d54ad96) during the generation of this report, so 2d446c3 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@benedikt-bartscher
benedikt-bartscher marked this pull request as ready for review July 9, 2026 08:43
@benedikt-bartscher
benedikt-bartscher requested a review from a team as a code owner July 9, 2026 08:43

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5e171d29bb

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread reflex/utils/token_manager.py Resolved

@masenf masenf left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@masenf
masenf merged commit c03ab4b into reflex-dev:main Jul 9, 2026
107 checks passed
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.

2 participants