[workspace model] Fix deadlock for the ExternalStorageConfigurationManagerImpl component initialization from the AWT and background thread

GitOrigin-RevId: 3f919a1c185aedb070642fe6a53caa7fb12375a5
This commit is contained in:
Mikhail Mazurkevich
2021-04-09 14:49:51 +00:00
committed by intellij-monorepo-bot
parent d8d61b8c41
commit 23fcadafc1
2 changed files with 18 additions and 18 deletions
@@ -28,34 +28,28 @@ internal class ExternalStorageConfigurationManagerImpl(private val project: Proj
*/
override fun setEnabled(value: Boolean) {
state.enabled = value
updateEntitySource()
if (project.isDefault || !WorkspaceModel.isEnabled) return
val app = ApplicationManager.getApplication()
app.invokeAndWait { app.runWriteAction(::updateEntitySource) }
}
override fun loadState(state: ExternalStorageConfiguration) {
super.loadState(state)
updateEntitySource()
if (project.isDefault || !WorkspaceModel.isEnabled) return
val app = ApplicationManager.getApplication()
app.invokeLater { app.runWriteAction(::updateEntitySource) }
}
private fun updateEntitySource() {
val value = state.enabled
if (project.isDefault || !WorkspaceModel.isEnabled) return
val runnable = Runnable {
WorkspaceModel.getInstance(project).updateProjectModel { updater ->
val entitiesMap = updater.entitiesBySource { it is JpsImportedEntitySource && it.storedExternally != value }
entitiesMap.values.asSequence().flatMap { it.values.asSequence().flatMap { entities -> entities.asSequence() } }.forEach { entity ->
val source = entity.entitySource
if (source is JpsImportedEntitySource) {
updater.changeSource(entity, JpsImportedEntitySource(source.internalFile, source.externalSystemId, value))
}
WorkspaceModel.getInstance(project).updateProjectModel { updater ->
val entitiesMap = updater.entitiesBySource { it is JpsImportedEntitySource && it.storedExternally != value }
entitiesMap.values.asSequence().flatMap { it.values.asSequence().flatMap { entities -> entities.asSequence() } }.forEach { entity ->
val source = entity.entitySource
if (source is JpsImportedEntitySource) {
updater.changeSource(entity, JpsImportedEntitySource(source.internalFile, source.externalSystemId, value))
}
}
}
val app = ApplicationManager.getApplication()
if (!app.isDispatchThread && app.isReadAccessAllowed) {
app.invokeLater { app.runWriteAction(runnable) }
}
else {
WriteAction.runAndWait<RuntimeException> { runnable.run() }
}
}
}
@@ -531,6 +531,9 @@ class ExternalSystemStorageTest {
VfsUtil.markDirtyAndRefresh(false, false, false, miscFile)
StoreReloadManager.getInstance().flushChangedProjectFileAlarm()
}
ApplicationManager.getApplication().invokeAndWait{
PlatformTestUtil.dispatchAllInvocationEventsInIdeEventQueue()
}
}
}
@@ -550,6 +553,9 @@ class ExternalSystemStorageTest {
VfsUtil.markDirtyAndRefresh(false, false, false, miscFile)
StoreReloadManager.getInstance().flushChangedProjectFileAlarm()
}
ApplicationManager.getApplication().invokeAndWait{
PlatformTestUtil.dispatchAllInvocationEventsInIdeEventQueue()
}
}
}