efsw icon indicating copy to clipboard operation
efsw copied to clipboard

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

Open ashione opened this issue 1 year ago • 3 comments

{
{
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 avatar Mar 06 '23 03:03 ashione

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

  • run & while function holds mWatcherLock, then handleAction wants minitLock
  • removeWatcher function hodls mInitLock, but requires mWatcherLock

ashione avatar Mar 06 '23 03:03 ashione

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 ) {

SpartanJ avatar Mar 06 '23 04:03 SpartanJ

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?

ashione avatar Mar 06 '23 06:03 ashione