diff --git a/packages/flutter_tools/lib/src/desktop.dart b/packages/flutter_tools/lib/src/desktop.dart index e62c4e779c..26773a2073 100644 --- a/packages/flutter_tools/lib/src/desktop.dart +++ b/packages/flutter_tools/lib/src/desktop.dart @@ -29,9 +29,15 @@ Future killProcess(String executable) async { if (values.length < 2) { continue; } - final String pid = values[1]; + final String processPid = values[1]; + final String currentRunningProcessPid = pid.toString(); + // Don't kill the flutter tool process + if (processPid == currentRunningProcessPid) { + continue; + } + final ProcessResult killResult = await processManager.run([ - 'kill', pid, + 'kill', processPid, ]); succeeded &= killResult.exitCode == 0; } diff --git a/packages/flutter_tools/lib/src/windows/windows_device.dart b/packages/flutter_tools/lib/src/windows/windows_device.dart index f2e5e10309..462223bf47 100644 --- a/packages/flutter_tools/lib/src/windows/windows_device.dart +++ b/packages/flutter_tools/lib/src/windows/windows_device.dart @@ -156,7 +156,7 @@ class WindowsDevices extends PollingDeviceDiscovery { Future> getDiagnostics() async => const []; } -final RegExp _whitespace = RegExp(r'\w+'); +final RegExp _whitespace = RegExp(r'\s+'); /// Returns the running process matching `process` name. /// @@ -174,8 +174,15 @@ List runningProcess(String processName) { continue; } final List parts = process.split(_whitespace); + + final String processPid = parts[0]; + final String currentRunningProcessPid = pid.toString(); + // Don't kill the flutter tool process + if (processPid == currentRunningProcessPid) { + continue; + } final List data = [ - parts[0], // ID + processPid, // ID parts[1], // Name ]; return data; diff --git a/packages/flutter_tools/test/general.shard/linux/linux_device_test.dart b/packages/flutter_tools/test/general.shard/linux/linux_device_test.dart index 29a56131f2..c25d2e9106 100644 --- a/packages/flutter_tools/test/general.shard/linux/linux_device_test.dart +++ b/packages/flutter_tools/test/general.shard/linux/linux_device_test.dart @@ -22,16 +22,21 @@ void main() { final LinuxDevice device = LinuxDevice(); final MockPlatform notLinux = MockPlatform(); final MockProcessManager mockProcessManager = MockProcessManager(); + const String flutterToolCommand = 'flutter --someoption somevalue'; when(notLinux.isLinux).thenReturn(false); when(mockProcessManager.run([ 'ps', 'aux', ])).thenAnswer((Invocation invocation) async { + // The flutter tool process is returned as output to the ps aux command final MockProcessResult result = MockProcessResult(); when(result.exitCode).thenReturn(0); - when(result.stdout).thenReturn(''); + when(result.stdout).thenReturn('username $pid $flutterToolCommand'); return result; }); + when(mockProcessManager.run([ + 'kill', '$pid', + ])).thenThrow(Exception('Flutter tool process has been killed')); testUsingContext('defaults', () async { final PrebuiltLinuxApp linuxApp = PrebuiltLinuxApp(executable: 'foo'); @@ -55,6 +60,14 @@ void main() { expect(portForwarder.forwardedPorts.isEmpty, true); }); + testUsingContext('The current running process is not killed when stopping the app', () async { + // The name of the executable is the same as a command line argument to the flutter tool + final PrebuiltLinuxApp linuxApp = PrebuiltLinuxApp(executable: 'somevalue'); + expect(await device.stopApp(linuxApp), true); + }, overrides: { + ProcessManager: () => mockProcessManager, + }); + testUsingContext('No devices listed if platform unsupported', () async { expect(await LinuxDevices().devices, []); }, overrides: { diff --git a/packages/flutter_tools/test/general.shard/macos/macos_device_test.dart b/packages/flutter_tools/test/general.shard/macos/macos_device_test.dart index 2dcb59edd7..b826dac45b 100644 --- a/packages/flutter_tools/test/general.shard/macos/macos_device_test.dart +++ b/packages/flutter_tools/test/general.shard/macos/macos_device_test.dart @@ -96,6 +96,25 @@ tester 17193 0.0 0.2 4791128 37820 ?? S 2:27PM 0:00.09 /Applica FileSystem: () => mockFileSystem, ProcessManager: () => mockProcessManager, }); + + testUsingContext('The current running process is not killed when stopping the app', () async { + final String psOut = ''' +tester $pid 0.0 0.2 4791128 37820 ?? S 2:27PM 0:00.09 flutter run --use-application-binary /Applications/foo +'''; + final MockMacOSApp mockMacOSApp = MockMacOSApp(); + // The name of the executable is the same as a command line argument to the flutter tool + when(mockMacOSApp.executable(any)).thenReturn('/Applications/foo'); + when(mockProcessManager.run(['ps', 'aux'])).thenAnswer((Invocation invocation) async { + return ProcessResult(1, 0, psOut, ''); + }); + when(mockProcessManager.run([ + 'kill', '$pid', + ])).thenThrow(Exception('Flutter tool process has been killed')); + + expect(await device.stopApp(mockMacOSApp), true); + }, overrides: { + ProcessManager: () => mockProcessManager, + }); }); test('noop port forwarding', () async { diff --git a/packages/flutter_tools/test/general.shard/windows/windows_device_test.dart b/packages/flutter_tools/test/general.shard/windows/windows_device_test.dart index 29fa6796b3..c1d650fc08 100644 --- a/packages/flutter_tools/test/general.shard/windows/windows_device_test.dart +++ b/packages/flutter_tools/test/general.shard/windows/windows_device_test.dart @@ -22,17 +22,22 @@ void main() { final WindowsDevice device = WindowsDevice(); final MockPlatform notWindows = MockPlatform(); final MockProcessManager mockProcessManager = MockProcessManager(); + const String flutterToolBinary = 'flutter.exe'; when(notWindows.isWindows).thenReturn(false); when(notWindows.environment).thenReturn(const {}); when(mockProcessManager.runSync([ 'powershell', '-script="Get-CimInstance Win32_Process"' ])).thenAnswer((Invocation invocation) { + // The flutter tool process is returned as output to the powershell script final MockProcessResult result = MockProcessResult(); when(result.exitCode).thenReturn(0); - when(result.stdout).thenReturn(''); + when(result.stdout).thenReturn('$pid $flutterToolBinary'); return result; }); + when(mockProcessManager.run([ + 'Taskkill', '/PID', '$pid', '/F' + ])).thenThrow(Exception('Flutter tool process has been killed')); testUsingContext('defaults', () async { final PrebuiltWindowsApp windowsApp = PrebuiltWindowsApp(executable: 'foo'); @@ -82,6 +87,14 @@ void main() { }, overrides: { FileSystem: () => MemoryFileSystem(), }); + + testUsingContext('The current running process is not killed when stopping the app', () async { + // The name of the executable is the same as the flutter tool one + final PrebuiltWindowsApp windowsApp = PrebuiltWindowsApp(executable: flutterToolBinary); + expect(await device.stopApp(windowsApp), false); + }, overrides: { + ProcessManager: () => mockProcessManager, + }); }); }