Skip to content

Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.

Fix for #2663: when the batch dies mid-list, disable.sh still reads back what is enabled and fails the run

Gathin235326ab194afb
diff --git a/tests/test_disable_verify.py b/tests/test_disable_verify.py
index 44772e65b..4a74d5885 100644
--- a/tests/test_disable_verify.py
+++ b/tests/test_disable_verify.py
@@ -59,6 +59,21 @@ def answer(command):
names += [name for name in state.get("other_user_enabled", []) if name not in names]
return "".join(f"package:{name}\n" for name in names)
return home_guard_answer(command)
+
+
+# A batch the TV cuts off (#2663): the first state["batch_dies_after"] lines reach the TV, then the
+# connection resets and tv.py fails, the way adb_shell's ConnectionResetError ended the SHIELD's run.
+home_guard_main = main
+
+
+def main():
+ if "--batch" in sys.argv[1:] and "batch_dies_after" in state:
+ lines = [line.strip() for line in sys.stdin if line.strip() and not line.startswith("#")]
+ state["commands"].extend(lines[:state["batch_dies_after"]])
+ save()
+ print("ConnectionResetError: [Errno 54] Connection reset by peer", file=sys.stderr)
+ return 1
+ return home_guard_main()
'''
MAIN = '\nif __name__ == "__main__":'
@@ -207,6 +222,25 @@ class DisableVerifyTest(unittest.TestCase):
self.assertEqual(sent, ["pm list packages -d"])
self.assertNotIn(COULD_NOT, self.said(entry, result))
+ def test_a_batch_the_tv_cut_off_still_reads_back_and_fails_the_run(self):
+ # #2663: the connection reset after the first package. The run still says what is left on
+ # and prints the ro.adb.secure note, then ends with a failed status.
+ for entry in self.entries():
+ with self.subTest(entry=entry):
+ self.tv(batch_dies_after=1)
+ result, sent = self.disable(entry)
+ self.assertNotEqual(result.returncode, 0, self.output(result))
+ self.assertEqual(sent, [f"pm disable-user --user 0 {BEFORE}"])
+ said = self.said(entry, result)
+ self.assertIn("stopped before the end of the list", said)
+ report = self.report(entry, result)
+ self.assertIn(AFTER, report)
+ self.assertNotIn(BEFORE, report, "the package the TV did take is not named")
+ self.assertIn("The batch stopped early", report)
+ self.assertNotIn("Every way of disabling a package is refused", report,
+ "a dropped connection is not a firmware that refuses pm")
+ self.assertIn("ro.adb.secure", said)
+
def test_disable_ps1_reads_and_says_what_disable_sh_does(self):
# Without pwsh the cases above run disable.sh only, so pin the PowerShell twin's text here.
sh = (ROOT / "tools" / "disable.sh").read_text(encoding="utf-8")
@@ -229,8 +263,15 @@ class DisableVerifyTest(unittest.TestCase):
self.assertGreater(text.index(fail), text.index(adb),
"the ADB warning is printed before a failed run ends")
for words in (HEADER, COULD_NOT, "Do not run the list again in a loop",
- "Every way of disabling a package is refused"):
+ "Every way of disabling a package is refused", "The batch stopped early",
+ "stopped before the end of the list"):
self.assertIn(words, text)
+ # A batch that died fails the run after the read-back and the ADB warning, not before (#2663).
+ for text, batch_fail, adb in (
+ (sh, '\n[ "$BATCH_RC" -eq 0 ] || exit "$BATCH_RC"\n', lines["disable.sh"][2]),
+ (ps1, "\nif ($batchExit -ne 0) { throw ", lines["disable.ps1"][2])):
+ self.assertGreater(text.index(batch_fail), text.index(adb))
+ self.assertGreater(text.index(batch_fail), text.index("'pm list packages -e --user 0'"))
if __name__ == "__main__":
diff --git a/tools/disable.ps1 b/tools/disable.ps1
index 2d1d0c67b..c47aa87e3 100644
--- a/tools/disable.ps1
+++ b/tools/disable.ps1
@@ -281,6 +281,11 @@ if ($LASTEXITCODE -ne 0) { throw 'no recovery record was written, so nothing was
$cmds = @($pkgs | ForEach-Object { "pm disable-user --user 0 $_" }) + 'pm list packages -d'
$cmds | & $py -I (Join-Path $repo 'tools\tv.py') --batch
$batchExit = $LASTEXITCODE
+# A batch that dies halfway (the TV reset the connection, #2663) still gets the read-back and the
+# ro.adb.secure note below; the throw at the end gives the run its failed status.
+if ($batchExit -ne 0) {
+ Write-Warning "tv.py --batch stopped before the end of the list (exit code $batchExit): the TV dropped the connection or stopped answering. The command it was on may or may not have reached the TV, and the ones after it were not sent. What is still enabled is read back below."
+}
# Same as disable.sh (#1376): read the TV back instead of trusting the batch's silence. `-e` lists enabled
# packages only, so one that is not on this TV, or that the factory had already disabled, is never named.
@@ -304,7 +309,9 @@ if ($pkgs.Count -gt 0) {
Write-Warning "Could not check which listed packages are still enabled: tv.py failed, or the TV listed no package for 'pm list packages -e --user 0'. Compare 'pm list packages -d --user 0' with the list by hand."
} else {
$stillOn = @($pkgs | Where-Object { $enabled -contains $_ } | Select-Object -Unique)
- if ($stillOn.Count -gt 0) {
+ if ($stillOn.Count -gt 0 -and $batchExit -ne 0) {
+ Write-Warning "Still enabled after this run, although the list disables them: $($stillOn -join ', '). The batch stopped early (above), so these were not sent, or their answer was lost. Once the TV answers again (tools\tv.py --probe), run the list once more. Do not run it again in a loop."
+ } elseif ($stillOn.Count -gt 0) {
Write-Warning "Still enabled after this run, although the list disables them: $($stillOn -join ', '). The TV did not take pm disable-user for these: it refused (its answer is in the output above), or the change did not hold. Do not run the list again in a loop. Run tools\tv.py 'pm disable-user --user 0 <package>' once by hand for each, or the list once more after a reboot. If every listed package stays enabled, this firmware refuses pm disable-user: stop there and say so (docs\TROUBLESHOOTING.md, `"Every way of disabling a package is refused`")."
}
}
@@ -314,7 +321,7 @@ if ($null -ne $adbSecure -and ($adbSecure -eq '0' -or $adbSecure -eq '')) {
$why = if ($adbSecure -eq '') { 'This TV does not set ro.adb.secure. On a debug (userdebug) build or Android 4.4 that is the same as ro.adb.secure=0: network ADB on port 5555 accepts any device on your network without the "Allow" prompt.' } else { 'This TV reports ro.adb.secure=0, so network ADB on port 5555 accepts any device on your network without the "Allow" prompt.' }
Write-Warning "$why When you are done, turn USB debugging, Network/ADB debugging and Wireless debugging off on the TV and reboot it (README: `"Isn't ADB access a security hole?`")."
}
-if ($batchExit -ne 0) { throw "tv.py --batch failed (exit code $batchExit): the list may be only partly applied; run it again once the TV answers" }
+if ($batchExit -ne 0) { throw "tv.py --batch failed (exit code $batchExit): the list may be only partly applied; run it once more when the TV answers" }
# Last, so the warnings above are printed either way: a listed package the TV left enabled fails the
# run, as in disable.sh.
if ($stillOn.Count -gt 0) { exit 1 }
diff --git a/tools/disable.sh b/tools/disable.sh
index 77409f377..1315e823a 100755
--- a/tools/disable.sh
+++ b/tools/disable.sh
@@ -375,10 +375,19 @@ fi
# and a retry must not overwrite the one taken before the first change (#239).
"$PY" -I tools/recovery.py --save "$LIST"
+# A batch that dies halfway (the TV reset the connection between two commands, #2663) still gets the
+# read-back and the ro.adb.secure note below, so the run ends by saying where it stopped; its status
+# is the batch's.
+BATCH_RC=0
{
[ -n "$PKGS" ] && printf '%s\n' "$PKGS" | sed 's/^/pm disable-user --user 0 /'
echo "pm list packages -d"
-} | "$PY" -I tools/tv.py --batch
+} | "$PY" -I tools/tv.py --batch || BATCH_RC=$?
+if [ "$BATCH_RC" -ne 0 ]; then
+ echo "tv.py --batch stopped before the end of the list (exit code $BATCH_RC): the TV dropped the" \
+ "connection or stopped answering. The command it was on may or may not have reached the TV, and" \
+ "the ones after it were not sent. What is still enabled is read back below." >&2
+fi
# Read the TV back instead of trusting the batch's silence (#1376). A `pm disable-user` the firmware
# refuses is a stack trace scrolling past in the output above, one that does not take says nothing at
@@ -405,12 +414,18 @@ if [ -n "$PKGS" ]; then
if [ -n "$STILL_ON" ]; then
{ echo "Still enabled after this run, although the list disables them:"
printf '%s\n' "$STILL_ON" | sed 's/^/ /'
- echo "The TV did not take pm disable-user for these: it refused (its answer is in the output"
- echo "above), or the change did not hold. Do not run the list again in a loop. Run"
- echo "tools/tv.py 'pm disable-user --user 0 <package>' once by hand for each, or the list once"
- echo "more after a reboot. If every listed package stays enabled, this firmware refuses"
- echo "pm disable-user: stop there and say so (docs/TROUBLESHOOTING.md,"
- echo "\"Every way of disabling a package is refused\")."; } >&2
+ if [ "$BATCH_RC" -ne 0 ]; then
+ echo "The batch stopped early (above), so these were not sent, or their answer was lost. Once"
+ echo "the TV answers again (tools/tv.py --probe), run the list once more. Do not run it again"
+ echo "in a loop."
+ else
+ echo "The TV did not take pm disable-user for these: it refused (its answer is in the output"
+ echo "above), or the change did not hold. Do not run the list again in a loop. Run"
+ echo "tools/tv.py 'pm disable-user --user 0 <package>' once by hand for each, or the list once"
+ echo "more after a reboot. If every listed package stays enabled, this firmware refuses"
+ echo "pm disable-user: stop there and say so (docs/TROUBLESHOOTING.md,"
+ echo "\"Every way of disabling a package is refused\")."
+ fi; } >&2
fi
fi
fi
@@ -428,6 +443,8 @@ if [ "$ADB_SECURE" = "0" ] || [ -z "$ADB_SECURE" ]; then
echo "TV and reboot it (README: \"Isn't ADB access a security hole?\")." >&2
fi
-# Last, so the warning above is printed either way: a listed package the TV left enabled fails the
-# run, as it does in check.py --fix, and a script or agent that goes on after this one stops here.
+# Last, so the warning above is printed either way: a batch that died, or a listed package the TV
+# left enabled, fails the run, as it does in check.py --fix, and a script or agent that goes on after
+# this one stops here.
+[ "$BATCH_RC" -eq 0 ] || exit "$BATCH_RC"
[ -z "$STILL_ON" ] || exit 1