Fix memory corruption in ScanFolder plugin
ClosedPublic

Authored by valeriymalov on Sep 4 2018, 11:59 AM.

Details

Summary

Reasoning behind this patch is kinda dubious because I don't fully understand what happens, but it shouldn't make the matters worse either

Seems like deletion of KDirWatch objects from scan_folder in ~ScanThread(), after said QThread has been stopped, causes memory corruption.

Preemptively deleting scan_folder contents before we stop the thread resolves this.

also mark booleans shared across main thread and KDirWatch callbacks as atomic

Test Plan

Without this patch unloading ScanFolder plugin sometimes crashes ktorrent, including unloading on exit
Doesn't happen anymore with this patch

Diff Detail

Repository
R473 KTorrent
Branch
master
Lint
No Linters Available
Unit
No Unit Test Coverage
Build Status
Buildable 2507
Build 2525: arc lint + arc unit
valeriymalov requested review of this revision.Sep 4 2018, 11:59 AM
valeriymalov created this revision.
stikonas requested changes to this revision.Sep 4 2018, 6:14 PM

Can you fix a spelling mistake in the comment? (casuses)

This revision now requires changes to proceed.Sep 4 2018, 6:14 PM
stikonas accepted this revision.EditedSep 4 2018, 9:27 PM

Well, I guess good to go since it fixes crashes on exit. I wonder if it's the same crash on exit many people were getting but I can't reproduce.

This revision is now accepted and ready to land.Sep 4 2018, 9:27 PM

By the way, is there any backtrace? Just so that we can compare it to some of those crash on exit bugs on bugzilla... (e.g. https://bugs.kde.org/show_bug.cgi?id=384476)

Backtraces are a bit inconsistent, usually it ends with malloc aborting with invalid porinter or other memory error

drknoqi, unloading https://paste.kde.org/pxhztgrux
drkonqi, shutdown https://paste.kde.org/pbn4epc8x
gdb, unloading https://paste.kde.org/pkfhrklpe
gdb, shutdown https://paste.kde.org/pevqcecvm

I would also occasionally get segfault inside KDirWatch instead of malloc error but I can't reproduce it now
A telltale for this crash is if it happens when user loads/unloads scanfolder plugin multiple times or if it doesn't happen when scanfolder isn't enabled in the first place