Skip to content

Commit d7c8cc9

Browse files
committed
Refactor console attachment handling in AppiumLocalService
Added a boolean variable to track console attachment status. Simplified error handling by removing redundant try-catch blocks and ensuring proper cleanup in the finally block.
1 parent fed3efb commit d7c8cc9

File tree

1 file changed

+6
-18
lines changed

1 file changed

+6
-18
lines changed

src/Appium.Net/Appium/Service/AppiumLocalService.cs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -207,46 +207,34 @@ private bool TryGracefulShutdownOnWindows(Process process, int timeoutMs = 5000)
207207
return false;
208208
}
209209

210+
bool attached = false;
210211
try
211212
{
212213
SetConsoleCtrlHandler(null, true);
213214

214-
if (!AttachConsole((uint)process.Id))
215+
attached = AttachConsole((uint)process.Id);
216+
if (!attached)
215217
{
216218
return false;
217219
}
218220

219221
if (!GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0))
220222
{
221-
FreeConsole();
222223
return false;
223224
}
224225

225-
FreeConsole();
226-
SetConsoleCtrlHandler(null, false);
227-
228226
if (process.WaitForExit(timeoutMs))
229227
{
230228
return true;
231229
}
232230
}
233-
catch (Win32Exception)
234-
{
235-
try
236-
{
237-
FreeConsole();
238-
SetConsoleCtrlHandler(null, false);
239-
}
240-
catch (Win32Exception) { }
241-
}
242-
catch (InvalidOperationException)
231+
finally
243232
{
244-
try
233+
if (attached)
245234
{
246235
FreeConsole();
247-
SetConsoleCtrlHandler(null, false);
248236
}
249-
catch (Win32Exception) { }
237+
SetConsoleCtrlHandler(null, false);
250238
}
251239

252240
return false;

0 commit comments

Comments
 (0)