# Buggy code
def on_comm_print_job_done(self):
    self._fileManager.delete_recovery_data()
    self._setPrintingUser(None)

    payload = self._payload_for_print_job_event()
    if payload:
        payload["time"] = self._comm.getPrintTime()
        self._updateProgressData(
            completion=1.0,
            filepos=payload["size"],
            printTime=payload["time"],
            printTimeLeft=0,
        )
        self._stateMonitor.set_state(
            self._dict(text=self.get_state_string(), flags=self._getStateFlags())
        )

        eventManager().fire(Events.PRINT_DONE, payload)
        self.script("afterPrintDone", context=dict(event=payload), must_be_set=False)

        def log_print():
            self._fileManager.log_print(
                payload["origin"],
                payload["path"],
                time.time(),
                payload["time"],
                True,
                self._printerProfileManager.get_current_or_default()["id"],
            )

        thread = threading.Thread(target=log_print)
        thread.daemon = True
        thread.start()

    else:
        self._updateProgressData()
        self._stateMonitor.set_state(
            self._dict(text=self.get_state_string(), flags=self._getStateFlags())
        )


# Traceback (AttributeError)
2018-04-25 15:38:03,495 - octoprint.startup - INFO - ******************************************************************************
2018-04-25 15:38:03,496 - octoprint.startup - INFO - Starting OctoPrint 1.3.9.dev131+gbe4b98b9
2018-04-25 15:38:03,497 - octoprint.startup - INFO - Starting in SAFE MODE. Third party plugins will be disabled!
2018-04-25 15:38:03,497 - octoprint.startup - INFO - ******************************************************************************
2018-04-25 15:38:03,521 - octoprint.util.connectivity_checker - INFO - Connectivity changed from offline to online
2018-04-25 15:38:03,523 - octoprint.startup - INFO - Blacklist processing done
2018-04-25 15:38:03,543 - octoprint.plugin.core - INFO - Loading plugins from /home/hash/devel/OctoPrint/venv/lib/python2.7/site-packages/OctoPrint-1.3.9.dev131+gbe4b98b9-py2.7.egg/octoprint/plugins, /home/hash/.octoprint/plugins and installed plugin packages...
2018-04-25 15:38:03,958 - octoprint.plugin.core - WARNING - Plugin "OctoPi Support Plugin" did not pass check
2018-04-25 15:38:04,300 - octoprint.plugins.discovery - INFO - pybonjour is not installed, Zeroconf Discovery won't be available
2018-04-25 15:38:04,304 - octoprint.plugin.core - INFO - Found 12 plugin(s) providing 9 mixin implementations, 5 hook handlers
2018-04-25 15:38:04,346 - octoprint.server.heartbeat - INFO - Starting server heartbeat, 900.0s interval
2018-04-25 15:38:04,357 - octoprint.server - INFO - Intermediary server started
2018-04-25 15:38:04,357 - octoprint.plugin.core - INFO - Loading plugins from /home/hash/devel/OctoPrint/venv/lib/python2.7/site-packages/OctoPrint-1.3.9.dev131+gbe4b98b9-py2.7.egg/octoprint/plugins, /home/hash/.octoprint/plugins and installed plugin packages...
2018-04-25 15:38:04,362 - octoprint.plugin.core - WARNING - Plugin "OctoPi Support Plugin" did not pass check
2018-04-25 15:38:04,620 - octoprint.plugin.core - INFO - Found 12 plugin(s) providing 9 mixin implementations, 5 hook handlers
2018-04-25 15:38:04,634 - octoprint.filemanager.storage - INFO - Initializing the file metadata for /home/hash/.octoprint/uploads...
2018-04-25 15:38:04,934 - octoprint.filemanager.storage - INFO - ... file metadata for /home/hash/.octoprint/uploads initialized successfully.
2018-04-25 15:38:04,976 - octoprint.plugins.softwareupdate - INFO - Loaded version cache from disk
2018-04-25 15:38:05,625 - octoprint.util.pip - INFO - Using "/home/hash/devel/OctoPrint/venv/bin/python -m pip" as command to invoke pip
2018-04-25 15:38:06,269 - octoprint.util.pip - INFO - Version of pip is 10.0.1
2018-04-25 15:38:06,270 - octoprint.util.pip - INFO - pip installs to /home/hash/devel/OctoPrint/venv/lib/python2.7/site-packages (writable -> yes), --user flag needed -> no, virtual env -> yes
2018-04-25 15:38:06,270 - octoprint.util.pip - INFO - ==> pip ok -> yes
2018-04-25 15:38:06,272 - octoprint.plugin.core - INFO - Initialized 9 plugin implementation(s)
2018-04-25 15:38:06,280 - octoprint.plugin.core - INFO - 12 plugin(s) registered with the system:
|  Announcement Plugin (bundled) = /home/hash/devel/OctoPrint/venv/lib/python2.7/site-packages/OctoPrint-1.3.9.dev131+gbe4b98b9-py2.7.egg/octoprint/plugins/announcements
|  Core Wizard (bundled) = /home/hash/devel/OctoPrint/venv/lib/python2.7/site-packages/OctoPrint-1.3.9.dev131+gbe4b98b9-py2.7.egg/octoprint/plugins/corewizard
|  CuraEngine (<= 15.04) (bundled) = /home/hash/devel/OctoPrint/venv/lib/python2.7/site-packages/OctoPrint-1.3.9.dev131+gbe4b98b9-py2.7.egg/octoprint/plugins/cura
|  Discovery (bundled) = /home/hash/devel/OctoPrint/venv/lib/python2.7/site-packages/OctoPrint-1.3.9.dev131+gbe4b98b9-py2.7.egg/octoprint/plugins/discovery
|  Logging (bundled) = /home/hash/devel/OctoPrint/venv/lib/python2.7/site-packages/OctoPrint-1.3.9.dev131+gbe4b98b9-py2.7.egg/octoprint/plugins/logging
| !Opitemp Plugin (1.0.1) = /home/hash/devel/OctoPrint-OpiTemp/octoprint_opitemp
|  Plugin Manager (bundled) = /home/hash/devel/OctoPrint/venv/lib/python2.7/site-packages/OctoPrint-1.3.9.dev131+gbe4b98b9-py2.7.egg/octoprint/plugins/pluginmanager
|  Printer Safety Check (bundled) = /home/hash/devel/OctoPrint/venv/lib/python2.7/site-packages/OctoPrint-1.3.9.dev131+gbe4b98b9-py2.7.egg/octoprint/plugins/printer_safety_check
|  Software Update (bundled) = /home/hash/devel/OctoPrint/venv/lib/python2.7/site-packages/OctoPrint-1.3.9.dev131+gbe4b98b9-py2.7.egg/octoprint/plugins/softwareupdate
| !TimelapseStatsOverlay (0.1.1) = /home/hash/devel/OctoPrint/venv/local/lib/python2.7/site-packages/OctoPrint_TimelapseStatsOverlay-0.1.1-py2.7.egg/octoprint_timelapsestatsoverlay
| !Virtual LCD (0.1.0) = /home/hash/devel/OctoPrint/venv/local/lib/python2.7/site-packages/OctoPrint_Vlcd-0.1.0-py2.7.egg/octoprint_vlcd
|  Virtual Printer (bundled) = /home/hash/devel/OctoPrint/venv/lib/python2.7/site-packages/OctoPrint-1.3.9.dev131+gbe4b98b9-py2.7.egg/octoprint/plugins/virtual_printer
2018-04-25 15:38:06,287 - octoprint.environment - INFO - Detected environment is Python 2.7.13 under Linux (linux2). Details:
|  hardware:
|    cores: 2
|    freq: 2000.0
|    ram: 1052196864
|  os:
|    id: linux
|    platform: linux2
|  python:
|    pip: 10.0.1
|    version: 2.7.13
|    virtualenv: /home/hash/devel/OctoPrint/venv
2018-04-25 15:38:06,291 - octoprint.server - INFO - Reset webasset folder /home/hash/.octoprint/generated/webassets...
2018-04-25 15:38:06,302 - octoprint.server - INFO - Reset webasset folder /home/hash/.octoprint/generated/.webassets-cache...
2018-04-25 15:38:06,364 - octoprint.server - INFO - Shutting down intermediary server...
2018-04-25 15:38:06,864 - octoprint.server - INFO - Intermediary server shut down
2018-04-25 15:38:06,866 - octoprint.events - INFO - Processing startup event, this is our first event
2018-04-25 15:38:06,867 - octoprint.events - INFO - Adding 1 events to queue that were held back before startup event
2018-04-25 15:38:06,874 - octoprint.filemanager - INFO - Adding backlog items from all storage types to analysis queue...
2018-04-25 15:38:06,916 - octoprint.plugins.pluginmanager - INFO - Loaded plugin repository data from disk, was still valid
2018-04-25 15:38:06,949 - octoprint.plugins.cura - INFO - Path to CuraEngine has not been configured or does not exist (currently set to None), Cura will not be selectable for slicing
2018-04-25 15:38:06,981 - octoprint.plugins.discovery - INFO - Registered OctoPrint instance on portatil for SSDP
2018-04-25 15:38:06,983 - octoprint.server - INFO - Listening on http://[::]:5000
2018-04-25 15:38:06,985 - octoprint.server - INFO - Server started successfully in safe mode as requested from config, removing flag
2018-04-25 15:38:07,028 - octoprint.filemanager - INFO - Added 0 items from storage type "local" to analysis queue
2018-04-25 15:38:07,116 - octoprint.plugins.pluginmanager - INFO - Loaded notice data from disk, was still valid
2018-04-25 15:38:07,329 - octoprint.plugins.pluginmanager - INFO - Loaded plugin repository data from disk, was still valid
2018-04-25 15:38:07,391 - octoprint.server.preemptive_cache - INFO - Preemptively caching / (ui _default) for {'query_string': 'l10n=en', 'path': '/', 'base_url': 'http://192.168.1.12:5000/'}
2018-04-25 15:38:07,478 - octoprint.plugins.pluginmanager - INFO - Loaded notice data from disk, was still valid
2018-04-25 15:38:11,307 - octoprint.server.preemptive_cache - INFO - ... done in 3.92s
2018-04-25 15:38:11,882 - octoprint.server.util.sockjs - INFO - New connection from client: 192.168.1.2
2018-04-25 15:38:13,112 - octoprint.server.util.sockjs - INFO - Client connection closed: 192.168.1.2
2018-04-25 15:38:13,893 - octoprint.server.util.sockjs - INFO - New connection from client: 192.168.1.2
2018-04-25 15:38:17,705 - octoprint.util.comm - INFO - Changing monitoring state from "Offline" to "Opening serial port"
2018-04-25 15:38:17,709 - octoprint.util.comm - INFO - Changing monitoring state from "Opening serial port" to "Detecting baudrate"
2018-04-25 15:38:18,714 - octoprint.util.comm - INFO - M110 detected, setting current line number to 0
2018-04-25 15:38:18,717 - octoprint.util.comm - INFO - Changing monitoring state from "Detecting baudrate" to "Operational"
2018-04-25 15:38:18,722 - octoprint.util.comm - INFO - M110 detected, setting current line number to 0
2018-04-25 15:38:18,728 - octoprint.util.comm - INFO - Printer reports firmware name "Virtual Marlin 1.0"
2018-04-25 15:38:26,667 - octoprint - ERROR - Exception on /api/job [POST]
Traceback (most recent call last):
  File "/home/hash/devel/OctoPrint/venv/local/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/hash/devel/OctoPrint/venv/local/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/hash/devel/OctoPrint/venv/local/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/hash/devel/OctoPrint/venv/local/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/hash/devel/OctoPrint/venv/local/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/hash/devel/OctoPrint/venv/local/lib/python2.7/site-packages/OctoPrint-1.3.9.dev131+gbe4b98b9-py2.7.egg/octoprint/server/util/flask.py", line 1140, in decorated_view
    return flask_login.login_required(func)(*args, **kwargs)
  File "/home/hash/devel/OctoPrint/venv/local/lib/python2.7/site-packages/flask_login.py", line 758, in decorated_view
    return func(*args, **kwargs)
  File "/home/hash/devel/OctoPrint/venv/local/lib/python2.7/site-packages/OctoPrint-1.3.9.dev131+gbe4b98b9-py2.7.egg/octoprint/server/api/job.py", line 42, in controlJob
    printer.start_print(tags=tags, user=user)
  File "/home/hash/devel/OctoPrint/venv/local/lib/python2.7/site-packages/OctoPrint-1.3.9.dev131+gbe4b98b9-py2.7.egg/octoprint/printer/standard.py", line 503, in start_print
    self._setPrintingUser(user)
