Skip to content
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

Improvement: Use shutdown() Before close() in connection.py #3567

Merged
merged 7 commits into from
Mar 26, 2025

Conversation

allrob23
Copy link
Contributor

Pull Request check-list

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

  • 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)?
  • Was the change added to CHANGES file?

Description of change

This PR improves socket resource management by implementing the recommended shutdown-then-close pattern in error handling scenarios.

Benefits

  1. Ensures proper connection termination – shutdown() initiates the TCP termination sequence before releasing resources.
  2. Prevents potential resource leaks – Reduces the risk of lingering sockets in edge cases.
  3. Enhances network reliability – Ensures pending data is handled correctly before closing.
  4. Aligns with Python best practices – Follows Python’s official socket documentation recommendations.

Technical Details

The Python documentation explicitly states:

"close() releases the resource associated with a connection but does not necessarily close the connection immediately. If you want to close the connection in a timely fashion, call shutdown() before close()."
Python socket documentation

This change introduces properly exception-handled shutdown() calls before close() to ensure sockets are gracefully terminated, even when exceptions occur.

Impact

This is a low-risk change that improves resource cleanup and robustness. The modification is minimal but aligns the codebase with networking best practices, ensuring better socket lifecycle management.

@petyaslavova
Copy link
Collaborator

Hi @allrob23 thank you for your contribution! We’ll review your change soon.

@petyaslavova
Copy link
Collaborator

Hi @allrob23 can you please check the linter errors?

@allrob23
Copy link
Contributor Author

@petyaslavova done, can you please run the check again?

Copy link
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 added the maintenance Maintenance (CI, Releases, etc) label Mar 26, 2025
@petyaslavova petyaslavova merged commit c42df94 into redis:master Mar 26, 2025
37 checks passed
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.

2 participants