Skip to content

Conversation

@m3d
Copy link
Member

@m3d m3d commented Aug 21, 2025

This bug made me crazy for "years" :( ... if you kill some recording with Ctrl+C where there are not many data typically everything was lost (only 16 bytes header was available). This solves the problem (tested with LoRa base-station logger).

@m3d m3d requested a review from tajgr August 21, 2025 20:07
@tajgr
Copy link
Collaborator

tajgr commented Aug 22, 2025

It is working but i tested it on systemmonitor driver and there is a bug in dmesg parsing. I got:

python -m osgar.record config/test-system-monitor.json 
2025-08-22 10:57:50,052 __main__         INFO     /home/jakub/git/logs_osgar/test-system-monitor-250822_085750.log
2025-08-22 10:57:50,064 __main__         INFO     SIGINT handler installed
dmesg: čtení z jaderného bufferu selhalo: Operace není povolena
Exception in thread Thread-1 (run_input):
Traceback (most recent call last):
  File "/home/jakub/miniconda3/envs/osgar/lib/python3.10/threading.py", line 1009, in _bootstrap_inner
    self.run()
  File "/home/jakub/miniconda3/envs/osgar/lib/python3.10/threading.py", line 946, in run
    self._target(*self._args, **self._kwargs)
  File "/home/jakub/git/osgar/osgar/drivers/system_monitor.py", line 91, in run_input
    dmesg = self.process_dmesg(dmesg_all)
  File "/home/jakub/git/osgar/osgar/drivers/system_monitor.py", line 43, in process_dmesg
    dmesg_time = self.find_last_dmesg_time(dmesg_all)
  File "/home/jakub/git/osgar/osgar/drivers/system_monitor.py", line 34, in find_last_dmesg_time
    dmesg_time = get_timestamp_from_dmesg(dmesg_all[ii])
IndexError: list index out of range
Traceback (most recent call last):
  File "/home/jakub/miniconda3/envs/osgar/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/home/jakub/miniconda3/envs/osgar/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/home/jakub/git/osgar/osgar/record.py", line 106, in <module>
    main()
  File "/home/jakub/git/osgar/osgar/record.py", line 102, in main
    record(cfg, log_prefix=prefix, duration_sec=args.duration, log_filename=args.log)
  File "/home/jakub/git/osgar/osgar/record.py", line 68, in record
    with LogWriter(prefix=log_prefix, filename=log_filename, note=str(sys.argv)) as log:
  File "/home/jakub/git/osgar/osgar/logger.py", line 158, in __exit__
    self.close()
  File "/home/jakub/git/osgar/osgar/logger.py", line 149, in close
    self.f.close()
AttributeError: 'NoneType' object has no attribute 'close'

The logger was probably already closed. So, add a check that logger is not None?

@m3d
Copy link
Member Author

m3d commented Aug 22, 2025

OK, thanks for test. I can confirm that if I use duration if crashes (with Ctrl+C it is OK). Your case seems to be related to sudo??

(osgar) md@md-ThinkPad-P50:~/git/osgar$ python -m osgar.record config/test-system-monitor.json --duration 10
2025-08-22 20:25:11,327 root             WARNING  Environment variable OSGAR_LOGS is not set - using working directory
2025-08-22 20:25:11,328 __main__         INFO     /home/md/git/osgar/test-system-monitor-250822_182511.log
2025-08-22 20:25:11,339 __main__         INFO     SIGINT handler installed
Traceback (most recent call last):
  File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/md/git/osgar/osgar/record.py", line 106, in <module>
    main()
  File "/home/md/git/osgar/osgar/record.py", line 102, in main
    record(cfg, log_prefix=prefix, duration_sec=args.duration, log_filename=args.log)
  File "/home/md/git/osgar/osgar/record.py", line 77, in record
    recorder.request_stop()
  File "/home/md/git/osgar/osgar/logger.py", line 158, in __exit__
    self.close()
  File "/home/md/git/osgar/osgar/logger.py", line 149, in close
    self.f.close()
AttributeError: 'NoneType' object has no attribute 'close'

@m3d
Copy link
Member Author

m3d commented Aug 22, 2025

well, I do not like this hack with multiple calls of close(), but ... if you have any suggestion, please let me know

@tajgr
Copy link
Collaborator

tajgr commented Aug 23, 2025

Maybe add to LogWriter some variable, e.g. log_closed and than check whereas it is ture. But current solution is working.

@m3d
Copy link
Member Author

m3d commented Aug 24, 2025

well, log_closed is identical to file descriptor to be None, or not?

@tajgr
Copy link
Collaborator

tajgr commented Aug 24, 2025

Yes, actually. You can ask if the file is None or not..

def close(self):
self.f.close()
self.f = None
if self.f is not None:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, this is the place where I ask, or maybe I do not understand? (note, that logger is shared among multiple modules)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea was move the if to the record. It could be maybe clearer.. but you can ignore it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, thanks

@m3d m3d merged commit 50f6e26 into master Aug 24, 2025
4 checks passed
@m3d m3d deleted the bugfix/record-flush-log-data branch August 24, 2025 11:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants