SpartanJ / efsw

efsw is a C++ cross-platform file system watcher and notifier.
Other
662 stars 101 forks source link

[WIP]fix invalid watcher iterator while found before removing it from map … #154

Open ashione opened 1 year ago

ashione commented 1 year ago
{
{
Lock lock( mWatchesLock);
wit = mWatches.find( pevent->wd);
}
wit->second;
}

iterator wit might be an invalid if this iterator has been removed from map collection after find method.

ashione commented 1 year ago

I found it fix invalid iterator issue but there is another deadlock condition:

SpartanJ commented 1 year ago

Hi @ashione, thanks for collaborating! I think this can generate a deadlock as you mentioned, let me check it a little bit. Maybe we can get the iterator end inside the lock, something like:

bool foundIt = false;
{
    Lock lock( mWatchesLock );

    wit = mWatches.find( pevent->wd );
    foundIt = wit != mWatches.end();
}

if ( foundIt ) {
ashione commented 1 year ago

Hi @ashione, thanks for collaborating! I think this can generate a deadlock as you mentioned, let me check it a little bit. Maybe we can get the iterator end inside the lock, something like:

bool foundIt = false;
{
  Lock lock( mWatchesLock );

  wit = mWatches.find( pevent->wd );
  foundIt = wit != mWatches.end();
}

if ( foundIt ) {

Actually, we use this data in handleAction after iterator located but related inotify pointer had been deleted. How to avoid accessing invalid iterator out of thie scope lock domain?