# Buggy code
def run():
    global args
    args = parse_args()
    if args.stop:
        stop_daemon(args)
        sys.exit(0)

    if args.respawn:
        stop_daemon(args)
        time.sleep(3)

    install_example_config_file()

    os.makedirs(args.run_dir, exist_ok=True)
    daemon = Daemonize(
        app="openrazer-daemon",
        pid=os.path.join(args.run_dir, "openrazer-daemon.pid"),
        action=run_daemon,
        foreground=args.foreground,
        verbose=args.verbose,
        chdir=args.run_dir,
    )
    daemon.start()


# Traceback (AttributeError)
thunderrd@Q6600:~$ openrazer-daemon -Fv
2017-11-01 21:44:38 | razer                          | INFO     | Initialising Daemon (v2.0.0). Pid: 13092
2017-11-01 21:44:38 | razer.screensaver              | INFO     | Initialising DBus Screensaver Monitor
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerAbyssus ---------------------------- (1532:0042)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerAbyssusV2 -------------------------- (1532:005B)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerAnansi ----------------------------- (1532:010F)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerBlackWidowChroma ------------------- (1532:0203)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerBlackWidowChromaOverwatch ---------- (1532:0211)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerBlackWidowChromaTournamentEdition -- (1532:0209)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerBlackWidowChromaV2 ----------------- (1532:0221)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification:

2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerBlackWidowClassic ------------------ (1532:011B)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerBlackWidowClassicAlternate --------- (1532:010E)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerBlackWidowUltimate2012 ------------- (1532:010D)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerBlackWidowUltimate2013 ------------- (1532:011A)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerBlackWidowUltimate2016 ------------- (1532:0214)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerBlackWidowXChroma ------------------ (1532:0216)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerBlackWidowXTournamentEditionChroma - (1532:021A)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerBlackWidowXUltimate ---------------- (1532:0217)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerBladeLate2016 ---------------------- (1532:0224)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerBladePro2017 ----------------------- (1532:0225)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerBladeProLate2016 ------------------- (1532:0210)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerBladeQHD --------------------------- (1532:020F)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerBladeStealth ----------------------- (1532:0205)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerBladeStealthLate2016 --------------- (1532:0220)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerBladeStealthLate2017 --------------- (1532:0232)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerBladeStealthMid2017 ---------------- (1532:022D)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerChromaMugHolder -------------------- (1532:0F07)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerCore ------------------------------- (1532:0215)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerDeathAdderChroma ------------------- (1532:0043)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerDeathAdderElite -------------------- (1532:005C)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerDeathStalkerChroma ----------------- (1532:0204)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerDeathStalkerExpert ----------------- (1532:0202)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerDiamondbackChroma ------------------ (1532:004C)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerFirefly ---------------------------- (1532:0C00)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerImperator -------------------------- (1532:002F)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerKraken ----------------------------- (1532:0504)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerKrakenClassic ---------------------- (1532:0501)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerKrakenV2 --------------------------- (1532:0510)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerMamba2012Wired --------------------- (1532:0024)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerMamba2012Wireless ------------------ (1532:0025)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerMambaChromaWired ------------------- (1532:0044)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerMambaChromaWireless ---------------- (1532:0045)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerMambaTE ---------------------------- (1532:0046)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerNaga2014 --------------------------- (1532:0040)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerNagaChroma ------------------------- (1532:0053)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerNagaHex ---------------------------- (1532:0041)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerNagaHexRed ------------------------- (1532:0036)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerNagaHexV2 -------------------------- (1532:0050)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerNostromo --------------------------- (1532:0111)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerOrbweaver -------------------------- (1532:0113)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerOrbweaverChroma -------------------- (1532:0207)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerOrnata ----------------------------- (1532:021F)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerOrnataChroma ----------------------- (1532:021E)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerOrochi2011 ------------------------- (1532:0013)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerOrochi2013 ------------------------- (1532:0039)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerOrochiWired ------------------------ (1532:0048)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerOuroboros -------------------------- (1532:0032)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerTaipan ----------------------------- (1532:0034)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerTartarus --------------------------- (1532:0201)
2017-11-01 21:44:38 | razer                          | DEBUG    | Loaded device specification: RazerTartarusChroma --------------------- (1532:0208)
2017-11-01 21:44:38 | razer                          | INFO     | Found device.0: 0003:1532:0214.0003
Exception ignored in: <object repr() failed>
Traceback (most recent call last):
File "/usr/lib64/python3.6/site-packages/openrazer_daemon/hardware/device_base.py", line 421, in __del__
self.close()
File "/usr/lib64/python3.6/site-packages/openrazer_daemon/hardware/device_base.py", line 334, in close
if not self._is_closed:
AttributeError: 'RazerBlackWidowUltimate2016' object has no attribute '_is_closed'
thunderrd@Q6600:~$

# Fixed code
def run():
    global args

    logger = None
    args = parse_args()

    if args.stop:
        stop_daemon(args)
        sys.exit(0)

    if args.respawn:
        stop_daemon(args)
        time.sleep(3)

    # daemonize logs exceptions to its logger (which defaults to the syslog)
    # and does not make them appear on stdout/stderr. If we're in foreground
    # mode, override that logger with our own.
    if not args.foreground:
        logger = logging.getLogger("run-daemon")
        if args.verbose:
            logger.setLevel(logging.DEBUG)

    install_example_config_file()

    os.makedirs(args.run_dir, exist_ok=True)
    daemon = Daemonize(
        app="openrazer-daemon",
        pid=os.path.join(args.run_dir, "openrazer-daemon.pid"),
        action=run_daemon,
        foreground=args.foreground,
        verbose=args.verbose,
        chdir=args.run_dir,
        logger=logger,
    )
    daemon.start()