You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
test_socket leaks file descriptors on macOS #80931
I tried reproducing this and I can't reproduce it in Ubuntu. Mac OS seems to fail with leaked references which I think is unrelated to the above Azure failure. The Mac issue is open (bpo-35092 reported by Victor closed as duplicate of bpo-23828) but it was about warning though running with regrtest seems to leak references in addition to warnings.
Ubuntu build :
karthi@ubuntu-s-1vcpu-1gb-blr1-01:~/cpython$ ./python -m test -R 3:3 test_socket
Run tests sequentially
0:00:00 load avg: 0.01 [1/1] test_socket
beginning 6 repetitions
123456
......
test_socket passed in 3 min 7 sec
== Tests result: SUCCESS ==
1 test OK.
Total duration: 3 min 7 sec
Tests result: SUCCESS
Mac OS build (Mac OS 10.10.4 (14E46))
➜ cpython git:(master) ./python.exe -m test -R 3:3 test_socket
Run tests sequentially
0:00:00 load avg: 2.00 [1/1] test_socket
beginning 6 repetitions
123456
/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/test/test_socket.py:2419: RuntimeWarning: received malformed or improperly-truncated ancillary data
result = sock.recvmsg(bufsize, \*args)
/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/test/test_socket.py:2510: RuntimeWarning: received malformed or improperly-truncated ancillary data
result = sock.recvmsg_into([buf], \*args)
./Users/karthikeyansingaravelan/stuff/python/cpython/Lib/test/test_socket.py:2419: RuntimeWarning: received malformed or improperly-truncated ancillary data
result = sock.recvmsg(bufsize, \*args)
/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/test/test_socket.py:2510: RuntimeWarning: received malformed or improperly-truncated ancillary data
result = sock.recvmsg_into([buf], \*args)
./Users/karthikeyansingaravelan/stuff/python/cpython/Lib/test/test_socket.py:2419: RuntimeWarning: received malformed or improperly-truncated ancillary data
result = sock.recvmsg(bufsize, \*args)
/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/test/test_socket.py:2510: RuntimeWarning: received malformed or improperly-truncated ancillary data
result = sock.recvmsg_into([buf], \*args)
./Users/karthikeyansingaravelan/stuff/python/cpython/Lib/test/test_socket.py:2419: RuntimeWarning: received malformed or improperly-truncated ancillary data
result = sock.recvmsg(bufsize, \*args)
/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/test/test_socket.py:2510: RuntimeWarning: received malformed or improperly-truncated ancillary data
result = sock.recvmsg_into([buf], \*args)
./Users/karthikeyansingaravelan/stuff/python/cpython/Lib/test/test_socket.py:2419: RuntimeWarning: received malformed or improperly-truncated ancillary data
result = sock.recvmsg(bufsize, \*args)
/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/test/test_socket.py:2510: RuntimeWarning: received malformed or improperly-truncated ancillary data
result = sock.recvmsg_into([buf], \*args)
./Users/karthikeyansingaravelan/stuff/python/cpython/Lib/test/test_socket.py:2419: RuntimeWarning: received malformed or improperly-truncated ancillary data
result = sock.recvmsg(bufsize, \*args)
/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/test/test_socket.py:2510: RuntimeWarning: received malformed or improperly-truncated ancillary data
result = sock.recvmsg_into([buf], \*args)
.
test_socket leaked [20, 20, 20] file descriptors, sum=60
test_socket failed in 2 min 31 sec
== Tests result: FAILURE ==
1 test failed:
test_socket
Total duration: 2 min 31 sec
Tests result: FAILURE
bpo-35092 reported by Victor closed as duplicate of bpo-23828
Sorry, it was reported by Pablo in bpo-35092. I haven't seen any old reports of this in search. The other buildbots on Ubuntu run fine so not sure if it's only for Azure and Ubuntu for the PR since master is green on Azure and the buildbots so feel free to close this if needed.
The test failure is reproducible on the PR 12271 merging master branch on Ubuntu with -R 3:3 test_socket. Sorry, I tried it initially on master thinking it was a master branch problem. I don't see C related code in the PR. There was a merge commit [0] after which the failure is consistently reproducible. I tried reverting parts of the PR to narrow down the failure and happened to come across the below statement in the diff where the test object and elapsed test for time are stored to be printed later. Commenting out the call makes tests pass for test_socket and much more baffling is that changing the code to use list instead of tuple causes the error to go away. Also I tried changing append to use extend or just to append test object instead of a tuple of test object and elapsed time which all pass. Instead of using test object in tuple just using (object(), elapsed) as a tuple also causes failure. I guess there is something with tuples here but it doesn't make much sense.
On trying to bisect the merge commit I have narrowed down the below commits where the PR passes with tuple itself and then fails. In between the success and failure commit there were some changes made to hunterleaks and socket.create_server utility was added.
The below patch on the PR to use list causes the tests to pass but I am highly confused over how changing to tuple to list fixes this and this might potentially be hiding a bug.
$ git diff
diff --git a/Lib/unittest/result.py b/Lib/unittest/result.py
index 273ca3beef..b8fd714679 100644
--- a/Lib/unittest/result.py
+++ b/Lib/unittest/result.py
@@ -160,7 +160,7 @@ class TestResult(object):
def addDuration(self, test, elapsed):
"""Called when a test finished to run, regardless of its outcome."""
- self.collectedDurations.append((test, elapsed))+ self.collectedDurations.append([test, elapsed])
defwasSuccessful(self):
"""Tells whether or not this result was a success."""
There is some file descriptors that are created by newFDs() but they are supposed to be cleaned up, maybe it is some race condition as I see that test_socket create a thread for each test?
The linked draft PR avoids the FD leaks by effectively skipping the problematic tests when detecting a refleak hunting run on macOS.
The way the PR detects such a run is gross and shouldn't be merged. If the general approach is OK (that is, detect a refleak hunting run and skip tests) there should be a better interface.
Something we could do:
Have a test.support.hunting_for_refleaks function that checks a global flag in that module
Have test.libregrtest.refleaks.runtest_refleak set this flag while running tests
The integration into the test functions is also not optimal, could be make nicer with two new decorators:
The latter mirrors skipWithClientIf, but would have a different implementation because the skipForRefleakingHuntingIf decorator needs to do something at test time instead of at load time.
Testing for a ref leaking run is not integrated into test.libregrtest and test.support.refleak_helper
Using decorators to skip tests while hunting for refleaks
The decorators are still part of test_socket, but that's primarily because I mirrored the client_skip functionality as sketched in my previous message. Maybe it is better to move the skip function to test.support.refleak_helper and have a new decorator in test_socket that replaces the client_skip logic.
thanks for the @-mention on the other issue @corona10 !
just wanted to mention that this buildbot is now covering refleaks on macOS. it was originally intended to provide free-threaded macOS refleaks coverage, but since it was consistently failing (due to this issue), I changed it to cover the default (with GIL) configuration until it is fixed.
this means you can invoke this builder on PRs using the correct incantation :)
I don't think it's related, but worth calling out that the ASAN macOS builder has also been failing consistently (3 failing tests: test_cmd_line test_posix test_subprocess). I can file a separate issue (or search for an existing one).
Well, it's not reproducible on my local machine.
But all failures are about missing file descriptors; it could be a local system issue.
Apparently, it's worth creating an issue on the build bot repo.
➜ cpython git:(main) ✗ ./python.exe -m test test_cmd_line test_posix test_subprocess -R 3:3
Using random seed: 2522577964
Raised RLIMIT_NOFILE: 256 -> 1024
0:00:00 load avg: 2.02 Run 3 tests sequentially
0:00:00 load avg: 2.02 [1/3] test_cmd_line
beginning 6 repetitions
123456
......
0:00:22 load avg: 2.80 [2/3] test_posix
beginning 6 repetitions
123456
......
0:00:30 load avg: 2.73 [3/3] test_subprocess
beginning 6 repetitions
123456
......
test_subprocess passed in 1 min 51 sec
== Tests result: SUCCESS ==
All 3 tests OK.
Total duration: 2 min 22 sec
Total tests: run=485 skipped=85
Total test files: run=3/3
Result: SUCCESS
Well, it's not reproducible on my local machine. But all failures are about missing file descriptors; it could be a local system issue. Apparently, it's worth creating an issue on the build bot repo.
...
I've filed an issue with Apple about this: FB1354223
The attached program reproduces the issue. When run without arguments it sends and receives two file descriptors, when run with arguments the receive buffer is too small and the proces leaks file descriptors.
/* * This program sends and receives a number of file descriptors between * a socket pair. * * To build: * $ cc -o repro repro.c * * There are two options to run: * * - Run with a buffer that's the right size to receive file descriptors: * * $ ./repro * * - Run with a too small buffer: * * $ ./repro arg * * The former works as expected, the latter leaks a file descriptor. */#include<dirent.h>#include<stdio.h>#include<sys/socket.h>#include<fcntl.h>#include<string.h>#include<unistd.h>#defineNUM_FD 2
intfd_count()
{
intcount=0;
DIR*dir;
structdirent*ent;
dir=opendir("/dev/fd");
if (dir==NULL) {
perror("opendir");
return-1;
}
for (ent=readdir(dir); ent!=NULL; ent=readdir(dir)) {
count+=1;
}
closedir(dir);
/* opendir uses a file descriptor */count-=1;
returncount;
}
intmain(intargc, char**argv)
{
intfd[2];
intr;
intdata_fds[NUM_FD];
intbefore, after;
intdata;
intxit=0;
/* * Setup: Create a socket pair for sending an receving * and open a file to send. */r=socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
if (r==-1) {
perror("socketpair");
return1;
}
for (inti=0; i<NUM_FD; i++) {
data_fds[i] =open("/dev/zero", O_RDONLY);
if (data_fds[i] ==-1) {
perror("open data_fd");
return1;
}
}
before=fd_count();
if (before==-1) {
return1;
}
/* * Send file desciptors using SCM_RIGHTS */
{
structmsghdrmsg= { 0 };
structcmsghdr*cmsg;
union {
charbuf[CMSG_SPACE(sizeof(data_fds))];
structcmsghdralign;
} u;
structiovecvec;
msg.msg_name=NULL;
msg.msg_namelen=0;
msg.msg_control=u.buf;
msg.msg_controllen=sizeof(u.buf);
data=1234;
msg.msg_iov=&vec;
msg.msg_iovlen=1;
vec.iov_base=&data;
vec.iov_len=sizeof(data);
cmsg=CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level=SOL_SOCKET;
cmsg->cmsg_type=SCM_RIGHTS;
cmsg->cmsg_len=CMSG_LEN(sizeof(int) *NUM_FD);
memcpy((int*)CMSG_DATA(cmsg), data_fds, NUM_FD*sizeof(int));
r=sendmsg(fd[0], &msg, 0);
if (r==-1) {
perror("sendmsg");
return1;
}
}
/* * Receive file descriptors */
{
structmsghdrrecv_msg= { 0 };
intrecv_fds[NUM_FD];
intrecv_data;
structiovecrecv_vec;
structcmsghdr*recv_cmsg;
charrecv_ch;
union {
charbuf[CMSG_SPACE(sizeof(recv_fds))];
structcmsghdralign;
} recv_u;
union {
charbuf[0];
structcmsghdralign;
} recv_u_small;
recv_msg.msg_name=NULL;
recv_msg.msg_namelen=0;
recv_msg.msg_iov=&recv_vec;
recv_msg.msg_iovlen=1;
recv_vec.iov_base=&recv_data;
recv_vec.iov_len=sizeof(recv_data);
if (argc==1) {
recv_msg.msg_control=recv_u.buf;
recv_msg.msg_controllen=sizeof(recv_u.buf);
} else {
recv_msg.msg_control=recv_u_small.buf;
recv_msg.msg_controllen=sizeof(recv_u_small.buf);
}
r=recvmsg(fd[1], &recv_msg, 0);
if (r==-1) {
perror("recvmsg");
return1;
}
recv_cmsg=CMSG_FIRSTHDR(&recv_msg);
if (recv_cmsg==NULL) {
fputs("No control message received\n", stderr);
xit=3;
goto done;
}
if (recv_cmsg->cmsg_level!=SOL_SOCKET||recv_cmsg->cmsg_type!=SCM_RIGHTS) {
fputs("Unexpected control message", stderr);
xit=3;
goto done;
}
memcpy(recv_fds, CMSG_DATA(recv_cmsg), sizeof(recv_fds));
fputs("Received fds", stdout);
for (inti=0; i<NUM_FD; i++) {
printf("%c %d", i==0?':':',', recv_fds[i]);
close(recv_fds[i]);
}
fputs("\n", stdout);
}
/* Test assertions */done:
after=fd_count();
if (after==-1) {
return1;
}
if (after!=before) {
printf("Number of open descriptors changed: before=%d after=%d\n", before, after);
return2;
}
returnxit;
}
Activity
tirkarthi commentedon Apr 29, 2019
This PR #12271 has consistent build failures in test_socket even after merging the master branch.
Sample build failure : https://dev.azure.com/Python/cpython/_build/results?buildId=41411
I tried reproducing this and I can't reproduce it in Ubuntu. Mac OS seems to fail with leaked references which I think is unrelated to the above Azure failure. The Mac issue is open (bpo-35092 reported by Victor closed as duplicate of bpo-23828) but it was about warning though running with regrtest seems to leak references in addition to warnings.
Ubuntu build :
Mac OS build (Mac OS 10.10.4 (14E46))
tirkarthi commentedon Apr 29, 2019
Sorry, it was reported by Pablo in bpo-35092. I haven't seen any old reports of this in search. The other buildbots on Ubuntu run fine so not sure if it's only for Azure and Ubuntu for the PR since master is green on Azure and the buildbots so feel free to close this if needed.
[-]test_socket failed (env changed) on Azure pipeline[/-][+]test_socket leaks file descriptors on macOS[/+]tirkarthi commentedon Apr 30, 2019
The test failure is reproducible on the PR 12271 merging master branch on Ubuntu with -R 3:3 test_socket. Sorry, I tried it initially on master thinking it was a master branch problem. I don't see C related code in the PR. There was a merge commit [0] after which the failure is consistently reproducible. I tried reverting parts of the PR to narrow down the failure and happened to come across the below statement in the diff where the test object and elapsed test for time are stored to be printed later. Commenting out the call makes tests pass for test_socket and much more baffling is that changing the code to use list instead of tuple causes the error to go away. Also I tried changing append to use extend or just to append test object instead of a tuple of test object and elapsed time which all pass. Instead of using test object in tuple just using (object(), elapsed) as a tuple also causes failure. I guess there is something with tuples here but it doesn't make much sense.
On trying to bisect the merge commit I have narrowed down the below commits where the PR passes with tuple itself and then fails. In between the success and failure commit there were some changes made to hunterleaks and socket.create_server utility was added.
f66e336 (success)
58721a9 (success)
2b00db6 (fails)
# Sample failure on PR's HEAD on Ubuntu
The below patch on the PR to use list causes the tests to pass but I am highly confused over how changing to tuple to list fixes this and this might potentially be hiding a bug.
def addDuration(self, test, elapsed): """Called when a test finished to run, regardless of its outcome.""" - self.collectedDurations.append((test, elapsed)) + self.collectedDurations.append([test, elapsed])[0] 3c4af91
remilapeyre commentedon Jun 7, 2020
I think I stumbled on this too, I think the test that causes some issue is testCmsgTrunc2Int:
There is some file descriptors that are created by newFDs() but they are supposed to be cleaned up, maybe it is some race condition as I see that test_socket create a thread for each test?
Eclips4 commentedon Aug 12, 2023
This still reproducible on current main and seems that hasn't been fixed yet.
Trace:
Refleaks:
19 remaining items
ronaldoussoren commentedon Jan 14, 2024
The linked draft PR avoids the FD leaks by effectively skipping the problematic tests when detecting a refleak hunting run on macOS.
The way the PR detects such a run is gross and shouldn't be merged. If the general approach is OK (that is, detect a refleak hunting run and skip tests) there should be a better interface.
Something we could do:
test.support.hunting_for_refleaksfunction that checks a global flag in that moduletest.libregrtest.refleaks.runtest_refleakset this flag while running testsThe integration into the test functions is also not optimal, could be make nicer with two new decorators:
The latter mirrors
skipWithClientIf, but would have a different implementation because theskipForRefleakingHuntingIfdecorator needs to do something at test time instead of at load time.ronaldoussoren commentedon Jan 14, 2024
I've updated the PR:
test.libregrtestandtest.support.refleak_helperThe decorators are still part of
test_socket, but that's primarily because I mirrored theclient_skipfunctionality as sketched in my previous message. Maybe it is better to move the skip function totest.support.refleak_helperand have a new decorator intest_socketthat replaces theclient_skiplogic.itamaro commentedon Jan 14, 2024
thanks for the @-mention on the other issue @corona10 !
just wanted to mention that this buildbot is now covering refleaks on macOS. it was originally intended to provide free-threaded macOS refleaks coverage, but since it was consistently failing (due to this issue), I changed it to cover the default (with GIL) configuration until it is fixed.
this means you can invoke this builder on PRs using the correct incantation :)
I don't think it's related, but worth calling out that the ASAN macOS builder has also been failing consistently (3 failing tests: test_cmd_line test_posix test_subprocess). I can file a separate issue (or search for an existing one).
corona10 commentedon Jan 15, 2024
@itamaro
Well, it's not reproducible on my local machine.
But all failures are about missing file descriptors; it could be a local system issue.
Apparently, it's worth creating an issue on the build bot repo.
itamaro commentedon Jan 15, 2024
python/buildmaster-config#450
Merge branch 'main' into pythongh-80931
gh-80931: Skip some socket tests while hunting for refleaks on macOS (#…
ronaldoussoren commentedon Jan 21, 2024
I've filed an issue with Apple about this: FB1354223
The attached program reproduces the issue. When run without arguments it sends and receives two file descriptors, when run with arguments the receive buffer is too small and the proces leaks file descriptors.
pythongh-80931: Skip some socket tests while hunting for refleaks on …
pythongh-80931: Skip some socket tests while hunting for refleaks on …
pythongh-80931: Skip some socket tests while hunting for refleaks on …
pythongh-80931: Skip some socket tests while hunting for refleaks on …
pythongh-80931: Skip some socket tests while hunting for refleaks on …
vstinner commentedon Feb 27, 2025
What's the status of this issue?