]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-43842: Fix race condition in test_logging SMTP test (GH-25436)
authorVictor Stinner <vstinner@python.org>
Fri, 16 Apr 2021 12:32:01 +0000 (14:32 +0200)
committerGitHub <noreply@github.com>
Fri, 16 Apr 2021 12:32:01 +0000 (14:32 +0200)
Fix a race condition in the SMTP test of test_logging. Don't close a
file descriptor (socket) from a different thread while
asyncore.loop() is polling the file descriptor.

Lib/test/test_logging.py
Misc/NEWS.d/next/Tests/2021-04-16-14-07-40.bpo-43842.w60GAH.rst [new file with mode: 0644]

index 6179e28759cceba8dd917768ceb51aae7271b5fa..bb31e29ca7691180abfc75cd8da7ec5c8bbb193c 100644 (file)
@@ -829,6 +829,7 @@ class TestSMTPServer(smtpd.SMTPServer):
         self.port = self.socket.getsockname()[1]
         self._handler = handler
         self._thread = None
+        self._quit = False
         self.poll_interval = poll_interval
 
     def process_message(self, peer, mailfrom, rcpttos, data):
@@ -860,16 +861,18 @@ class TestSMTPServer(smtpd.SMTPServer):
                               :func:`select` or :func:`poll` call by
                               :func:`asyncore.loop`.
         """
-        asyncore.loop(poll_interval, map=self._map)
+        while not self._quit:
+            asyncore.loop(poll_interval, map=self._map, count=1)
 
     def stop(self):
         """
         Stop the thread by closing the server instance.
         Wait for the server thread to terminate.
         """
-        self.close()
+        self._quit = True
         threading_helper.join_thread(self._thread)
         self._thread = None
+        self.close()
         asyncore.close_all(map=self._map, ignore_all=True)
 
 
diff --git a/Misc/NEWS.d/next/Tests/2021-04-16-14-07-40.bpo-43842.w60GAH.rst b/Misc/NEWS.d/next/Tests/2021-04-16-14-07-40.bpo-43842.w60GAH.rst
new file mode 100644 (file)
index 0000000..5b4a120
--- /dev/null
@@ -0,0 +1,4 @@
+Fix a race condition in the SMTP test of test_logging. Don't close a file
+descriptor (socket) from a different thread while asyncore.loop() is polling
+the file descriptor.
+Patch by Victor Stinner.