Fix race conditions in symbol library preloading

Two race conditions may have caused intermittent false "Library not found in
library table" errors when "remember open files" was enabled.  First,
PreloadLibraries had a gap between checking and setting the in-progress flag and
second, SYMBOL_LIBRARY_ADAPTER::AsyncLoad didn't have a mutex

Fixes https://gitlab.com/kicad/code/kicad/-/issues/22764
This commit is contained in:
Seth Hillbrand
2026-01-19 07:55:58 -08:00
parent 544b3b60b0
commit 9c0e406e94
2 changed files with 11 additions and 2 deletions
+6 -2
View File
@@ -482,7 +482,12 @@ void IFACE::PreloadLibraries( KIWAY* aKiway )
wxCHECK( aKiway, /* void */ );
if( m_libraryPreloadInProgress.load() )
// Use compare_exchange to atomically check and set the flag to prevent race conditions
// when PreloadLibraries is called multiple times concurrently (e.g., from project manager
// and schematic editor both scheduling via CallAfter)
bool expected = false;
if( !m_libraryPreloadInProgress.compare_exchange_strong( expected, true ) )
return;
Pgm().ClearLibraryLoadMessages();
@@ -566,7 +571,6 @@ void IFACE::PreloadLibraries( KIWAY* aKiway )
};
thread_pool& tp = GetKiCadThreadPool();
m_libraryPreloadInProgress.store( true );
m_libraryPreloadReturn = tp.submit_task( preload );
}
@@ -295,6 +295,11 @@ bool SYMBOL_LIBRARY_ADAPTER::IsSymbolLibWritable( const wxString& aLib )
void SYMBOL_LIBRARY_ADAPTER::AsyncLoad()
{
std::unique_lock<std::mutex> asyncLock( m_loadMutex, std::try_to_lock );
if( !asyncLock )
return;
// TODO(JE) any reason to clean these up earlier?
std::erase_if( m_futures,
[]( const std::future<void>& aFuture )