Improved timeout handling for ADB connect command and suppressed devices command log spam

Enhanced RunAdbCommandToString to use a 5-second timeout for connect commands, preventing prolonged blocking during startup/initialization when an ADB wireless config exists but fails to connect to the device. Also added a suppressLogging parameter to control 'devices' command output logging, which is used for our quick 'devices' check command that runs every second while no devices are connected
This commit is contained in:
jp64k
2025-12-30 17:23:00 +01:00
parent 5819bc8083
commit 02ab0f0e2a
2 changed files with 38 additions and 15 deletions

51
ADB.cs
View File

@@ -72,7 +72,7 @@ namespace AndroidSideloader
return _currentDevice;
}
public static ProcessOutput RunAdbCommandToString(string command)
public static ProcessOutput RunAdbCommandToString(string command, bool suppressLogging = false)
{
command = command.Replace("adb", "");
@@ -85,7 +85,7 @@ namespace AndroidSideloader
command = $" -s {DeviceID} {command}";
}
if (!command.Contains("dumpsys") && !command.Contains("shell pm list packages") && !command.Contains("KEYCODE_WAKEUP"))
if (!suppressLogging && !command.Contains("dumpsys") && !command.Contains("shell pm list packages") && !command.Contains("KEYCODE_WAKEUP") )
{
string logcmd = command;
if (logcmd.Contains(Environment.CurrentDirectory))
@@ -95,6 +95,9 @@ namespace AndroidSideloader
_ = Logger.Log($"Running command: {logcmd}");
}
bool isConnectCommand = command.Contains("connect");
int timeoutMs = isConnectCommand ? 5000 : -1; // 5 second timeout for connect commands
using (Process adb = new Process())
{
adb.StartInfo.FileName = adbFilePath;
@@ -111,19 +114,39 @@ namespace AndroidSideloader
try
{
output = adb.StandardOutput.ReadToEnd();
error = adb.StandardError.ReadToEnd();
}
catch { }
if (command.Contains("connect"))
{
bool graceful = adb.WaitForExit(3000);
if (!graceful)
if (isConnectCommand)
{
adb.Kill();
adb.WaitForExit();
// For connect commands, we use async reading with timeout to avoid blocking on TCP timeout
var outputTask = adb.StandardOutput.ReadToEndAsync();
var errorTask = adb.StandardError.ReadToEndAsync();
bool exited = adb.WaitForExit(timeoutMs);
if (!exited)
{
try { adb.Kill(); } catch { }
adb.WaitForExit(1000);
output = "Connection timed out";
error = "cannot connect: Connection timed out";
Logger.Log($"ADB connect command timed out after {timeoutMs}ms", LogLevel.WARNING);
}
else
{
// Process exited within timeout, safe to read output
output = outputTask.Result;
error = errorTask.Result;
}
}
else
{
// For non-connect commands, read output normally
output = adb.StandardOutput.ReadToEnd();
error = adb.StandardError.ReadToEnd();
}
}
catch (Exception ex)
{
Logger.Log($"Error reading ADB output: {ex.Message}", LogLevel.WARNING);
}
if (error.Contains("ADB_VENDOR_KEYS") && !settings.AdbDebugWarned)
@@ -134,7 +157,7 @@ namespace AndroidSideloader
{
_ = FlexibleMessageBox.Show(Program.form, "There is not enough room on your device to install this package. Please clear AT LEAST 2x the amount of the app you are trying to install.");
}
if (!output.Contains("version") && !output.Contains("KEYCODE_WAKEUP") && !output.Contains("Filesystem") && !output.Contains("package:") && !output.Equals(null))
if (!suppressLogging && !output.Contains("version") && !output.Contains("KEYCODE_WAKEUP") && !output.Contains("Filesystem") && !output.Contains("package:") && !output.Equals(null))
{
_ = Logger.Log(output);
}

View File

@@ -7436,7 +7436,7 @@ function onYouTubeIframeAPIReady() {
// Run a quick device check in background
try
{
string output = await Task.Run(() => ADB.RunAdbCommandToString("devices").Output);
string output = await Task.Run(() => ADB.RunAdbCommandToString("devices", suppressLogging: true).Output);
string[] lines = output.Split('\n');
bool hasDeviceNow = false;