Skip to content

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

Merged
merged 2 commits into from
Apr 3, 2017
Merged

Fix socket leaks #351

merged 2 commits into from
Apr 3, 2017

Conversation

it-x
Copy link

@it-x it-x commented Feb 27, 2017

The close method moved to finally block, so closing a socket will occur even in case of exception

@the-knights-who-say-ni
Copy link

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:

  1. If you don't have an account on b.p.o, please create one
  2. Make sure your GitHub username is listed in "Your Details" at b.p.o
  3. If you have not already done so, please sign the PSF contributor agreement. The "bugs.python.org username " requested by the form is the "Login name" field under "Your Details".
  4. If you just signed the CLA, please wait at least one US business day and then check "Your Details" on bugs.python.org to see if your account has been marked as having signed the CLA (the delay is due to a person having to manually check your signed CLA)
  5. Reply here saying you have completed the above steps

Thanks again to your contribution and we look forward to looking at it!

@it-x
Copy link
Author

it-x commented Feb 28, 2017

I'm signed the CLA

Copy link
Member

@vstinner vstinner left a 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?

@vstinner
Copy link
Member

vstinner commented Mar 2, 2017

Oh, ignore my comment, I missed the Python version (2.7): in Python 2.7, socket.socket doesn't support the context manager protocol :-(

Copy link
Member

@vstinner vstinner left a 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?

@it-x
Copy link
Author

it-x commented Mar 2, 2017

Ok, I did

@Mariatta
Copy link
Member

Mariatta commented Mar 4, 2017

Should there be an issue in the bug tracker for this?

Also, I'm applying the cherry-pick for 2.7 label here to make it more obvious that this is for 2.7 branch :)

callback(line)
finally:
if fp:
fp.close()
Copy link
Member

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.

Copy link
Member

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.

Copy link
Member

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 ?

Copy link
Member

Choose a reason for hiding this comment

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

KeyboardInterrupt ;-)

@serhiy-storchaka
Copy link
Member

You can use contextlib.closing().

Copy link
Member

@vstinner vstinner left a 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()
Copy link
Member

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 ?

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a 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.

@vstinner vstinner merged commit d64146c into python:2.7 Apr 3, 2017
@vstinner
Copy link
Member

vstinner commented Apr 3, 2017

Thank you @J0l-lnny for your fix! I merged your PR.

@vstinner
Copy link
Member

vstinner commented Apr 3, 2017

@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:

    def retrlines(self, cmd, callback = None):
        ...
        with self.transfercmd(cmd) as conn, \
                 conn.makefile('r', encoding=self.encoding) as fp:
        ...

At least, it ensures that conn and fp are closed properly, right?

@serhiy-storchaka
Copy link
Member

What is wrong with a nested with? The resources management in the master branch looks correct to me.

@vstinner
Copy link
Member

vstinner commented Apr 3, 2017

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

@serhiy-storchaka
Copy link
Member

It is properly closed.

with a, b:
        ...

is just a syntax sugar for

with a:
    with b:
        ...

You perhaps missed with deprecated and removed

with contextlib.nested(a, b):
        ...

@vstinner
Copy link
Member

vstinner commented Apr 3, 2017 via email

@it-x it-x deleted the 2.7 branch April 7, 2017 18:22
jaraco pushed a commit that referenced this pull request Dec 2, 2022
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>
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.

6 participants