From 896145d9d266ee2758cfcd7691238cbc1f9e1ab8 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Sat, 22 Jul 2017 13:22:54 +0200 Subject: bpo-26732: fix too many fds in processes started with the "forkserver" method (#2813) * bpo-26732: fix too many fds in processes started with the "forkserver" method A child process would inherit as many fds as the number of still-running children. * Add blurb and test comment --- Lib/test/_test_multiprocessing.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'Lib/test/_test_multiprocessing.py') diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py index 329a6d29ac..a14fa7422e 100644 --- a/Lib/test/_test_multiprocessing.py +++ b/Lib/test/_test_multiprocessing.py @@ -494,6 +494,40 @@ class _TestProcess(BaseTestCase): self.assertIs(wr(), None) self.assertEqual(q.get(), 5) + @classmethod + def _test_child_fd_inflation(self, evt, q): + q.put(test.support.fd_count()) + evt.wait() + + def test_child_fd_inflation(self): + # Number of fds in child processes should not grow with the + # number of running children. + if self.TYPE == 'threads': + self.skipTest('test not appropriate for {}'.format(self.TYPE)) + + sm = multiprocessing.get_start_method() + if sm == 'fork': + # The fork method by design inherits all fds from the parent, + # trying to go against it is a lost battle + self.skipTest('test not appropriate for {}'.format(sm)) + + N = 5 + evt = self.Event() + q = self.Queue() + + procs = [self.Process(target=self._test_child_fd_inflation, args=(evt, q)) + for i in range(N)] + for p in procs: + p.start() + + try: + fd_counts = [q.get() for i in range(N)] + self.assertEqual(len(set(fd_counts)), 1, fd_counts) + + finally: + evt.set() + for p in procs: + p.join() # # -- cgit v1.2.1