# Buggy code
def _process_loop(self):
    """
    The process loop is called off of the main thread and will not exit
    unless the main agent is shutdown.
    """

    _log.debug("Starting process loop.")
    self._setup_backup_db()
    self.historian_setup()

    # now that everything is setup we need to make sure that the topics
    # are syncronized between

    # Based on the state of the back log and whether or not sucessful
    # publishing is currently happening (and how long it's taking)
    # we may or may not want to wait on the event queue for more input
    # before proceeding with the rest of the loop.
    # wait_for_input = not bool(self._get_outstanding_to_publish())
    wait_for_input = not bool(self._get_outstanding_to_publish())

    while True:
        try:
            _log.debug("Reading from/waiting for queue.")
            new_to_publish = [self._event_queue.get(wait_for_input, self._retry_period)]
        except Empty:
            _log.debug("Queue wait timed out. Falling out.")
            new_to_publish = []

        if new_to_publish:
            _log.debug("Checking for queue build up.")
            while True:
                try:
                    new_to_publish.append(self._event_queue.get_nowait())
                except Empty:
                    break

        self._backup_new_to_publish(new_to_publish)

        wait_for_input = True
        start_time = datetime.utcnow()

        _log.debug("Calling publish_to_historian.")
        while True:
            to_publish_list = self._get_outstanding_to_publish()
            if not to_publish_list:
                break

            try:
                self.publish_to_historian(to_publish_list)
            except Exception as exp:
                _log.error(
                    "An unhandled exception has occured while publishing to historian."
                )
                _log.exception(exp)

            if not self._any_sucessfull_publishes():
                break
            self._cleanup_successful_publishes()

            now = datetime.utcnow()
            if now - start_time > self._max_time_publishing:
                wait_for_input = False
                break
    _log.debug("Finished processing")


# Traceback (TypeError)
2015-08-28 08:38:59,118 (forwarderagent-3.0 6934) volttron.platform.agent.base_historian ERROR: An unhandled exception has occured while publishing to historian.
2015-08-28 08:38:59,119 (forwarderagent-3.0 6934) <stderr> ERROR: Traceback (most recent call last):
2015-08-28 08:38:59,119 (forwarderagent-3.0 6934) <stderr> ERROR:   File "/usr/lib/python2.7/logging/__init__.py", line 851, in emit
2015-08-28 08:38:59,120 (forwarderagent-3.0 6934) <stderr> ERROR:     msg = self.format(record)
2015-08-28 08:38:59,120 (forwarderagent-3.0 6934) <stderr> ERROR:   File "/usr/lib/python2.7/logging/__init__.py", line 724, in format
2015-08-28 08:38:59,120 (forwarderagent-3.0 6934) <stderr> ERROR:     return fmt.format(record)
2015-08-28 08:38:59,120 (forwarderagent-3.0 6934) <stderr> ERROR:   File "/home/volttron/volttron-3x/volttron/platform/agent/utils.py", line 228, in format
2015-08-28 08:38:59,121 (forwarderagent-3.0 6934) <stderr> ERROR:     return jsonapi.dumps(dct)
2015-08-28 08:38:59,121 (forwarderagent-3.0 6934) <stderr> ERROR:   File "/home/volttron/volttron-3x/env/local/lib/python2.7/site-packages/zmq/utils/jsonapi.py", line 40, in dumps
2015-08-28 08:38:59,121 (forwarderagent-3.0 6934) <stderr> ERROR:     s = jsonmod.dumps(o, **kwargs)
2015-08-28 08:38:59,122 (forwarderagent-3.0 6934) <stderr> ERROR:   File "/home/volttron/volttron-3x/env/local/lib/python2.7/site-packages/simplejson/__init__.py", line 397, in dumps
2015-08-28 08:38:59,122 (forwarderagent-3.0 6934) <stderr> ERROR:     **kw).encode(obj)
2015-08-28 08:38:59,122 (forwarderagent-3.0 6934) <stderr> ERROR:   File "/home/volttron/volttron-3x/env/local/lib/python2.7/site-packages/simplejson/encoder.py", line 275, in encode
2015-08-28 08:38:59,122 (forwarderagent-3.0 6934) <stderr> ERROR:     chunks = self.iterencode(o, _one_shot=True)
2015-08-28 08:38:59,123 (forwarderagent-3.0 6934) <stderr> ERROR:   File "/home/volttron/volttron-3x/env/local/lib/python2.7/site-packages/simplejson/encoder.py", line 357, in iterencode
2015-08-28 08:38:59,123 (forwarderagent-3.0 6934) <stderr> ERROR:     return _iterencode(o, 0)
2015-08-28 08:38:59,123 (forwarderagent-3.0 6934) <stderr> ERROR:   File "/home/volttron/volttron-3x/env/local/lib/python2.7/site-packages/simplejson/encoder.py", line 252, in default
2015-08-28 08:38:59,124 (forwarderagent-3.0 6934) <stderr> ERROR:     raise TypeError(repr(o) + " is not JSON serializable")
2015-08-28 08:38:59,124 (forwarderagent-3.0 6934) <stderr> ERROR: TypeError: ProtocolError('previous send operation is not complete',) is not JSON serializable
2015-08-28 08:38:59,124 (forwarderagent-3.0 6934) <stderr> ERROR: Logged from file base_historian.py, line 370

# Fixed code
def _process_loop(self):
    """
    The process loop is called off of the main thread and will not exit
    unless the main agent is shutdown.
    """

    _log.debug("Starting process loop.")
    self._setup_backup_db()
    self.historian_setup()

    # now that everything is setup we need to make sure that the topics
    # are syncronized between

    # Based on the state of the back log and whether or not sucessful
    # publishing is currently happening (and how long it's taking)
    # we may or may not want to wait on the event queue for more input
    # before proceeding with the rest of the loop.
    # wait_for_input = not bool(self._get_outstanding_to_publish())
    wait_for_input = not bool(self._get_outstanding_to_publish())

    while True:
        try:
            _log.debug("Reading from/waiting for queue.")
            new_to_publish = [self._event_queue.get(wait_for_input, self._retry_period)]
        except Empty:
            _log.debug("Queue wait timed out. Falling out.")
            new_to_publish = []

        if new_to_publish:
            _log.debug("Checking for queue build up.")
            while True:
                try:
                    new_to_publish.append(self._event_queue.get_nowait())
                except Empty:
                    break

        self._backup_new_to_publish(new_to_publish)

        wait_for_input = True
        start_time = datetime.utcnow()

        _log.debug("Calling publish_to_historian.")
        while True:
            to_publish_list = self._get_outstanding_to_publish()
            if not to_publish_list:
                break

            try:
                self.publish_to_historian(to_publish_list)
            except Exception:
                _log.exception(
                    "An unhandled exception occured while publishing to the historian."
                )

            if not self._any_sucessfull_publishes():
                break
            self._cleanup_successful_publishes()

            now = datetime.utcnow()
            if now - start_time > self._max_time_publishing:
                wait_for_input = False
                break
    _log.debug("Finished processing")