This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Possible fd leak in socketmodule
Type: resource usage Stage: resolved
Components: Extension Modules Versions: Python 3.4
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: christian.heimes, pitrou, vstinner
Priority: normal Keywords: needs review, patch

Created on 2013-07-22 12:34 by christian.heimes, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
closesock3.patch christian.heimes, 2013-07-22 16:56 review
accept.patch vstinner, 2013-07-26 21:17 review
Messages (13)
msg193530 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-07-22 12:34
Coverity claims that sock_accept() may leak a fd. I have been starring at the code for a while and I'm still not sure if Coverity is right. The macros make the code paths hard to follow. The attached patch is simple and should not be a performance issue.

http://hg.python.org/cpython/file/01597384531f/Modules/socketmodule.c#l1965

6. open_fn: Returning handle opened by function "accept(int, __SOCKADDR_ARG, socklen_t * restrict)".
7. var_assign: Assigning: "newfd" = handle returned from "accept(s->sock_fd, __SOCKADDR_ARG({ .__sockaddr__ = &addrbuf.sa}), &addrlen)".
CID 983312 (#1 of 1): Resource leak (RESOURCE_LEAK)14. overwrite_var: Overwriting handle "newfd" in "newfd = accept(s->sock_fd, __SOCKADDR_ARG({ .__sockaddr__ = &addrbuf.sa}), &addrlen)" leaks the handle.
1969        newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
msg193531 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-07-22 13:06
What if you add "errno = 0" at the beginning of the function? Perhaps Coverity is confused by the fact that we use CHECK_ERRNO() to infer whether accept() succeeded or not, rather than simply checking the return value.

I don't like your patch, because it adds a code path that would really be a logic error and therefore should not pass silently.
msg193534 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-07-22 13:58
Coverity may not understand the interaction with errno at all. Or it may simple point out that some operating systems are not standard conform and our assumption is not universally true.

Here is a patch that raises an exception when newfd != INVALID_SOCKET.

I would not mind if we close the issue with "false positive". The code seems to work just fine on all major platforms -- or we haven't seen the issue in the wild yet.
msg193535 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-07-22 14:00
> Coverity may not understand the interaction with errno at all. Or it
> may simple point out that some operating systems are not standard
> conform and our assumption is not universally true.

Have you tried to reset errno at the beginning of the function?
msg193537 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-07-22 14:18
Am 22.07.2013 16:00, schrieb Antoine Pitrou:
> Have you tried to reset errno at the beginning of the function?

It doesn't affect Coverity's report:

     BEGIN_SELECT_LOOP(s)
     Py_BEGIN_ALLOW_THREADS
+    errno = 0;
     timeout = internal_select_ex(s, 0, interval);
     if (!timeout) {
         newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
msg193549 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-07-22 16:56
This patch does the trick.
msg193753 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2013-07-26 21:17
http://bugs.python.org/file31016/closesock3.patch looks overkill. For example, BEGIN_SELECT_LOOP already sets errno to 0.

Here is a simpler patch.
msg193754 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2013-07-26 21:23
@pitrou: Does accept.patch look correct?

I don't understand why we would call accept() more than once.
msg194579 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-08-06 21:11
> I don't understand why we would call accept() more than once.

Because of EINTR?
msg200722 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-10-21 08:50
Shall I commit my patch or shall I close the coverity issue as intentionally?
msg203167 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-11-17 14:21
The patch has been languishing for some time. Commit or close?
msg203171 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-11-17 14:26
If none of use really understands the problem Coverity is trying to warn us about, I'd say "let languish or close".
msg203195 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-11-17 15:51
OK, I'm rejecting it.
History
Date User Action Args
2022-04-11 14:57:48adminsetgithub: 62728
2013-11-17 15:51:41christian.heimessetstatus: open -> closed
resolution: rejected
messages: + msg203195

stage: patch review -> resolved
2013-11-17 14:26:05pitrousetmessages: + msg203171
2013-11-17 14:21:22christian.heimessetmessages: + msg203167
2013-10-21 08:50:53christian.heimessetmessages: + msg200722
versions: - Python 3.3
2013-08-06 21:11:19pitrousetmessages: + msg194579
2013-08-06 11:53:32serhiy.storchakasetnosy: - serhiy.storchaka
2013-07-26 21:23:07vstinnersetmessages: + msg193754
2013-07-26 21:17:42vstinnersetfiles: + accept.patch
nosy: + vstinner
messages: + msg193753

2013-07-22 16:56:12christian.heimessetfiles: + closesock3.patch

messages: + msg193549
2013-07-22 16:55:17christian.heimessetfiles: - closesock2.patch
2013-07-22 16:55:13christian.heimessetfiles: - closesock.patch
2013-07-22 14:18:42christian.heimessetmessages: + msg193537
2013-07-22 14:00:19pitrousetmessages: + msg193535
2013-07-22 13:58:01christian.heimessetfiles: + closesock2.patch

messages: + msg193534
2013-07-22 13:06:35pitrousetmessages: + msg193531
2013-07-22 12:39:12christian.heimessetnosy: + pitrou, serhiy.storchaka
2013-07-22 12:34:56christian.heimescreate