AttributeError: 'Printer' object has no attribute '_setPrintingUser'
2018-04-25 15:38:26,671 - tornado.access - ERROR - 500 POST /api/job (192.168.1.2) 9.74ms
2018-04-25 15:40:23,406 - octoprint.filemanager.analysis - INFO - Starting analysis of local:Micro_Catapult_INC.gcode
2018-04-25 15:40:23,407 - octoprint.filemanager.analysis - INFO - Invoking analysis command: /home/hash/devel/OctoPrint/venv/bin/python -m octoprint analysis gcode --speed-x=16000 --speed-y=16000 --max-t=10 --throttle=0.0 --throttle-lines=100 /home/hash/.octoprint/uploads/Micro_Catapult_INC.gcode
2018-04-25 15:40:23,429 - octoprint - ERROR - Exception on /api/files/local [POST]
Traceback (most recent call last):
  File "/home/hash/devel/OctoPrint/venv/local/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/hash/devel/OctoPrint/venv/local/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/hash/devel/OctoPrint/venv/local/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/hash/devel/OctoPrint/venv/local/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/hash/devel/OctoPrint/venv/local/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/hash/devel/OctoPrint/venv/local/lib/python2.7/site-packages/OctoPrint-1.3.9.dev131+gbe4b98b9-py2.7.egg/octoprint/server/util/flask.py", line 1140, in decorated_view
    return flask_login.login_required(func)(*args, **kwargs)
  File "/home/hash/devel/OctoPrint/venv/local/lib/python2.7/site-packages/flask_login.py", line 758, in decorated_view
    return func(*args, **kwargs)
  File "/home/hash/devel/OctoPrint/venv/local/lib/python2.7/site-packages/OctoPrint-1.3.9.dev131+gbe4b98b9-py2.7.egg/octoprint/server/api/files.py", line 361, in uploadGcodeFile
    filename = fileProcessingFinished(added_file, fileManager.path_on_disk(FileDestinations.LOCAL, added_file), target)
  File "/home/hash/devel/OctoPrint/venv/local/lib/python2.7/site-packages/OctoPrint-1.3.9.dev131+gbe4b98b9-py2.7.egg/octoprint/server/api/files.py", line 332, in fileProcessingFinished
    selectAndOrPrint(filename, absFilename, destination)
  File "/home/hash/devel/OctoPrint/venv/local/lib/python2.7/site-packages/OctoPrint-1.3.9.dev131+gbe4b98b9-py2.7.egg/octoprint/server/api/files.py", line 345, in selectAndOrPrint
    printer.select_file(absFilename, destination == FileDestinations.SDCARD, printAfterSelect, user)
  File "/home/hash/devel/OctoPrint/venv/local/lib/python2.7/site-packages/OctoPrint-1.3.9.dev131+gbe4b98b9-py2.7.egg/octoprint/printer/standard.py", line 448, in select_file
    tags=kwargs.get("tags", set()) | {"trigger:printer.select_file"})
  File "/home/hash/devel/OctoPrint/venv/local/lib/python2.7/site-packages/OctoPrint-1.3.9.dev131+gbe4b98b9-py2.7.egg/octoprint/util/comm.py", line 1094, in selectFile
    self._callback.on_comm_file_selected(filename, self._currentFile.getFilesize(), False, user=user)
  File "/home/hash/devel/OctoPrint/venv/local/lib/python2.7/site-packages/OctoPrint-1.3.9.dev131+gbe4b98b9-py2.7.egg/octoprint/printer/standard.py", line 1155, in on_comm_file_selected
    self.start_print(pos=self._posAfterSelect, user=self._printingUser)
