-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
gh-142403: Avoid leaking file descriptor in socket.py #142404
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -376,45 +376,46 @@ def _sendfile_zerocopy(self, zerocopy_func, giveup_exc_type, file, | |
| # poll/select have the advantage of not requiring any | ||
| # extra file descriptor, contrarily to epoll/kqueue | ||
| # (also, they require a single syscall). | ||
| if hasattr(selectors, 'PollSelector'): | ||
| selector = selectors.PollSelector() | ||
| else: | ||
| selector = selectors.SelectSelector() | ||
| selector.register(sockno, selectors.EVENT_WRITE) | ||
|
|
||
| total_sent = 0 | ||
| # localize variable access to minimize overhead | ||
| selector_select = selector.select | ||
| try: | ||
| while True: | ||
| if timeout and not selector_select(timeout): | ||
| raise TimeoutError('timed out') | ||
| if count: | ||
| blocksize = min(count - total_sent, blocksize) | ||
| if blocksize <= 0: | ||
| break | ||
| try: | ||
| sent = zerocopy_func(fileno, offset, blocksize) | ||
| except BlockingIOError: | ||
| if not timeout: | ||
| # Block until the socket is ready to send some | ||
| # data; avoids hogging CPU resources. | ||
| selector_select() | ||
| continue | ||
| except OSError as err: | ||
| if total_sent == 0: | ||
| # We can get here for different reasons, the main | ||
| # one being 'file' is not a regular mmap(2)-like | ||
| # file, in which case we'll fall back on using | ||
| # plain send(). | ||
| raise giveup_exc_type(err) | ||
| raise err from None | ||
| else: | ||
| if sent == 0: | ||
| break # EOF | ||
| offset += sent | ||
| total_sent += sent | ||
| return total_sent | ||
| with ( | ||
| selectors.PollSelector() | ||
| if hasattr(selectors, 'PollSelector') | ||
| else selectors.SelectSelector() | ||
| ) as selector: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer to use |
||
| selector.register(sockno, selectors.EVENT_WRITE) | ||
|
|
||
| # localize variable access to minimize overhead | ||
| selector_select = selector.select | ||
| while True: | ||
| if timeout and not selector_select(timeout): | ||
| raise TimeoutError('timed out') | ||
| if count: | ||
| blocksize = min(count - total_sent, blocksize) | ||
| if blocksize <= 0: | ||
| break | ||
| try: | ||
| sent = zerocopy_func(fileno, offset, blocksize) | ||
| except BlockingIOError: | ||
| if not timeout: | ||
| # Block until the socket is ready to send some | ||
| # data; avoids hogging CPU resources. | ||
| selector_select() | ||
| continue | ||
| except OSError as err: | ||
| if total_sent == 0: | ||
| # We can get here for different reasons, the main | ||
| # one being 'file' is not a regular mmap(2)-like | ||
| # file, in which case we'll fall back on using | ||
| # plain send(). | ||
| raise giveup_exc_type(err) | ||
| raise err from None | ||
| else: | ||
| if sent == 0: | ||
| break # EOF | ||
| offset += sent | ||
| total_sent += sent | ||
| return total_sent | ||
| finally: | ||
| if total_sent > 0 and hasattr(file, 'seek'): | ||
| file.seek(offset) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Stop leaking the selector file descriptor, and release it when done. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure that there is a leak of a file descriptor? The selectors used by |
||
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.
I would prefer to keep this code to create the selector, it's more readable.