# Buggy code
def db_writer_thread(self):
    num_frames = 0
    last_write = time.time()
    self._create_db()

    while not self.stop_running_event.is_set():
        messages = []

        m = self.get_message(self.GET_MESSAGE_TIMEOUT)
        while m is not None:
            log.debug("sqlitewriter buffering message")

            messages.append(
                (
                    m.timestamp,
                    m.arbitration_id,
                    m.id_type,
                    m.is_remote_frame,
                    m.is_error_frame,
                    m.dlc,
                    buffer(m.data),
                )
            )
            m = self.get_message(self.GET_MESSAGE_TIMEOUT)

            if time.time() - last_write > self.MAX_TIME_BETWEEN_WRITES:
                log.debug("Max timeout between writes reached")
                break

        if len(messages) > 0:
            with self.conn:
                log.debug("Writing %s frames to db", len(messages))
                self.conn.executemany(SqliteWriter.insert_msg_template, messages)
                num_frames += len(messages)
                last_write = time.time()

    self.conn.close()
    log.info("Stopped sqlite writer after writing %s messages", num_frames)


# Traceback (AssertionError)
======================================================================
FAIL: test_sql_reader (listener_test.FileReaderTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/travis/build/hardbyte/python-can/test/listener_test.py", line 179, in test_sql_reader
self.assertEqual(len(ms), 1)
AssertionError: 0 != 1
-------------------- >> begin captured logging << --------------------
can: DEBUG: can config: {'channel': None, 'interface': 'virtual'}
can.io.sql: INFO: Creating sqlite db
can.io.sql: DEBUG: Stopping sqlite writer
can.io.sql: INFO: Stopped sqlite writer after writing 0 messages
can.io.sql: DEBUG: Starting sqlreader with /tmp/tmpcqqrl_m3
can.io.sql: DEBUG: Iterating through messages from sql db
--------------------- >> end captured logging << ---------------------

# Fixed code
def db_writer_thread(self):
    num_frames = 0
    last_write = time.time()
    self._create_db()

    while not self.stop_running_event.is_set():
        messages = []

        m = self.get_message(self.GET_MESSAGE_TIMEOUT)
        while m is not None:
            log.debug("sqlitewriter buffering message")

            messages.append(
                (
                    m.timestamp,
                    m.arbitration_id,
                    m.id_type,
                    m.is_remote_frame,
                    m.is_error_frame,
                    m.dlc,
                    buffer(m.data),
                )
            )

            if time.time() - last_write > self.MAX_TIME_BETWEEN_WRITES:
                log.debug("Max timeout between writes reached")
                break

            m = self.get_message(self.GET_MESSAGE_TIMEOUT)

        if len(messages) > 0:
            with self.conn:
                log.debug("Writing %s frames to db", len(messages))
                self.conn.executemany(SqliteWriter.insert_msg_template, messages)
                num_frames += len(messages)
                last_write = time.time()

    self.conn.close()
    log.info("Stopped sqlite writer after writing %s messages", num_frames)