# Buggy code
def get_serial(self):
    """
    Get serial number for device

    :return: String of the serial number
    :rtype: str
    """
    # TODO raise exception if serial cant be got and handle during device add
    if self._serial is None:
        serial_path = os.path.join(self._device_path, "device_serial")
        count = 0
        serial = ""
        while len(serial) == 0:
            if count >= 5:
                break

            try:
                with open(serial_path, "r") as f:
                    serial = f.read().strip()
            except (PermissionError, OSError) as err:
                self.logger.warning("getting serial: {0}".format(err))
                serial = ""

            count += 1
            time.sleep(0.1)

            if len(serial) == 0:
                self.logger.debug("getting serial: {0} count:{1}".format(serial, count))

        if (
            serial == ""
            or serial == "Default string"
            or serial == "empty (NULL)"
            or serial == "As printed in the D cover"
        ):
            serial = "UNKWN{0:012}".format(random.randint(0, 4096))

        self._serial = serial.replace(" ", "_")

    return self._serial


# Traceback (UnicodeDecodeError)
Starting daemon.
2018-03-25 18:28:24 | razer                          | INFO     | Initialising Daemon (v2.2.2). Pid: 6168
2018-03-25 18:28:24 | razer.screensaver              | INFO     | Initialising DBus Screensaver Monitor
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerAbyssus ---------------------------- (1532:0042)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerAbyssus1800 ------------------------ (1532:0020)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerAbyssusV2 -------------------------- (1532:005B)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerAnansi ----------------------------- (1532:010F)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerBlackWidowChroma ------------------- (1532:0203)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerBlackWidowChromaOverwatch ---------- (1532:0211)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerBlackWidowChromaTournamentEdition -- (1532:0209)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerBlackWidowChromaV2 ----------------- (1532:0221)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerBlackWidowClassic ------------------ (1532:011B)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerBlackWidowClassicAlternate --------- (1532:010E)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerBlackWidowUltimate2012 ------------- (1532:010D)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerBlackWidowUltimate2013 ------------- (1532:011A)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerBlackWidowUltimate2016 ------------- (1532:0214)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerBlackWidowXChroma ------------------ (1532:0216)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerBlackWidowXTournamentEditionChroma - (1532:021A)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerBlackWidowXUltimate ---------------- (1532:0217)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerBladeLate2016 ---------------------- (1532:0224)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerBladePro2017 ----------------------- (1532:0225)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerBladePro2017FullHD ----------------- (1532:022F)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerBladeProLate2016 ------------------- (1532:0210)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerBladeQHD --------------------------- (1532:020F)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerBladeStealth ----------------------- (1532:0205)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerBladeStealthLate2016 --------------- (1532:0220)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerBladeStealthLate2017 --------------- (1532:0232)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerBladeStealthMid2017 ---------------- (1532:022D)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerChromaMugHolder -------------------- (1532:0F07)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerCore ------------------------------- (1532:0215)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerDeathAdderChroma ------------------- (1532:0043)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerDeathAdderElite -------------------- (1532:005C)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerDeathStalkerChroma ----------------- (1532:0204)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerDeathStalkerExpert ----------------- (1532:0202)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerDiamondbackChroma ------------------ (1532:004C)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerFirefly ---------------------------- (1532:0C00)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerImperator -------------------------- (1532:002F)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerKraken ----------------------------- (1532:0504)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerKrakenClassic ---------------------- (1532:0501)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerKrakenV2 --------------------------- (1532:0510)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerMamba2012Wired --------------------- (1532:0024)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerMamba2012Wireless ------------------ (1532:0025)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerMambaChromaWired ------------------- (1532:0044)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerMambaChromaWireless ---------------- (1532:0045)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerMambaTE ---------------------------- (1532:0046)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerNaga2014 --------------------------- (1532:0040)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerNagaChroma ------------------------- (1532:0053)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerNagaHex ---------------------------- (1532:0041)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerNagaHexRed ------------------------- (1532:0036)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerNagaHexV2 -------------------------- (1532:0050)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerNostromo --------------------------- (1532:0111)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerOrbweaver -------------------------- (1532:0113)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerOrbweaverChroma -------------------- (1532:0207)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerOrnata ----------------------------- (1532:021F)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerOrnataChroma ----------------------- (1532:021E)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerOrochi2011 ------------------------- (1532:0013)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerOrochi2013 ------------------------- (1532:0039)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerOrochiWired ------------------------ (1532:0048)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerOuroboros -------------------------- (1532:0032)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerTaipan ----------------------------- (1532:0034)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerTartarus --------------------------- (1532:0201)
2018-03-25 18:28:24 | razer                          | DEBUG    | Loaded device specification: RazerTartarusChroma --------------------- (1532:0208)
2018-03-25 18:28:24 | razer                          | INFO     | Found device.0: 0003:1532:0204.0003
2018-03-25 18:28:24 | razer.device0                  | INFO     | Initialising device.0 RazerDeathStalkerChroma
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/daemonize.py", line 245, in start
self.action(*privileged_action_result)
File "/usr/bin/openrazer-daemon", line 126, in run_daemon
test_dir=args.test_dir)
File "/usr/lib/python3/dist-packages/openrazer_daemon/daemon.py", line 107, in __init__
self._load_devices(first_run=True)
File "/usr/lib/python3/dist-packages/openrazer_daemon/daemon.py", line 398, in _load_devices
razer_device = device_class(sys_path, device_number, self._config, testing=self._test_dir is not None, additional_interfaces=sorted(additional_interfaces))
File "/usr/lib/python3/dist-packages/openrazer_daemon/hardware/keyboards.py", line 50, in __init__
super(_RippleKeyboard, self).__init__(*args, **kwargs)
File "/usr/lib/python3/dist-packages/openrazer_daemon/hardware/keyboards.py", line 19, in __init__
super(_MacroKeyboard, self).__init__(*args, **kwargs)
File "/usr/lib/python3/dist-packages/openrazer_daemon/hardware/device_base.py", line 477, in __init__
super().__init__(device_path, device_number, config, testing, additional_interfaces, additional_methods)
File "/usr/lib/python3/dist-packages/openrazer_daemon/hardware/device_base.py", line 440, in __init__
super().__init__(device_path, device_number, config, testing, additional_interfaces, additional_methods)
File "/usr/lib/python3/dist-packages/openrazer_daemon/hardware/device_base.py", line 64, in __init__
self.serial = self.get_serial()
File "/usr/lib/python3/dist-packages/openrazer_daemon/hardware/device_base.py", line 207, in get_serial
serial = open(serial_path, 'r').read().strip()
File "/usr/lib/python3.6/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 2: invalid start byte

