Skip to content

Hold references to ClusterNode disconnect task#3826

Merged
petyaslavova merged 3 commits intoredis:masterfrom
rhoboro:feat/hold-references-to-disconnect-task
Feb 25, 2026
Merged

Hold references to ClusterNode disconnect task#3826
petyaslavova merged 3 commits intoredis:masterfrom
rhoboro:feat/hold-references-to-disconnect-task

Conversation

@rhoboro
Copy link
Copy Markdown
Contributor

@rhoboro rhoboro commented Oct 31, 2025

Thank you for this awesome library! I've been a long-time user of this project ✨

Pull Request check-list

  • Do tests and lints pass with this change?
  • Do the CI tests pass with this change (enable it first in your forked repo and wait for the github action build to finish)?
  • Is the new or changed code fully tested?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Is there an example added to the examples folder (if applicable)?

NOTE: these things are not required to open a PR and can be done
afterwards / while the PR is open.

Description of change

I found a potential bug and have fixed it.
This fix aligns with the recommendations in the official documentation linked below, as I have personally encountered unintended task loss due to this issue in the past.

Important: Save a reference to the result of this function, to avoid a task disappearing mid-execution. The event loop only keeps weak references to tasks. A task that isn’t referenced elsewhere may get garbage collected at any time, even before it’s done. For reliable “fire-and-forget” background tasks, gather them in a collection...

https://docs.python.org/3.13/library/asyncio-task.html#asyncio.create_task

@jit-ci
Copy link
Copy Markdown

jit-ci bot commented Oct 31, 2025

Hi, I’m Jit, a friendly security platform designed to help developers build secure applications from day zero with an MVS (Minimal viable security) mindset.

In case there are security findings, they will be communicated to you as a comment inside the PR.

Hope you’ll enjoy using Jit.

Questions? Comments? Want to learn more? Get in touch with us.

@petyaslavova
Copy link
Copy Markdown
Collaborator

Hi @rhoboro, thank you for your contribution! We will review your change soon.

@petyaslavova petyaslavova requested a review from Copilot February 25, 2026 08:31
@petyaslavova petyaslavova added the maintenance Maintenance (CI, Releases, etc) label Feb 25, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a potential bug where background tasks created for disconnecting removed cluster nodes could be prematurely garbage collected due to Python's asyncio only keeping weak references to tasks. The fix holds strong references to these tasks in a set, ensuring they complete execution.

Changes:

  • Added _background_tasks set to track disconnection tasks
  • Updated set_nodes to store task references and automatically remove them upon completion

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Collaborator

@petyaslavova petyaslavova left a comment

Choose a reason for hiding this comment

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

LGTM.

@petyaslavova petyaslavova merged commit 5b46430 into redis:master Feb 25, 2026
63 checks passed
petyaslavova added a commit that referenced this pull request Feb 25, 2026
* Hold references to ClusterNode disconnect task

* Fix linters

---------

Co-authored-by: petyaslavova <petya.slavova@redis.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintenance Maintenance (CI, Releases, etc)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants