# Path: aiosmtplib/sync.py
# def shutdown_loop(loop: asyncio.AbstractEventLoop, timeout: float = 1.0) -> None:
#     """
#     Do the various dances to gently shutdown an event loop.
#     """
#     tasks = all_tasks(loop=loop)
#     if tasks:
#         for task in tasks:
#             task.cancel()
#         try:
#             loop.run_until_complete(asyncio.wait(tasks, timeout=timeout))
#         except RuntimeError:
#             pass
# 
#     if not loop.is_closed():
#         if PY36_OR_LATER:
#             loop.run_until_complete(loop.shutdown_asyncgens())
# 
#         loop.call_soon(loop.stop)
#         loop.run_forever()
#         loop.close()


import asyncio
import base64
import logging
import socket
import threading
from email.errors import HeaderParseError
from email.message import Message
from aiosmtpd.handlers import Message as MessageHandler
from aiosmtpd.smtp import MISSING
from aiosmtpd.smtp import SMTP as SMTPD
from aiosmtplib.sync import shutdown_loop
            self.server = self.loop.run_until_complete(
                self.loop.create_server(
                    self.factory,
                    host=self.hostname,
                    port=self.port,
                    ssl=self.ssl_context,
                    family=socket.AF_INET,
                )
            )
        except Exception as error:
            self._thread_exception = error
            return
        self.loop.call_soon(ready_event.set)
        self.loop.run_forever()
        self.server.close()
        self.loop.run_until_complete(self.server.wait_closed())
        self.server = None

    def start(self):
        ready_event = threading.Event()
        self._thread = threading.Thread(target=self._run, args=(ready_event,))
        self._thread.daemon = True
        self._thread.start()
        # Wait a while until the server is responding.
        ready_event.wait(self.ready_timeout)
        if self._thread_exception is not None:
            raise self._thread_exception

    def _stop(self):
        self.loop.stop()

# Path: aiosmtplib/sync.py
# def shutdown_loop(loop: asyncio.AbstractEventLoop, timeout: float = 1.0) -> None:
#     """
#     Do the various dances to gently shutdown an event loop.
#     """
#     tasks = all_tasks(loop=loop)
#     if tasks:
#         for task in tasks:
#             task.cancel()
#         try:
#             loop.run_until_complete(asyncio.wait(tasks, timeout=timeout))
#         except RuntimeError:
#             pass
# 
#     if not loop.is_closed():
#         if PY36_OR_LATER:
#             loop.run_until_complete(loop.shutdown_asyncgens())
# 
#         loop.call_soon(loop.stop)
#         loop.run_forever()
#         loop.close()


# Path: tests/smtpd.py
import asyncio
import base64
import logging
import socket
import threading
from email.errors import HeaderParseError
from email.message import Message
from aiosmtpd.handlers import Message as MessageHandler
from aiosmtpd.smtp import MISSING
from aiosmtpd.smtp import SMTP as SMTPD
from aiosmtplib.sync import shutdown_loop

            self.server = self.loop.run_until_complete(
                self.loop.create_server(
                    self.factory,
                    host=self.hostname,
                    port=self.port,
                    ssl=self.ssl_context,
                    family=socket.AF_INET,
                )
            )
        except Exception as error:
            self._thread_exception = error
            return
        self.loop.call_soon(ready_event.set)
        self.loop.run_forever()
        self.server.close()
        self.loop.run_until_complete(self.server.wait_closed())
        self.server = None

    def start(self):
        ready_event = threading.Event()
        self._thread = threading.Thread(target=self._run, args=(ready_event,))
        self._thread.daemon = True
        self._thread.start()
        # Wait a while until the server is responding.
        ready_event.wait(self.ready_timeout)
        if self._thread_exception is not None:
            raise self._thread_exception

    def _stop(self):
        self.loop.stop()

        shutdown_loop(self.loop)