Repository navigation
[flutter_tools] Bound Spotlight mdfind execution with timeout on macOS - #189461
Conversation
On macOS, `AndroidStudio.allInstalled()` calls `mdfind` via `runSync` to locate Android Studio installations outside standard locations. When Spotlight (`mds_stores`/`mdworker`) is unresponsive or re-indexing, `mdfind` hangs indefinitely and blocks `flutter doctor` and `flutter daemon`. Wrap `mdfind` in a shell execution with a 3-second process timeout and killer subshell cleanup to prevent `flutter doctor` and `flutter daemon` from hanging indefinitely. If the query times out or fails, `_allMacOS` gracefully falls back to standard candidate paths. Fixes flutter#189177
There was a problem hiding this comment.
Code Review
This pull request wraps the macOS Spotlight mdfind query in a shell execution with a 3-second timeout to prevent hanging when Spotlight is unresponsive, and adds a test to verify that a timed-out query is handled gracefully. Feedback suggests only assigning the query result when the process exits successfully with an exit code of 0 to avoid processing partial or corrupted output.
| if (spotlightResult.exitCode != 0) { | ||
| globals.printTrace( | ||
| 'Spotlight mdfind query failed or timed out with exit code ${spotlightResult.exitCode}', | ||
| ); | ||
| } | ||
| spotlightQueryResult = spotlightResult.stdout as String; |
There was a problem hiding this comment.
If the mdfind query fails or times out (resulting in a non-zero exit code), spotlightResult.stdout might contain partial, corrupted, or empty output. It is safer to only assign spotlightQueryResult when the process exits successfully (exit code 0), and otherwise leave it as an empty string to avoid processing invalid paths.
if (spotlightResult.exitCode == 0) {
spotlightQueryResult = spotlightResult.stdout as String;
} else {
globals.printTrace(
'Spotlight mdfind query failed or timed out with exit code ${spotlightResult.exitCode}',
);
}|
|
||
| @visibleForTesting | ||
| const String kSpotlightMdfindCommand = | ||
| 'mdfind \'kMDItemCFBundleIdentifier="com.google.android.studio*"\' & pid=\$!; (sleep 3; kill -9 \$pid 2>/dev/null) & killer=\$!; wait \$pid 2>/dev/null; status=\$?; kill \$killer 2>/dev/null; exit \$status'; |
There was a problem hiding this comment.
At the very top of our styleguide it says "Never use timeouts or timers". However, sometimes we do when the OS tooling underneath us does hang, ot itself require a timeout (like wireless device discovery).
@vashworth any better ideas for this, given Spotlight caching nonsense?
There was a problem hiding this comment.
Not really, unfortunately. I'm kind of surprised we use Spotlight in the first place. Looking at the code, it looks like this is used for more than just doctor, too. It's called anytime you use androidStudio or java from the context. The code says it's a "nice-to-have", but it may be confusing to users if sometimes it can find it and sometimes it can't.
I'd be more for just removing it out entirely and prompting the user to use the config if it's purposely put in a non-standard location. Or if it does find it, prompting them to save it to the config so it doesn't continuously use Spotlight after it finds it the first time.
I think that's up to the Android team, though
gmackall
left a comment
There was a problem hiding this comment.
Lgtm if we want to keep this path (I don't have strong opinions, given that (IIUC) it's a fallback after standard install directory and explicit config directory).
…Spotlight to locate Android Studio on macOS (flutter#189952) flutter#189461 introduced a script in the flutter_tools AndroidStudio class that wraps the Spotlight mdfind command with a timeout. This script had been waiting for the full 3 second timeout even if the mdfind process exited before then. The extra wait showed up in performance benchmarks of tasks that invoke AndroidStudio such as app compilation. See flutter#189177
flutter/flutter@2a2a79d...b65f4d9 2026-07-24 [email protected] Add dart_runtime_service_vm_aot.dart.snapshot to the snapshot list in the macOS code signing configuration (flutter/flutter#189981) 2026-07-24 [email protected] Roll Fuchsia Test Scripts from wLST_A-xfOeGT_5mj... to E8hJ1AfK8CtGtaES0... (flutter/flutter#189956) 2026-07-23 [email protected] Consolidate AndroidArch and DarwinArch into CpuArch (flutter/flutter#189315) 2026-07-23 [email protected] Roll Dart SDK from 9258584f98b8 to e3fc57eae9eb (7 revisions) (flutter/flutter#189949) 2026-07-23 [email protected] [flutter_tools] Do not always wait for the full timeout when running Spotlight to locate Android Studio on macOS (flutter/flutter#189952) 2026-07-23 [email protected] Roll Skia from 1d8bf9270d8c to 6e9c4687c001 (15 revisions) (flutter/flutter#189954) 2026-07-23 [email protected] [flutter_tools] Initialize Cache.flutterRoot at the start of the upgrade_test suite (flutter/flutter#189937) 2026-07-23 [email protected] Parse AndroidX property in gradle.properties (flutter/flutter#188372) 2026-07-23 [email protected] [Impeller]Use the IO context for OpenGL program setup (flutter/flutter#185723) 2026-07-23 [email protected] Allow building projects lacking Runner.xcworkspace (flutter/flutter#186239) 2026-07-23 [email protected] [flutter_tools] Invalidate WebEntrypointTarget when plugin set changes (flutter/flutter#189460) 2026-07-23 [email protected] Bump devtools_shared to 13.1.0 (flutter/flutter#189507) 2026-07-23 [email protected] forceNdkDownload should skip configuring cmake when ndk-build is used (flutter/flutter#187201) 2026-07-23 [email protected] Disable execution order shuffling for the flutter_tools upgrade_test suite (flutter/flutter#189920) 2026-07-23 [email protected] Move WindowManager outside of WidgetsApp (flutter/flutter#188866) 2026-07-23 [email protected] Roll Skia from 3424966b8a2b to 1d8bf9270d8c (3 revisions) (flutter/flutter#189901) 2026-07-23 [email protected] Roll Fuchsia Linux SDK from GswhlPRO-D1qSNclx... to 9org0yL3yZkp80x5S... (flutter/flutter#189898) 2026-07-23 [email protected] Remove outdated logs that were added to track #172636 (flutter/flutter#189282) 2026-07-23 [email protected] Roll Skia from 5e183e5aeac5 to 3424966b8a2b (33 revisions) (flutter/flutter#189890) 2026-07-23 [email protected] [examples] Use super parameters in missed spots (flutter/flutter#186194) 2026-07-23 [email protected] [flutter_tools] Bound Spotlight mdfind execution with timeout on macOS (flutter/flutter#189461) 2026-07-23 [email protected] Fix non primary buttons not being captured on windows (flutter/flutter#188394) 2026-07-22 [email protected] Listen to log reader before VM Service and make delay configurable (flutter/flutter#187202) 2026-07-22 [email protected] Roll Dart SDK from 1e65011ee004 to 9258584f98b8 (7 revisions) (flutter/flutter#189883) 2026-07-22 [email protected] Adds skill for generating engine diffs for new releases. (flutter/flutter#189869) 2026-07-22 [email protected] [ios,macos] Add Swift Sourcekit LSP support (flutter/flutter#189761) 2026-07-22 [email protected] [iOS] Mark DisplayLinkManager.shared and init() @mainactor (flutter/flutter#189815) 2026-07-22 [email protected] Fix `Rect::ExpandToMinTransformedSize` to return the input rectangle when no expansion is needed, and remove 1-pixel roundrect to rect simplification (flutter/flutter#189808) 2026-07-22 [email protected] Skip emulator.getEmulators test (flutter/flutter#189879) 2026-07-22 [email protected] Fix null terminator in input truncates clipboard (flutter/flutter#188652) If this roll has caused a breakage, revert this CL and stop the roller using the controls here: https://autoroll.skia.org/r/flutter-packages Please CC [email protected],[email protected] on the revert to ensure that a human is aware of the problem. To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose To report a problem with the AutoRoller itself, please file a bug: https://issues.skia.org/issues/new?component=1389291&template=1850622 Documentation for the AutoRoller is here: https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
Description
On macOS,
AndroidStudio.allInstalled()callsmdfindviarunSyncto locate Android Studio installations outside standard locations. When Spotlight (mds_stores/mdworker) is unresponsive or re-indexing,mdfindhangs indefinitely and blocksflutter doctorandflutter daemon.This PR wraps
mdfindin a shell execution with a 3-second process timeout and killer subshell cleanup to preventflutter doctorandflutter daemonfrom hanging indefinitely. If the query times out or fails,_allMacOSgracefully falls back to standard candidate paths.Fixes #189177
Tests
installation detection on MacOS gracefully handles unresponsive Spotlight query (issue #189177)unit test inpackages/flutter_tools/test/general.shard/android/android_studio_test.dart.