We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Bug on almalinux 9
Hi,

I have these messages on almalinux 9/ Rocky Linux 9


[mickael@srvalmalinux ~]$ sudo  /usr/bin/dnf-automatic /etc/dnf/automatic.conf --timer
Last metadata expiration check: 2:45:52 ago on Fri Mar 10 14:38:38 2023.
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Traceback (most recent call last):
  File "/usr/bin/dnf-automatic", line 36, in <module>
    sys.exit(dnf.automatic.main.main(sys.argv[1:]))
  File "/usr/lib/python3.9/site-packages/dnf/automatic/main.py", line 342, in main
    emitters.commit()
  File "/usr/lib/python3.9/site-packages/dnf/util.py", line 526, in fn
    return list(map(call_what, self))
  File "/usr/lib/python3.9/site-packages/dnf/util.py", line 525, in call_what
    return method(*args, **kwargs)
  File "/usr/lib/python3.9/site-packages/dnf/automatic/emitter.py", line 106, in commit
    smtp = smtplib.SMTP(self._conf.email_host, timeout=300)
  File "/usr/lib64/python3.9/smtplib.py", line 255, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib64/python3.9/smtplib.py", line 341, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/lib64/python3.9/smtplib.py", line 312, in _get_socket
    return socket.create_connection((host, port), timeout,
  File "/usr/lib64/python3.9/socket.py", line 844, in create_connection
    raise err
  File "/usr/lib64/python3.9/socket.py", line 832, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `dnf/automatic/emitter.py`
Content:
```
1 # emitter.py
2 # Emitters for dnf-automatic.
3 #
4 # Copyright (C) 2014-2016 Red Hat, Inc.
5 #
6 # This copyrighted material is made available to anyone wishing to use,
7 # modify, copy, or redistribute it subject to the terms and conditions of
8 # the GNU General Public License v.2, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY expressed or implied, including the implied warranties of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
12 # Public License for more details.  You should have received a copy of the
13 # GNU General Public License along with this program; if not, write to the
14 # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 # 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
16 # source code or documentation are not subject to the GNU General Public
17 # License and may only be used or replicated with the express permission of
18 # Red Hat, Inc.
19 #
20 
21 from __future__ import absolute_import
22 from __future__ import print_function
23 from __future__ import unicode_literals
24 from dnf.i18n import _
25 import logging
26 import dnf.pycomp
27 import smtplib
28 import email.utils
29 import subprocess
30 import time
31 
32 APPLIED = _("The following updates have been applied on '%s':")
33 APPLIED_TIMESTAMP = _("Updates completed at %s")
34 AVAILABLE = _("The following updates are available on '%s':")
35 DOWNLOADED = _("The following updates were downloaded on '%s':")
36 
37 logger = logging.getLogger('dnf')
38 
39 
40 class Emitter(object):
41     def __init__(self, system_name):
42         self._applied = False
43         self._available_msg = None
44         self._downloaded = False
45         self._system_name = system_name
46         self._trans_msg = None
47 
48     def _prepare_msg(self):
49         msg = []
50         if self._applied:
51             msg.append(APPLIED % self._system_name)
52             msg.append(self._available_msg)
53             msg.append(APPLIED_TIMESTAMP % time.strftime("%c"))
54         elif self._downloaded:
55             msg.append(DOWNLOADED % self._system_name)
56             msg.append(self._available_msg)
57         elif self._available_msg:
58             msg.append(AVAILABLE % self._system_name)
59             msg.append(self._available_msg)
60         else:
61             return None
62         return '\n'.join(msg)
63 
64     def notify_applied(self):
65         assert self._available_msg
66         self._applied = True
67 
68     def notify_available(self, msg):
69         self._available_msg = msg
70 
71     def notify_downloaded(self):
72         assert self._available_msg
73         self._downloaded = True
74 
75 
76 class EmailEmitter(Emitter):
77     def __init__(self, system_name, conf):
78         super(EmailEmitter, self).__init__(system_name)
79         self._conf = conf
80 
81     def _prepare_msg(self):
82         if self._applied:
83             subj = _("Updates applied on '%s'.") % self._system_name
84         elif self._downloaded:
85             subj = _("Updates downloaded on '%s'.") % self._system_name
86         elif self._available_msg:
87             subj = _("Updates available on '%s'.") % self._system_name
88         else:
89             return None, None
90         return subj, super(EmailEmitter, self)._prepare_msg()
91 
92     def commit(self):
93         subj, body = self._prepare_msg()
94         message = dnf.pycomp.email_mime(body)
95         message.set_charset('utf-8')
96         email_from = self._conf.email_from
97         email_to = self._conf.email_to
98         message['Date'] = email.utils.formatdate()
99         message['From'] = email_from
100         message['Subject'] = subj
101         message['To'] = ','.join(email_to)
102         message['Message-ID'] = email.utils.make_msgid()
103 
104         # Send the email
105         try:
106             smtp = smtplib.SMTP(self._conf.email_host, timeout=300)
107             smtp.sendmail(email_from, email_to, message.as_string())
108             smtp.close()
109         except smtplib.SMTPException as exc:
110             msg = _("Failed to send an email via '%s': %s") % (
111                 self._conf.email_host, exc)
112             logger.error(msg)
113 
114 
115 class CommandEmitterMixIn(object):
116     """
117     Executes a desired command, and pushes data into its stdin.
118     Both data and command can be formatted according to user preference.
119     For this reason, this class expects a {str:str} dictionary as _prepare_msg
120     return value.
121     Meant for mixing with Emitter classes, as it does not define any names used
122     for formatting on its own.
123     """
124     def commit(self):
125         command_fmt = self._conf.command_format
126         stdin_fmt = self._conf.stdin_format
127         msg = self._prepare_msg()
128         # all strings passed to shell should be quoted to avoid accidental code
129         # execution
130         quoted_msg = dict((key, dnf.pycomp.shlex_quote(val))
131                           for key, val in msg.items())
132         command = command_fmt.format(**quoted_msg)
133         stdin_feed = stdin_fmt.format(**msg).encode('utf-8')
134 
135         # Execute the command
136         subp = subprocess.Popen(command, shell=True, stdin=subprocess.PIPE)
137         subp.communicate(stdin_feed)
138         subp.stdin.close()
139         if subp.wait() != 0:
140             msg = _("Failed to execute command '%s': returned %d") \
141                   % (command, subp.returncode)
142             logger.error(msg)
143 
144 
145 class CommandEmitter(CommandEmitterMixIn, Emitter):
146     def __init__(self, system_name, conf):
147         super(CommandEmitter, self).__init__(system_name)
148         self._conf = conf
149 
150     def _prepare_msg(self):
151         return {'body': super(CommandEmitter, self)._prepare_msg()}
152 
153 
154 class CommandEmailEmitter(CommandEmitterMixIn, EmailEmitter):
155     def _prepare_msg(self):
156         subject, body = super(CommandEmailEmitter, self)._prepare_msg()
157         return {'subject': subject,
158                 'body': body,
159                 'email_from': self._conf.email_from,
160                 'email_to': ' '.join(self._conf.email_to)}
161 
162 
163 class StdIoEmitter(Emitter):
164     def commit(self):
165         msg = self._prepare_msg()
166         print(msg)
167 
168 
169 class MotdEmitter(Emitter):
170     def commit(self):
171         msg = self._prepare_msg()
172         with open('/etc/motd', 'w') as fobj:
173             fobj.write(msg)
174 
175
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/dnf/automatic/emitter.py b/dnf/automatic/emitter.py
--- a/dnf/automatic/emitter.py
+++ b/dnf/automatic/emitter.py
@@ -106,7 +106,7 @@
             smtp = smtplib.SMTP(self._conf.email_host, timeout=300)
             smtp.sendmail(email_from, email_to, message.as_string())
             smtp.close()
-        except smtplib.SMTPException as exc:
+        except OSError as exc:
             msg = _("Failed to send an email via '%s': %s") % (
                 self._conf.email_host, exc)
             logger.error(msg)

{"golden_diff": "diff --git a/dnf/automatic/emitter.py b/dnf/automatic/emitter.py\n--- a/dnf/automatic/emitter.py\n+++ b/dnf/automatic/emitter.py\n@@ -106,7 +106,7 @@\n             smtp = smtplib.SMTP(self._conf.email_host, timeout=300)\n             smtp.sendmail(email_from, email_to, message.as_string())\n             smtp.close()\n-        except smtplib.SMTPException as exc:\n+        except OSError as exc:\n             msg = _(\"Failed to send an email via '%s': %s\") % (\n                 self._conf.email_host, exc)\n             logger.error(msg)\n", "issue": "Bug on almalinux 9\nHi,\r\n\r\nI have these messages on almalinux 9/ Rocky Linux 9\r\n\r\n\r\n[mickael@srvalmalinux ~]$ sudo  /usr/bin/dnf-automatic /etc/dnf/automatic.conf --timer\r\nLast metadata expiration check: 2:45:52 ago on Fri Mar 10 14:38:38 2023.\r\nRunning transaction check\r\nTransaction check succeeded.\r\nRunning transaction test\r\nTransaction test succeeded.\r\nRunning transaction\r\nTraceback (most recent call last):\r\n  File \"/usr/bin/dnf-automatic\", line 36, in <module>\r\n    sys.exit(dnf.automatic.main.main(sys.argv[1:]))\r\n  File \"/usr/lib/python3.9/site-packages/dnf/automatic/main.py\", line 342, in main\r\n    emitters.commit()\r\n  File \"/usr/lib/python3.9/site-packages/dnf/util.py\", line 526, in fn\r\n    return list(map(call_what, self))\r\n  File \"/usr/lib/python3.9/site-packages/dnf/util.py\", line 525, in call_what\r\n    return method(*args, **kwargs)\r\n  File \"/usr/lib/python3.9/site-packages/dnf/automatic/emitter.py\", line 106, in commit\r\n    smtp = smtplib.SMTP(self._conf.email_host, timeout=300)\r\n  File \"/usr/lib64/python3.9/smtplib.py\", line 255, in __init__\r\n    (code, msg) = self.connect(host, port)\r\n  File \"/usr/lib64/python3.9/smtplib.py\", line 341, in connect\r\n    self.sock = self._get_socket(host, port, self.timeout)\r\n  File \"/usr/lib64/python3.9/smtplib.py\", line 312, in _get_socket\r\n    return socket.create_connection((host, port), timeout,\r\n  File \"/usr/lib64/python3.9/socket.py\", line 844, in create_connection\r\n    raise err\r\n  File \"/usr/lib64/python3.9/socket.py\", line 832, in create_connection\r\n    sock.connect(sa)\r\nConnectionRefusedError: [Errno 111] Connection refused\r\n\n", "before_files": [{"content": "# emitter.py\n# Emitters for dnf-automatic.\n#\n# Copyright (C) 2014-2016 Red Hat, Inc.\n#\n# This copyrighted material is made available to anyone wishing to use,\n# modify, copy, or redistribute it subject to the terms and conditions of\n# the GNU General Public License v.2, or (at your option) any later version.\n# This program is distributed in the hope that it will be useful, but WITHOUT\n# ANY WARRANTY expressed or implied, including the implied warranties of\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General\n# Public License for more details.  You should have received a copy of the\n# GNU General Public License along with this program; if not, write to the\n# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA\n# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the\n# source code or documentation are not subject to the GNU General Public\n# License and may only be used or replicated with the express permission of\n# Red Hat, Inc.\n#\n\nfrom __future__ import absolute_import\nfrom __future__ import print_function\nfrom __future__ import unicode_literals\nfrom dnf.i18n import _\nimport logging\nimport dnf.pycomp\nimport smtplib\nimport email.utils\nimport subprocess\nimport time\n\nAPPLIED = _(\"The following updates have been applied on '%s':\")\nAPPLIED_TIMESTAMP = _(\"Updates completed at %s\")\nAVAILABLE = _(\"The following updates are available on '%s':\")\nDOWNLOADED = _(\"The following updates were downloaded on '%s':\")\n\nlogger = logging.getLogger('dnf')\n\n\nclass Emitter(object):\n    def __init__(self, system_name):\n        self._applied = False\n        self._available_msg = None\n        self._downloaded = False\n        self._system_name = system_name\n        self._trans_msg = None\n\n    def _prepare_msg(self):\n        msg = []\n        if self._applied:\n            msg.append(APPLIED % self._system_name)\n            msg.append(self._available_msg)\n            msg.append(APPLIED_TIMESTAMP % time.strftime(\"%c\"))\n        elif self._downloaded:\n            msg.append(DOWNLOADED % self._system_name)\n            msg.append(self._available_msg)\n        elif self._available_msg:\n            msg.append(AVAILABLE % self._system_name)\n            msg.append(self._available_msg)\n        else:\n            return None\n        return '\\n'.join(msg)\n\n    def notify_applied(self):\n        assert self._available_msg\n        self._applied = True\n\n    def notify_available(self, msg):\n        self._available_msg = msg\n\n    def notify_downloaded(self):\n        assert self._available_msg\n        self._downloaded = True\n\n\nclass EmailEmitter(Emitter):\n    def __init__(self, system_name, conf):\n        super(EmailEmitter, self).__init__(system_name)\n        self._conf = conf\n\n    def _prepare_msg(self):\n        if self._applied:\n            subj = _(\"Updates applied on '%s'.\") % self._system_name\n        elif self._downloaded:\n            subj = _(\"Updates downloaded on '%s'.\") % self._system_name\n        elif self._available_msg:\n            subj = _(\"Updates available on '%s'.\") % self._system_name\n        else:\n            return None, None\n        return subj, super(EmailEmitter, self)._prepare_msg()\n\n    def commit(self):\n        subj, body = self._prepare_msg()\n        message = dnf.pycomp.email_mime(body)\n        message.set_charset('utf-8')\n        email_from = self._conf.email_from\n        email_to = self._conf.email_to\n        message['Date'] = email.utils.formatdate()\n        message['From'] = email_from\n        message['Subject'] = subj\n        message['To'] = ','.join(email_to)\n        message['Message-ID'] = email.utils.make_msgid()\n\n        # Send the email\n        try:\n            smtp = smtplib.SMTP(self._conf.email_host, timeout=300)\n            smtp.sendmail(email_from, email_to, message.as_string())\n            smtp.close()\n        except smtplib.SMTPException as exc:\n            msg = _(\"Failed to send an email via '%s': %s\") % (\n                self._conf.email_host, exc)\n            logger.error(msg)\n\n\nclass CommandEmitterMixIn(object):\n    \"\"\"\n    Executes a desired command, and pushes data into its stdin.\n    Both data and command can be formatted according to user preference.\n    For this reason, this class expects a {str:str} dictionary as _prepare_msg\n    return value.\n    Meant for mixing with Emitter classes, as it does not define any names used\n    for formatting on its own.\n    \"\"\"\n    def commit(self):\n        command_fmt = self._conf.command_format\n        stdin_fmt = self._conf.stdin_format\n        msg = self._prepare_msg()\n        # all strings passed to shell should be quoted to avoid accidental code\n        # execution\n        quoted_msg = dict((key, dnf.pycomp.shlex_quote(val))\n                          for key, val in msg.items())\n        command = command_fmt.format(**quoted_msg)\n        stdin_feed = stdin_fmt.format(**msg).encode('utf-8')\n\n        # Execute the command\n        subp = subprocess.Popen(command, shell=True, stdin=subprocess.PIPE)\n        subp.communicate(stdin_feed)\n        subp.stdin.close()\n        if subp.wait() != 0:\n            msg = _(\"Failed to execute command '%s': returned %d\") \\\n                  % (command, subp.returncode)\n            logger.error(msg)\n\n\nclass CommandEmitter(CommandEmitterMixIn, Emitter):\n    def __init__(self, system_name, conf):\n        super(CommandEmitter, self).__init__(system_name)\n        self._conf = conf\n\n    def _prepare_msg(self):\n        return {'body': super(CommandEmitter, self)._prepare_msg()}\n\n\nclass CommandEmailEmitter(CommandEmitterMixIn, EmailEmitter):\n    def _prepare_msg(self):\n        subject, body = super(CommandEmailEmitter, self)._prepare_msg()\n        return {'subject': subject,\n                'body': body,\n                'email_from': self._conf.email_from,\n                'email_to': ' '.join(self._conf.email_to)}\n\n\nclass StdIoEmitter(Emitter):\n    def commit(self):\n        msg = self._prepare_msg()\n        print(msg)\n\n\nclass MotdEmitter(Emitter):\n    def commit(self):\n        msg = self._prepare_msg()\n        with open('/etc/motd', 'w') as fobj:\n            fobj.write(msg)\n\n", "path": "dnf/automatic/emitter.py"}], "after_files": [{"content": "# emitter.py\n# Emitters for dnf-automatic.\n#\n# Copyright (C) 2014-2016 Red Hat, Inc.\n#\n# This copyrighted material is made available to anyone wishing to use,\n# modify, copy, or redistribute it subject to the terms and conditions of\n# the GNU General Public License v.2, or (at your option) any later version.\n# This program is distributed in the hope that it will be useful, but WITHOUT\n# ANY WARRANTY expressed or implied, including the implied warranties of\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General\n# Public License for more details.  You should have received a copy of the\n# GNU General Public License along with this program; if not, write to the\n# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA\n# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the\n# source code or documentation are not subject to the GNU General Public\n# License and may only be used or replicated with the express permission of\n# Red Hat, Inc.\n#\n\nfrom __future__ import absolute_import\nfrom __future__ import print_function\nfrom __future__ import unicode_literals\nfrom dnf.i18n import _\nimport logging\nimport dnf.pycomp\nimport smtplib\nimport email.utils\nimport subprocess\nimport time\n\nAPPLIED = _(\"The following updates have been applied on '%s':\")\nAPPLIED_TIMESTAMP = _(\"Updates completed at %s\")\nAVAILABLE = _(\"The following updates are available on '%s':\")\nDOWNLOADED = _(\"The following updates were downloaded on '%s':\")\n\nlogger = logging.getLogger('dnf')\n\n\nclass Emitter(object):\n    def __init__(self, system_name):\n        self._applied = False\n        self._available_msg = None\n        self._downloaded = False\n        self._system_name = system_name\n        self._trans_msg = None\n\n    def _prepare_msg(self):\n        msg = []\n        if self._applied:\n            msg.append(APPLIED % self._system_name)\n            msg.append(self._available_msg)\n            msg.append(APPLIED_TIMESTAMP % time.strftime(\"%c\"))\n        elif self._downloaded:\n            msg.append(DOWNLOADED % self._system_name)\n            msg.append(self._available_msg)\n        elif self._available_msg:\n            msg.append(AVAILABLE % self._system_name)\n            msg.append(self._available_msg)\n        else:\n            return None\n        return '\\n'.join(msg)\n\n    def notify_applied(self):\n        assert self._available_msg\n        self._applied = True\n\n    def notify_available(self, msg):\n        self._available_msg = msg\n\n    def notify_downloaded(self):\n        assert self._available_msg\n        self._downloaded = True\n\n\nclass EmailEmitter(Emitter):\n    def __init__(self, system_name, conf):\n        super(EmailEmitter, self).__init__(system_name)\n        self._conf = conf\n\n    def _prepare_msg(self):\n        if self._applied:\n            subj = _(\"Updates applied on '%s'.\") % self._system_name\n        elif self._downloaded:\n            subj = _(\"Updates downloaded on '%s'.\") % self._system_name\n        elif self._available_msg:\n            subj = _(\"Updates available on '%s'.\") % self._system_name\n        else:\n            return None, None\n        return subj, super(EmailEmitter, self)._prepare_msg()\n\n    def commit(self):\n        subj, body = self._prepare_msg()\n        message = dnf.pycomp.email_mime(body)\n        message.set_charset('utf-8')\n        email_from = self._conf.email_from\n        email_to = self._conf.email_to\n        message['Date'] = email.utils.formatdate()\n        message['From'] = email_from\n        message['Subject'] = subj\n        message['To'] = ','.join(email_to)\n        message['Message-ID'] = email.utils.make_msgid()\n\n        # Send the email\n        try:\n            smtp = smtplib.SMTP(self._conf.email_host, timeout=300)\n            smtp.sendmail(email_from, email_to, message.as_string())\n            smtp.close()\n        except OSError as exc:\n            msg = _(\"Failed to send an email via '%s': %s\") % (\n                self._conf.email_host, exc)\n            logger.error(msg)\n\n\nclass CommandEmitterMixIn(object):\n    \"\"\"\n    Executes a desired command, and pushes data into its stdin.\n    Both data and command can be formatted according to user preference.\n    For this reason, this class expects a {str:str} dictionary as _prepare_msg\n    return value.\n    Meant for mixing with Emitter classes, as it does not define any names used\n    for formatting on its own.\n    \"\"\"\n    def commit(self):\n        command_fmt = self._conf.command_format\n        stdin_fmt = self._conf.stdin_format\n        msg = self._prepare_msg()\n        # all strings passed to shell should be quoted to avoid accidental code\n        # execution\n        quoted_msg = dict((key, dnf.pycomp.shlex_quote(val))\n                          for key, val in msg.items())\n        command = command_fmt.format(**quoted_msg)\n        stdin_feed = stdin_fmt.format(**msg).encode('utf-8')\n\n        # Execute the command\n        subp = subprocess.Popen(command, shell=True, stdin=subprocess.PIPE)\n        subp.communicate(stdin_feed)\n        subp.stdin.close()\n        if subp.wait() != 0:\n            msg = _(\"Failed to execute command '%s': returned %d\") \\\n                  % (command, subp.returncode)\n            logger.error(msg)\n\n\nclass CommandEmitter(CommandEmitterMixIn, Emitter):\n    def __init__(self, system_name, conf):\n        super(CommandEmitter, self).__init__(system_name)\n        self._conf = conf\n\n    def _prepare_msg(self):\n        return {'body': super(CommandEmitter, self)._prepare_msg()}\n\n\nclass CommandEmailEmitter(CommandEmitterMixIn, EmailEmitter):\n    def _prepare_msg(self):\n        subject, body = super(CommandEmailEmitter, self)._prepare_msg()\n        return {'subject': subject,\n                'body': body,\n                'email_from': self._conf.email_from,\n                'email_to': ' '.join(self._conf.email_to)}\n\n\nclass StdIoEmitter(Emitter):\n    def commit(self):\n        msg = self._prepare_msg()\n        print(msg)\n\n\nclass MotdEmitter(Emitter):\n    def commit(self):\n        msg = self._prepare_msg()\n        with open('/etc/motd', 'w') as fobj:\n            fobj.write(msg)\n\n", "path": "dnf/automatic/emitter.py"}]}