victronenergy / venus

Victron Energy Unix/Linux OS
https://github.com/victronenergy/venus/wiki
568 stars 70 forks source link

vrmlogger: Logging breaks if malformed sqlite3 file on internal storage #1128

Closed izak closed 1 year ago

izak commented 1 year ago

Scenario:

    @4000000064d0bf9230351a44     main()
    @4000000064d0bf9230353984   File "/opt/victronenergy/vrmlogger/vrmlogger.py", line 681, in main
    @4000000064d0bf92305c841c     start_logging(state)
    @4000000064d0bf92305d91a4   File "/opt/victronenergy/vrmlogger/vrmlogger.py", line 440, in start_logging
    @4000000064d0bf9230773424     endpoint.send(VrmCommandType.ANNOUNCE, get_announce())
    @4000000064d0bf92307812cc   File "/opt/victronenergy/vrmlogger/http_endpoint.py", line 494, in send
    @4000000064d0bf9230950cc4     self._put_in_buffer_with_fallback_on_internal(data, int(time.time()))
    @4000000064d0bf92309602dc   File "/opt/victronenergy/vrmlogger/http_endpoint.py", line 449, in _put_in_buffer_with_fallback_on_internal
    @4000000064d0bf9230b0ed7c     self._active_buffer.put(data, timestamp)
    @4000000064d0bf9230b1c83c   File "/opt/victronenergy/vrmlogger/nonvolatilequeueramdiskwrapper.py", line 58, in put
    @4000000064d0bf9230bae05c     return super(NonVolatileQueueRamDiskWrapper, self).put(data, timestamp)
    @4000000064d0bf9230bbf1cc   File "/opt/victronenergy/vrmlogger/nonvolatilequeue.py", line 186, in put
    @4000000064d0bf9230cae204     cursor.execute("DELETE FROM queue WHERE timestamp < (strftime('%s', 'now') - ?)", (self._max_age,))
    @4000000064d0bf9230cbeba4 sqlite3.DatabaseError: database disk image is malformed

The code around nonvolatilequeueramdiskwrapper.py, line 58 (where the error is raised) will switch to a RAM-disk on some errors. But not this one.

According to documentation, sqlite3.DatabaseError is an unexpected exception to show up here. We would expect an OperationalError.

Nevertheless, it seems one easy improvement, is to catch the base error here, so we can more smoothly switch to RAM-disk. Then we at least continue logging. So:

izak commented 1 year ago

There is a lot here that is not being caught.

sqlite3.DatabaseError: file is not a database <--- I corrupted the file with zeroes at the start
sqlite3.DatabaseError: malformed database schema (?) <--- I left the heading intact but wrote zeroes 512 bytes into it

Safer to rather treat all DatabaseErrors the same: As a sign of corruption.