saltstack / salt

Software to automate the management and configuration of any infrastructure or application at scale. Install Salt from the Salt package repositories here:
https://docs.saltproject.io/salt/install-guide/en/latest/
Apache License 2.0
14.19k stars 5.48k forks source link

[BUG] ssl_handshake raises OSError (Python Issue 31122) #62047

Open recmanj opened 2 years ago

recmanj commented 2 years ago

Description When using TCP transport (SSL) the master logs might get flooded by these errors:

2022-05-11 00:29:32,231 [tornado.general  :555 ][ERROR   ][1707] Uncaught exception, closing connection.
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/salt/ext/tornado/iostream.py", line 520, in _handle_events
    self._handle_read()
  File "/usr/lib/python3.6/site-packages/salt/ext/tornado/iostream.py", line 1396, in _handle_read
    self._do_ssl_handshake()
  File "/usr/lib/python3.6/site-packages/salt/ext/tornado/iostream.py", line 1316, in _do_ssl_handshake
    self.socket.do_handshake()
  File "/usr/lib64/python3.6/ssl.py", line 1036, in do_handshake
    self._sslobj.do_handshake()
  File "/usr/lib64/python3.6/ssl.py", line 648, in do_handshake
    self._sslobj.do_handshake()
OSError: [Errno 0] Error
2022-05-11 00:29:32,232 [tornado.application:640 ][ERROR   ][1707] Exception in callback None
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/salt/ext/tornado/ioloop.py", line 890, in start
    handler_func(fd_obj, events)
  File "/usr/lib/python3.6/site-packages/salt/ext/tornado/stack_context.py", line 278, in null_wrapper
    return fn(*args, **kwargs)
  File "/usr/lib/python3.6/site-packages/salt/ext/tornado/iostream.py", line 520, in _handle_events
    self._handle_read()
  File "/usr/lib/python3.6/site-packages/salt/ext/tornado/iostream.py", line 1396, in _handle_read
    self._do_ssl_handshake()
  File "/usr/lib/python3.6/site-packages/salt/ext/tornado/iostream.py", line 1316, in _do_ssl_handshake
    self.socket.do_handshake()
  File "/usr/lib64/python3.6/ssl.py", line 1036, in do_handshake
    self._sslobj.do_handshake()
  File "/usr/lib64/python3.6/ssl.py", line 648, in do_handshake
    self._sslobj.do_handshake()
OSError: [Errno 0] Error

Errno 0 is being raised incorrectly here and it should be fixed in newer versions of Python (https://bugs.python.org/issue31122) but the bug is present on Python 3.6 which is still the default in RHEL 8 for example.

I am not entirely sure if this fix only prevents logs being flooded or even fixes some memory leak a little bit as the memory usage of salt-master became more stable after deploying the fix.

This is basically just a copy of https://github.com/tornadoweb/tornado/pull/2518/files#diff-15c0dbe9be844d2630a4a3fc39215fcbb1734d09ff8c379b0cdb951c82ad989b.

Setup Salt-master with TCP transport and SSL enabled

Steps to Reproduce the behavior Should happen when salt-minion closes connection unexpectedly.

Expected behavior The exception gets caught.

Versions Report

Salt Version:
          Salt: 3004.1

Dependency Versions:
          cffi: 1.11.5
      cherrypy: Not Installed
      dateutil: 2.6.1
     docker-py: Not Installed
         gitdb: 4.0.1
     gitpython: 3.0.9
        Jinja2: 2.10.1
       libgit2: Not Installed
      M2Crypto: 0.35.2
          Mako: Not Installed
       msgpack: 0.6.2
  msgpack-pure: Not Installed
  mysql-python: Not Installed
     pycparser: 2.14
      pycrypto: Not Installed
  pycryptodome: Not Installed
        pygit2: Not Installed
        Python: 3.6.8 (default, Sep  9 2021, 07:49:02)
  python-gnupg: Not Installed
        PyYAML: 3.12
         PyZMQ: 19.0.0
         smmap: 3.0.1
       timelib: Not Installed
       Tornado: 4.5.3
           ZMQ: 4.3.4

System Versions:
          dist: rhel 8.5 Ootpa
        locale: UTF-8
       machine: x86_64
       release: 4.18.0-348.23.1.el8_5.x86_64
        system: Linux
       version: Red Hat Enterprise Linux 8.5 Ootpa

recmanj commented 2 years ago

PR inc