AttributeError: 'Printer' object has no attribute '_printingUser'
2018-04-25 15:40:23,437 - tornado.access - ERROR - 500 POST /api/files/local?print=true (192.168.1.2) 105.88ms
2018-04-25 15:40:32,554 - octoprint.filemanager.analysis - INFO - Analysis of entry local:Micro_Catapult_INC.gcode finished, needed 9.15s
2018-04-25 15:41:03,810 - octoprint.server - INFO - Shutting down...
2018-04-25 15:41:03,983 - octoprint.events - INFO - Processing shutdown event, this will be our last event
2018-04-25 15:41:03,989 - octoprint.events - INFO - Event loop shut down
2018-04-25 15:41:03,991 - octoprint.server - INFO - Goodbye!


# Fixed code
def on_comm_print_job_done(self):
    self._fileManager.delete_recovery_data()

    payload = self._payload_for_print_job_event()
    if payload:
        payload["time"] = self._comm.getPrintTime()
        self._updateProgressData(
            completion=1.0,
            filepos=payload["size"],
            printTime=payload["time"],
            printTimeLeft=0,
        )
        self._stateMonitor.set_state(
            self._dict(text=self.get_state_string(), flags=self._getStateFlags())
        )

        eventManager().fire(Events.PRINT_DONE, payload)
        self.script("afterPrintDone", context=dict(event=payload), must_be_set=False)

        def log_print():
            self._fileManager.log_print(
                payload["origin"],
                payload["path"],
                time.time(),
                payload["time"],
                True,
                self._printerProfileManager.get_current_or_default()["id"],
            )

        thread = threading.Thread(target=log_print)
        thread.daemon = True
        thread.start()

    else:
        self._updateProgressData()
        self._stateMonitor.set_state(
            self._dict(text=self.get_state_string(), flags=self._getStateFlags())
        )