# Buggy code
def close(self):
    """
    Cleanly shutdown the router socket
    """
    if self._closing:
        return
    self._closing = True
    if hasattr(self, "_monitor") and self._monitor is not None:
        self._monitor.stop()
        self._monitor = None
    if hasattr(self, "clients"):
        self.clients.close()
    self.stream.close()


# Traceback (IOError)
2015-12-13 06:53:53,199 [salt.master      ][ERROR   ][17378] Error in function _mine:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 1379, in run_func
ret = getattr(self, func)(load)
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 1007, in _mine
load = self.__verify_load(load, ('id', 'data', 'tok'))
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 924, in __verify_load
if not self.__verify_minion(load['id'], load['tok']):
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 824, in __verify_minion
with salt.utils.fopen(pub_path, 'r') as fp_:
File "/usr/lib/python2.7/dist-packages/salt/utils/__init__.py", line 1204, in fopen
fhandle = open(*args, **kwargs)
IOError: [Errno 2] No such file or directory: '/etc/salt/pki/master/minions/ause1-encoder546'
2015-12-13 06:54:25,104 [salt.transport.zeromq][ERROR   ][17378] Bad load from minion
2015-12-13 06:54:35,228 [salt.transport.zeromq][ERROR   ][17370] Bad load from minion
2015-12-13 06:54:46,744 [salt.transport.zeromq][ERROR   ][17398] Bad load from minion
2015-12-13 06:54:53,352 [salt.master      ][ERROR   ][17317] Error in function _mine:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 1379, in run_func
ret = getattr(self, func)(load)
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 1007, in _mine
load = self.__verify_load(load, ('id', 'data', 'tok'))
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 924, in __verify_load
if not self.__verify_minion(load['id'], load['tok']):
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 824, in __verify_minion
with salt.utils.fopen(pub_path, 'r') as fp_:
File "/usr/lib/python2.7/dist-packages/salt/utils/__init__.py", line 1204, in fopen
fhandle = open(*args, **kwargs)
IOError: [Errno 2] No such file or directory: '/etc/salt/pki/master/minions/ause1-encoder546'
2015-12-13 06:55:00,814 [salt.master      ][ERROR   ][17343] Error in function _mine:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 1379, in run_func
ret = getattr(self, func)(load)
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 1007, in _mine
load = self.__verify_load(load, ('id', 'data', 'tok'))
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 924, in __verify_load
if not self.__verify_minion(load['id'], load['tok']):
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 824, in __verify_minion
with salt.utils.fopen(pub_path, 'r') as fp_:
File "/usr/lib/python2.7/dist-packages/salt/utils/__init__.py", line 1204, in fopen
fhandle = open(*args, **kwargs)
IOError: [Errno 2] No such file or directory: '/etc/salt/pki/master/minions/ause1-encoder646'
2015-12-13 06:55:17,444 [salt.master      ][ERROR   ][17443] Error in function _mine:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 1379, in run_func
ret = getattr(self, func)(load)
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 1007, in _mine
load = self.__verify_load(load, ('id', 'data', 'tok'))
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 924, in __verify_load
if not self.__verify_minion(load['id'], load['tok']):
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 824, in __verify_minion
with salt.utils.fopen(pub_path, 'r') as fp_:
File "/usr/lib/python2.7/dist-packages/salt/utils/__init__.py", line 1204, in fopen
fhandle = open(*args, **kwargs)
IOError: [Errno 2] No such file or directory: '/etc/salt/pki/master/minions/ause1-encoder66'

# Fixed code
def close(self):
    """
    Cleanly shutdown the router socket
    """
    if self._closing:
        return
    self._closing = True
    if hasattr(self, "_monitor") and self._monitor is not None:
        self._monitor.stop()
        self._monitor = None
    if hasattr(self, "_w_monitor") and self._w_monitor is not None:
        self._w_monitor.stop()
        self._w_monitor = None
    if hasattr(self, "clients"):
        self.clients.close()
    self.stream.close()