-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
Fix socket leaks #351
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
Fix socket leaks #351
Conversation
Hello, and thanks for your contribution! I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA). Unfortunately we couldn't find an account corresponding to your GitHub username on bugs.python.org (b.p.o) to verify you have signed the CLA. This is necessary for legal reasons before we can look at your contribution. Please follow these steps to help rectify the issue:
Thanks again to your contribution and we look forward to looking at it! |
I'm signed the CLA |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"with conn:" cannot be used?
Oh, ignore my comment, I missed the Python version (2.7): in Python 2.7, socket.socket doesn't support the context manager protocol :-( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
retrbinary(), retrlines() and storelines() at least have the same bug, no? Can you please fix similar bugs in other functions?
Ok, I did |
Should there be an issue in the bug tracker for this? Also, I'm applying the |
callback(line) | ||
finally: | ||
if fp: | ||
fp.close() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can fp.close()
fail? If yes, conn
will be not closed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The file is in read mode, I don't see why fp.close() would fail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An alternative is to use two try/finally: conn and then fp. It would avoid "fp = None" as well. Do you think that it's worth it @serhiy-storchaka ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
KeyboardInterrupt
;-)
You can use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but I prefer to wait for @serhiy-storchaka for retrlines().
callback(line) | ||
finally: | ||
if fp: | ||
fp.close() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An alternative is to use two try/finally: conn and then fp. It would avoid "fp = None" as well. Do you think that it's worth it @serhiy-storchaka ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. I think we can ignore very small issue in retrlines()
in 2.7. I just remembered of alternatives for the case if you missed them.
Thank you @J0l-lnny for your fix! I merged your PR. |
@serhiy-storchaka: "LGTM. I think we can ignore very small issue in retrlines() in 2.7." Right, thank you for your review. If someone wants to handle the most complex corner cases, I suggest to focus on the master branch. Right now, the master branch uses a "nested" with which isn't ideal since it doesn't handle exceptions in creating fp fails (or is interrupted by CTRL+c or whatever): Extract of ftplib.py in the master branch:
At least, it ensures that conn and fp are closed properly, right? |
What is wrong with a nested with? The resources management in the master branch looks correct to me. |
If makefile() raises an exception, conn is not closed. Well, in fact I'm not sure, that's why I was asking :-) |
It is properly closed.
is just a syntax sugar for
You perhaps missed with deprecated and removed
|
(...) is just a syntax sugar for "with a: with b: ..."
Ok! master is fine in that case ;-) Thanks for the confirmation.
Note: By "nested", I really mean "with expr1 as a, expr2 as b: ...".
|
Bumps [celery](https://github.com/celery/celery) from 4.4.2 to 4.4.6. - [Release notes](https://github.com/celery/celery/releases) - [Changelog](https://github.com/celery/celery/blob/master/Changelog.rst) - [Commits](celery/celery@4.4.2...v4.4.6) Signed-off-by: dependabot-preview[bot] <[email protected]> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
The close method moved to finally block, so closing a socket will occur even in case of exception