Skip to content

Resolves a "Slow operations are prohibited on EDT" exception #8457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions src/io/flutter/sdk/FlutterSdkUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,26 +96,39 @@ private static void updateKnownPaths(@SuppressWarnings("SameParameterValue") @No
* Adds the current path and other known paths to the combo, most recently used first.
*/
public static void addKnownSDKPathsToCombo(@NotNull JComboBox combo) {
final Set<String> pathsToShow = new LinkedHashSet<>();

// First, get the current path from the combo box on the EDT.
final String currentPath = combo.getEditor().getItem().toString().trim();
final Set<String> pathsToShow = new LinkedHashSet<>();
if (!currentPath.isEmpty()) {
pathsToShow.add(currentPath);
}

final String[] knownPaths = getKnownFlutterSdkPaths();
for (String path : knownPaths) {
if (FlutterSdk.forPath(path) != null) {
pathsToShow.add(FileUtil.toSystemDependentName(path));
// Now, run the slow operation (finding valid SDK paths) on a background thread.
ApplicationManager.getApplication().executeOnPooledThread(() -> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat! I'll have to read up on how this thread pool is managed and what kinds of operations are best run on it. Maybe this is the approach we should take elsewhere?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is part of it, I was reading up on coalesceBy, see the message in #8242, which I think will be the right approach.

final String[] knownPaths = getKnownFlutterSdkPaths();
final Set<String> validPaths = new LinkedHashSet<>();
for (String path : knownPaths) {
// The call to forPath() is on a background thread.
if (FlutterSdk.forPath(path) != null) {
validPaths.add(FileUtil.toSystemDependentName(path));
}
}
}

//noinspection unchecked
combo.setModel(new DefaultComboBoxModel(ArrayUtil.toStringArray(pathsToShow)));
// After the slow operation is complete, switch back to the EDT to update the UI.
ApplicationManager.getApplication().invokeLater(() -> {
// This code runs on the EDT.
pathsToShow.addAll(validPaths);

if (combo.getSelectedIndex() == -1 && combo.getItemCount() > 0) {
combo.setSelectedIndex(0);
}
// Update the combo box model with the new paths.
//noinspection unchecked
combo.setModel(new DefaultComboBoxModel<>(ArrayUtil.toStringArray(pathsToShow)));

// Select the first item if none is selected.
if (combo.getSelectedIndex() == -1 && combo.getItemCount() > 0) {
combo.setSelectedIndex(0);
}
});
});
}

@NotNull
Expand Down Expand Up @@ -365,7 +378,6 @@ public static String parseFlutterSdkPath(String packagesFileContent) {
return path;
}
}

return null;
}

Expand Down