Skip to content

Commit 52e5506

Browse files
committed
fix: Avoid stale var cache and poll for userspace
Force direct device queries and clear cached fastboot vars to avoid stale values after reboot/mode changes. IsUserspace() now calls GetVar(..., useCache:false). After rebooting to fastboot, _varCache is cleared before reconnect to ensure fresh vars. Reboot-to-userspace now polls up to 30s (1s intervals) for the device to report userspace, throwing an error if it doesn't appear, and preserves the existing small delay to match AOSP behavior.
1 parent 345097e commit 52e5506

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

FirmwareKit.Comm.Fastboot/FastbootDriver.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public bool IsUserspace()
3939
FastbootDebug.Log($"IsUserspace()");
4040
try
4141
{
42-
return GetVar("is-userspace") == "yes";
42+
// Always query device directly; cached values can be stale across reboot mode changes.
43+
return GetVar("is-userspace", useCache: false) == "yes";
4344
}
4445
catch
4546
{
@@ -343,6 +344,9 @@ public void EnsureUserspace()
343344
NotifyCurrentStep("Operation requires fastbootd, rebooting...");
344345
Reboot("fastboot").ThrowIfError();
345346

347+
// Clear stale vars (including is-userspace=no) before reconnect and re-check.
348+
_varCache.Clear();
349+
346350
// Match AOSP behavior: allow disconnect to happen before attempting reconnect.
347351
System.Threading.Thread.Sleep(1000);
348352
NotifyCurrentStep("waiting for any device >");
@@ -399,7 +403,20 @@ public void EnsureUserspace()
399403
throw new NotSupportedException("Automatic reboot to userspace is only supported for USB, TCP and UDP transports.");
400404
}
401405

402-
if (!IsUserspace())
406+
DateTime userspaceWaitStart = DateTime.Now;
407+
bool enteredUserspace = false;
408+
while ((DateTime.Now - userspaceWaitStart).TotalSeconds < 30)
409+
{
410+
if (IsUserspace())
411+
{
412+
enteredUserspace = true;
413+
break;
414+
}
415+
416+
System.Threading.Thread.Sleep(1000);
417+
}
418+
419+
if (!enteredUserspace)
403420
{
404421
throw new Exception("Failed to boot into userspace fastboot; one or more components might be unbootable.");
405422
}

0 commit comments

Comments
 (0)