Skip to content

Commit dd9096a

Browse files
authored
Merge pull request #78856 from artemcm/FixRaceOnSwiftDepLookup
[Dependency Scanning] Synchronize on updating Swift dependency lookup results in-parallel
2 parents 694ee2d + ae98eae commit dd9096a

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

lib/DependencyScan/ModuleDependencyScanner.cpp

+13-6
Original file line numberDiff line numberDiff line change
@@ -926,16 +926,19 @@ void ModuleDependencyScanner::resolveSwiftImportsForModule(
926926
for (const auto &dependsOn : moduleDependencyInfo.getModuleImports())
927927
moduleLookupResult.insert(
928928
std::make_pair(dependsOn.importIdentifier, std::nullopt));
929+
std::mutex lookupResultLock;
929930

930931
// A scanning task to query a module by-name. If the module already exists
931932
// in the cache, do nothing and return.
932933
auto scanForSwiftModuleDependency =
933-
[this, &cache, &moduleLookupResult](Identifier moduleIdentifier,
934-
bool isTestable) {
934+
[this, &cache, &lookupResultLock, &moduleLookupResult](Identifier moduleIdentifier,
935+
bool isTestable) {
935936
auto moduleName = moduleIdentifier.str().str();
936-
// If this is already in the cache, no work to do here
937-
if (cache.hasSwiftDependency(moduleName))
938-
return;
937+
{
938+
std::lock_guard<std::mutex> guard(lookupResultLock);
939+
if (cache.hasSwiftDependency(moduleName))
940+
return;
941+
}
939942

940943
auto moduleDependencies = withDependencyScanningWorker(
941944
[&cache, moduleIdentifier,
@@ -944,7 +947,11 @@ void ModuleDependencyScanner::resolveSwiftImportsForModule(
944947
moduleIdentifier, cache.getModuleOutputPath(),
945948
cache.getScanService().getPrefixMapper(), isTestable);
946949
});
947-
moduleLookupResult.insert_or_assign(moduleName, moduleDependencies);
950+
951+
{
952+
std::lock_guard<std::mutex> guard(lookupResultLock);
953+
moduleLookupResult.insert_or_assign(moduleName, moduleDependencies);
954+
}
948955
};
949956

950957
// Enque asynchronous lookup tasks

0 commit comments

Comments
 (0)