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

Socket not closing after server disconnect #1797

Merged
merged 1 commit into from
Dec 16, 2021
Merged

Conversation

pawl
Copy link
Contributor

@pawl pawl commented Dec 16, 2021

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)?

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

Description of change

This is based on @michael-lazar's recent work on fixing a Celery memory leak caused by py-amqp: celery/py-amqp#374

I recently learned that socket.shutdown() can throw an OSError when the server caused the disconnection. I also learned that socket.shutdown and socket.close have different purposes: https://stackoverflow.com/a/598759

So, you still need to run socket.close after socket.shutdown to allow garbage collection to deallocate the socket.

Currently, redis-py's Connection.disconnect() won't continue to close the socket if it encounters an OSError on shutdown. This prevents garbage collection from cleaning up the socket and will use more memory than necessary (maybe even causing a leak?).

This PR puts a separate try/except around socket.close and socket.shutdown to allow the socket to be closed after a failed shutdown.

@chayim chayim added the bug Bug label Dec 16, 2021
@chayim chayim changed the title Bugfix: not closing socket after server disconnect Socket not closing after server disconnect Dec 16, 2021
@chayim
Copy link
Contributor

chayim commented Dec 16, 2021

@pawl Great catch, and a great PR, thank you! I'm particularly appreciative of your adding unit tests for this. Approving for merge, and it'll be in the upcoming release.

@auvipy
Copy link

auvipy commented Dec 21, 2021

which redis release will include this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants