bgreenlee / pygtail

Pygtail reads log file lines that have not been read. It will even handle log files that have been rotated. Based on logcheck's logtail2 (http://logcheck.org)
GNU General Public License v2.0
251 stars 79 forks source link

Race condition when logfile is rotated while pygtail is running #71

Open pajowu opened 9 months ago

pajowu commented 9 months ago

There is at least one race condition when creating a pygtail instance at the same time a logfile is rotated, which can lead to impossible offset files being created. A test-case for this can be found in this commit: https://github.com/bgreenlee/pygtail/commit/98d3d50986cf4b2c9ec5df8b3ec97e8273a1ea72

This is caused by __init__ determining the name of the logfile referenced by offset_file_inode, but the file only being opened once the first line is read. If the file was rotated in between, _filehandle will open the newer logfile, but try to seek to the old offset, which in most cases is after the end of the file, so the read will fail. However when writing the offset, tell() is used, which will return the offset that was seeked to, not the length of the new file -> We end up with the inode of the new file, but the old offset being written to the offset file.