tibirna / qgit

Official git repository for QGit.
Other
175 stars 68 forks source link

Recover gracefully from corrupted cache situations #71

Closed tibirna closed 3 years ago

tibirna commented 5 years ago

If the cache file (.git/qgit_cache.dat) gets corrupted in some way (e.g. file truncated or crash in the middle of saving it), qgit can't be started anymore on the affected repository, crashing in the cache reading process.

TODO: wrap all cache process in a try...catch... and simply reconstruct cache on any error. If recovery process fails, forego use of cache and inform user for further investigation.

Related issue: #69

yuyichao commented 4 years ago

A related issue and the root cause of the crash is that the cache loading process corrupts the memory when invalid cache is encountered and it may not be good enough to simply retry.

This happens because Cache::load stored a raw pointer from the sha buffer into the global cache (i.e. revsFiles). However, if the reads errors, the caller (Git::loadFileCache) does not keep this buffer alive so as soon as loadFileCache returns the cache is already corrupted and must not be looked at again (every keys in the hash table becomes invalid AFAICT). The solution to this problem would be to unconditionally preserve the shaBuf or revert all changes to the cache immediately.

It also seems that the reader of the shas does not check bound and simply forward the pointer by 41 bytes every cycle. A bounds check should probably be added...

yuyichao commented 4 years ago

And just for the reference, the cause for my corrupted cache file may be openning multiple qgit on the same checkout at the same time, or using complex options to filter the commits (--not, --branches, --remotes=*, etc) or a combination of both. Clearing the cache (or let qgit do it automatically with #103) fixes the problem and trying the command line options I've used before did not reproduce it.

In this case, the fresh cache file is 4.1kB whereas the old one was 1.8MB so the currupted one is clearly not just truncated.