# Buggy code
def fetch(self):
    """Fetch the data from hddtemp daemon."""
    # Taking care of sudden deaths/stops of hddtemp daemon
    try:
        sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sck.connect((self.host, self.port))
        data = b""
        while True:
            received = sck.recv(4096)
            if not received:
                break
            data += received
    except socket.error as e:
        logger.debug(
            "Cannot connect to an HDDtemp server ({}:{} => {})".format(
                self.host, self.port, e
            )
        )
        logger.debug(
            "Disable the HDDtemp module. Use the --disable-hddtemp to hide the previous message."
        )
        if self.args is not None:
            self.args.disable_hddtemp = True
        data = ""
    finally:
        sck.close()
        if data != "":
            logger.debug("Received data from the HDDtemp server: {}".format(data))

    return data


# Traceback (NameError)
$ glances
^CTraceback (most recent call last):
File "/usr/local/bin/glances", line 11, in <module>
load_entry_point('Glances==2.11.1', 'console_scripts', 'glances')()
File "/usr/local/lib/python2.7/site-packages/glances/__init__.py", line 134, in main
start(config=config, args=args)
File "/usr/local/lib/python2.7/site-packages/glances/__init__.py", line 100, in start
mode = GlancesMode(config=config, args=args)
File "/usr/local/lib/python2.7/site-packages/glances/standalone.py", line 69, in __init__
self.stats.update()
File "/usr/local/lib/python2.7/site-packages/glances/stats.py", line 192, in update
self._plugins[p].update()
File "/usr/local/lib/python2.7/site-packages/glances/plugins/glances_plugin.py", line 884, in wrapper
ret = fct(self, *args, **kw)
File "/usr/local/lib/python2.7/site-packages/glances/plugins/glances_plugin.py", line 893, in wrapper
ret = fct(*args, **kw)
File "/usr/local/lib/python2.7/site-packages/glances/plugins/glances_hddtemp.py", line 64, in update
self.stats = self.glancesgrabhddtemp.get()
File "/usr/local/lib/python2.7/site-packages/glances/plugins/glances_hddtemp.py", line 157, in get
self.__update__()
File "/usr/local/lib/python2.7/site-packages/glances/plugins/glances_hddtemp.py", line 104, in __update__
data = self.fetch()
File "/usr/local/lib/python2.7/site-packages/glances/plugins/glances_hddtemp.py", line 142, in fetch
sck.connect((self.host, self.port))
File "/usr/local/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
File "/usr/local/lib/python2.7/site-packages/glances/__init__.py", line 68, in __signal_handler
end()
File "/usr/local/lib/python2.7/site-packages/glances/__init__.py", line 73, in end
mode.end()
NameError: global name 'mode' is not defined

# Fixed code
def fetch(self):
    """Fetch the data from hddtemp daemon."""
    # Taking care of sudden deaths/stops of hddtemp daemon
    try:
        sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sck.connect((self.host, self.port))
        data = b""
        while True:
            received = sck.recv(4096)
            if not received:
                break
            data += received
    except Exception as e:
        logger.debug(
            "Cannot connect to an HDDtemp server ({}:{} => {})".format(
                self.host, self.port, e
            )
        )
        logger.debug(
            "Disable the HDDtemp module. Use the --disable-hddtemp to hide the previous message."
        )
        if self.args is not None:
            self.args.disable_hddtemp = True
        data = ""
    finally:
        sck.close()
        if data != "":
            logger.debug("Received data from the HDDtemp server: {}".format(data))

    return data