Commits

sbt  committed 118b301 Draft

Fix ResourceWarning for unclosed socket.

  • Participants
  • Parent commits 089b298
  • Branches spawn

Comments (0)

Files changed (1)

File Lib/multiprocessing/popen_forkserver.py

     def prepare_new_process(self, fds):
         if len(fds) + 3 >= self.MAXFDS_TO_SEND:
             raise ValueError('too many fds')
-        client = socket.socket(socket.AF_UNIX)
-        client.connect(self._address)
-        parent_r, child_w = os.pipe()
-        child_r, parent_w = os.pipe()
-        allfds = [child_r, child_w, self._alive_w]
-        allfds += fds
-        try:
-            reduction.sendfds(client, allfds)
-            return parent_r, parent_w
-        except:
-            os.close(parent_r)
-            os.close(parent_w)
-            raise
-        finally:
-            os.close(child_r)
-            os.close(child_w)
+        with socket.socket(socket.AF_UNIX) as client:
+            client.connect(self._address)
+            parent_r, child_w = os.pipe()
+            child_r, parent_w = os.pipe()
+            allfds = [child_r, child_w, self._alive_w]
+            allfds += fds
+            try:
+                reduction.sendfds(client, allfds)
+                return parent_r, parent_w
+            except:
+                os.close(parent_r)
+                os.close(parent_w)
+                raise
+            finally:
+                os.close(child_r)
+                os.close(child_w)
 
     def read_ulong(self, fd):
         data = b''