Skip to content

GzipFile BLFWriter negative seek error #1378

@j-c-cook

Description

@j-c-cook

Describe the bug

A GZipFile object implementation in cpython is unable to be sought negatively while in write mode. This is problematic for the BLFWriter. Upon initialization, the BLFWriter writes a default header to the file, in which the start and stop timestamps are not set. Once the BLFWriter is told to stop, the remaining data in the buffer is flushed to the file and then the header must be updated with the start and stop timestamps. This is where the GZipFile object has problems. The location of the writer must be moved to the 0'th position of the file. When in write mode, the offset position cannot be negatively sought.

To Reproduce

Socket CAN setup

Enable CAN interface:

sudo ip link add dev vcan0 type vcan && sudo ip link set up vcan0

Generate random CAN messages 1 millisecond apart:

cangen vcan0 -e -I 18EAFF26 -L i -v -v -v -g 1

python-can setup

Enable the Logger:

python -m can.logger -c vcan0 -i socketcan -b 250000 -f file.blf.gz

The issue can be seen with the SizedRotatingLogger as well.

python -m can.logger -c vcan0 -i socketcan -b 250000 -f file.blf.gz -s 150000

Expected behavior

A file.blf.gz should be created without error.

Additional context

OS and version: Kubuntu 22.04
Python version: 3.7.13
python-can version: pip install git+https://github.com/hardbyte/python-can@2da28c1a1c87776618a60218b0b97800cf2deb34
python-can interface/s (if applicable):

Traceback and logs

Logger trace back:

(pycan) jackcook@rollo:~$ python -m can.logger -c vcan0 -i socketcan -b 250000 -f file.blf.gz
Connected to SocketcanBus: socketcan channel 'vcan0'
Can Logger (Started on 2022-08-27 10:59:58.127917)
^CTraceback (most recent call last):
  File "/home/jackcook/anaconda3/envs/pycan/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/home/jackcook/anaconda3/envs/pycan/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/jackcook/anaconda3/envs/pycan/lib/python3.7/site-packages/can/logger.py", line 268, in <module>
    main()
  File "/home/jackcook/anaconda3/envs/pycan/lib/python3.7/site-packages/can/logger.py", line 264, in main
    logger.stop()
  File "/home/jackcook/anaconda3/envs/pycan/lib/python3.7/site-packages/can/io/blf.py", line 589, in stop
    self.file.seek(0)
  File "/home/jackcook/anaconda3/envs/pycan/lib/python3.7/gzip.py", line 371, in seek
    raise OSError('Negative seek in write mode')
OSError: Negative seek in write mode

SizedRotatingLogger trace back:

(pycan) jackcook@rollo:~$ python -m can.logger -c vcan0 -i socketcan -b 250000 -f file.blf.gz -s 150000
Connected to SocketcanBus: socketcan channel 'vcan0'
Can Logger (Started on 2022-08-27 11:01:30.452316)
Traceback (most recent call last):
  File "/home/jackcook/anaconda3/envs/pycan/lib/python3.7/site-packages/can/logger.py", line 259, in main
    logger(msg)
  File "/home/jackcook/anaconda3/envs/pycan/lib/python3.7/site-packages/can/listener.py", line 43, in __call__
    self.on_message_received(msg)
  File "/home/jackcook/anaconda3/envs/pycan/lib/python3.7/site-packages/can/io/logger.py", line 206, in on_message_received
    self.do_rollover()
  File "/home/jackcook/anaconda3/envs/pycan/lib/python3.7/site-packages/can/io/logger.py", line 336, in do_rollover
    self.writer.stop()
  File "/home/jackcook/anaconda3/envs/pycan/lib/python3.7/site-packages/can/io/blf.py", line 589, in stop
    self.file.seek(0)
  File "/home/jackcook/anaconda3/envs/pycan/lib/python3.7/gzip.py", line 371, in seek
    raise OSError('Negative seek in write mode')
OSError: Negative seek in write mode

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/jackcook/anaconda3/envs/pycan/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/home/jackcook/anaconda3/envs/pycan/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/jackcook/anaconda3/envs/pycan/lib/python3.7/site-packages/can/logger.py", line 268, in <module>
    main()
  File "/home/jackcook/anaconda3/envs/pycan/lib/python3.7/site-packages/can/logger.py", line 264, in main
    logger.stop()
  File "/home/jackcook/anaconda3/envs/pycan/lib/python3.7/site-packages/can/io/logger.py", line 241, in stop
    self.writer.stop()
  File "/home/jackcook/anaconda3/envs/pycan/lib/python3.7/site-packages/can/io/blf.py", line 589, in stop
    self.file.seek(0)
  File "/home/jackcook/anaconda3/envs/pycan/lib/python3.7/gzip.py", line 371, in seek
    raise OSError('Negative seek in write mode')
OSError: Negative seek in write mode

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions