bpo-43842: Fix race condition in test_logging SMTP test (GH-25436) (GH-25437)

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.

(cherry picked from commit 75ec103b3a)
This commit is contained in:
Victor Stinner 2021-04-16 16:06:38 +02:00 committed by GitHub
parent 56c76df6e8
commit e1903e11a3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 2 deletions

View file

@ -826,6 +826,7 @@ class TestSMTPServer(smtpd.SMTPServer):
self.port = self.socket.getsockname()[1] self.port = self.socket.getsockname()[1]
self._handler = handler self._handler = handler
self._thread = None self._thread = None
self._quit = False
self.poll_interval = poll_interval self.poll_interval = poll_interval
def process_message(self, peer, mailfrom, rcpttos, data): def process_message(self, peer, mailfrom, rcpttos, data):
@ -857,16 +858,18 @@ class TestSMTPServer(smtpd.SMTPServer):
:func:`select` or :func:`poll` call by :func:`select` or :func:`poll` call by
:func:`asyncore.loop`. :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): def stop(self):
""" """
Stop the thread by closing the server instance. Stop the thread by closing the server instance.
Wait for the server thread to terminate. Wait for the server thread to terminate.
""" """
self.close() self._quit = True
support.join_thread(self._thread) support.join_thread(self._thread)
self._thread = None self._thread = None
self.close()
asyncore.close_all(map=self._map, ignore_all=True) asyncore.close_all(map=self._map, ignore_all=True)

View file

@ -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.