Skip to content

[GROW-3247] release connection even if an unexpected exception is thrown in cluster pipeline #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 20, 2023

Conversation

zach-iee
Copy link

Pull Request check-list

Please make sure to review and check all of these items:

  • Does $ tox pass with this change (including linting)?
  • 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)?
  • Was the change added to CHANGES file?

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

Description of change

i've confirmed that the connection is not released to the connection pool, when an unexpected exception occurs during cluster pipeline execution. This PR ensures that ClusterPipeline releases connection, even if an unexpected exception occurs.

connections are disconnected before they are released to the pool in order to prevent any socket-related issues that redis-py comment discusses

*c.args, node_flag=passed_targets
)
if not target_nodes:
try:
Copy link
Author

@zach-iee zach-iee Jul 19, 2023

Choose a reason for hiding this comment

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

diff is mostly due to the added indentation. the only code i've added are try, except BaseException, n.connection = None and nodes = {}

Choose a reason for hiding this comment

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

Then i will use it
image

Copy link
Author

Choose a reason for hiding this comment

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

OMG never knew about this...

@codecov-commenter
Copy link

codecov-commenter commented Jul 19, 2023

Codecov Report

Patch coverage: 91.25% and project coverage change: -0.02 ⚠️

Comparison is base (68c3505) 92.34% compared to head (fd76ff9) 92.33%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master       #8      +/-   ##
==========================================
- Coverage   92.34%   92.33%   -0.02%     
==========================================
  Files         119      119              
  Lines       30615    30638      +23     
==========================================
+ Hits        28272    28289      +17     
- Misses       2343     2349       +6     
Impacted Files Coverage Δ
redis/cluster.py 90.86% <89.70%> (+0.11%) ⬆️
tests/test_cluster.py 96.76% <100.00%> (-0.19%) ⬇️

... and 2 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Comment on lines +2040 to +2041
n.connection = None
nodes = {}
Copy link
Author

Choose a reason for hiding this comment

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

added n.connection = None, so that already returned n.connection is not used again
added nodes = {} to clear out NodeCommands from nodes

Copy link
Author

Choose a reason for hiding this comment

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

Comment on lines +2095 to +2096
n.connection = None
nodes = {}
Copy link
Author

Choose a reason for hiding this comment

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

added n.connection = None, so that already returned n.connection is not used again
added nodes = {} to clear out NodeCommands from nodes

Copy link
Author

Choose a reason for hiding this comment

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

Comment on lines +2158 to +2166
except BaseException:
# if nodes is not empty, a problem must have occurred
# since we cant guarantee the state of the connections,
# disconnect before returning it to the connection pool
for n in nodes.values():
if n.connection:
n.connection.disconnect()
n.connection_pool.release(n.connection)
raise
Copy link
Author

Choose a reason for hiding this comment

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

i've added this to disconnect and return the connection to the pool

for n in nodes.values():
n.connection_pool.release(n.connection)
n.connection = None
nodes = {}

Choose a reason for hiding this comment

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

Suggested change
nodes = {}
del nodes[node_name]

I don't think we should delete other healthy nodes. 🤔

Copy link
Author

Choose a reason for hiding this comment

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

the variable name is confusing, but nodes here aren't actual nodes in the redis cluster, but NodeCommands object that's only used in this function. Since we raise the exception in line 2051 anyway, there is no difference between del nodes[node_name] vs nodes = {}

Copy link

@bellatoris bellatoris left a comment

Choose a reason for hiding this comment

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

for me, it completely makes sense. But beside of the changes of this PR, I can't see any reason to have infinite loop in here, because there is no continue or other paths to reach to break line except some exceptions that percolate up to out of loop (and loop is useless for this exception cases)

*c.args, node_flag=passed_targets
)
if not target_nodes:
try:

Choose a reason for hiding this comment

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

Then i will use it
image

@zach-iee
Copy link
Author

for me, it completely makes sense. But beside of the changes of this PR, I can't see any reason to have infinite loop in here, because there is no continue or other paths to reach to break line except some exceptions that percolate up to out of loop (and loop is useless for this exception cases)

i completely agree. It was introduced in https://github.com/redis/redis-py/pull/2225/files, but i think continue statement got lost afterwards

@zach-iee zach-iee requested a review from jihoShin87 July 19, 2023 15:24
Copy link

@jihoShin87 jihoShin87 left a comment

Choose a reason for hiding this comment

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

LGTM!!!!

@zach-iee zach-iee merged commit ae88892 into master Jul 20, 2023
zach-iee added a commit that referenced this pull request Jul 20, 2023
…own in cluster pipeline (#8)

* [GROW-3247] release connection even if an unexpected exception is thrown in cluster pipeline

* [GROW-3247] fix style issue

* unassign n.connection at every loop
zach-iee added a commit that referenced this pull request Jul 20, 2023
…own in cluster pipeline (#8)

* [GROW-3247] release connection even if an unexpected exception is thrown in cluster pipeline

* [GROW-3247] fix style issue

* unassign n.connection at every loop
@zach-iee zach-iee deleted the GROW-3247 branch July 25, 2023 08:53
zach-iee added a commit that referenced this pull request Aug 21, 2023
…own in cluster pipeline (#8)

* [GROW-3247] release connection even if an unexpected exception is thrown in cluster pipeline

* [GROW-3247] fix style issue

* unassign n.connection at every loop
zach-iee added a commit that referenced this pull request Aug 21, 2023
…own in cluster pipeline (#8)

* [GROW-3247] release connection even if an unexpected exception is thrown in cluster pipeline

* [GROW-3247] fix style issue

* unassign n.connection at every loop
zach-iee added a commit that referenced this pull request Sep 14, 2023
…own in cluster pipeline (#8)

* [GROW-3247] release connection even if an unexpected exception is thrown in cluster pipeline

* [GROW-3247] fix style issue

* unassign n.connection at every loop
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.

4 participants