Exception ignored in: <object repr() failed>
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/openrazer_daemon/hardware/device_base.py", line 426, in __del__
self.close()
File "/usr/lib/python3/dist-packages/openrazer_daemon/hardware/device_base.py", line 339, in close
if not self._is_closed:
AttributeError: 'RazerDeathStalkerChroma' object has no attribute '_is_closed'
Stopping daemon.

# Fixed code
def get_serial(self):
    """
    Get serial number for device

    :return: String of the serial number
    :rtype: str
    """
    # TODO raise exception if serial cant be got and handle during device add
    if self._serial is None:
        serial_path = os.path.join(self._device_path, "device_serial")
        count = 0
        serial = ""
        while len(serial) == 0:
            if count >= 5:
                break

            try:
                with open(serial_path, "r") as f:
                    serial = f.read().strip()
            except (PermissionError, OSError) as err:
                self.logger.warning("getting serial: {0}".format(err))
                serial = ""
            except UnicodeDecodeError as err:
                self.logger.warning("malformed serial: {0}".format(err))
                serial = ""

            count += 1
            time.sleep(0.1)

            if len(serial) == 0:
                self.logger.debug("getting serial: {0} count:{1}".format(serial, count))

        if (
            serial == ""
            or serial == "Default string"
            or serial == "empty (NULL)"
            or serial == "As printed in the D cover"
        ):
            serial = "UNKWN{0:012}".format(random.randint(0, 4096))

        self._serial = serial.replace(" ", "_")

    return self._serial