From 93f1ed2a0aa1e332a29feb1ea90a788097f85f60 Mon Sep 17 00:00:00 2001 From: Joël Stemmer Date: Tue, 14 May 2024 11:55:36 +0100 Subject: Catch and ignore `IllegalArgumentException` thrown by `unbindService` When `TransportConnection` handles `onServiceConnected` or `onBindingDied` it calls `Context.unbindService`. In a very small number of cases this fails because the connection is not (or no longer?) registered. When this happens, the system server crashes causing the device to reboot. We don't really care if the service is not registered when we try to unbind it, so we just catch and ignore the exception to avoid crashing the system server. Bug: 335547110 Test: atest TransportConnectionTest.java Change-Id: Ic7f34eacd1f621c42a68128ef992043dba6808f1 --- .../server/backup/transport/TransportConnection.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'services/backup/java') diff --git a/services/backup/java/com/android/server/backup/transport/TransportConnection.java b/services/backup/java/com/android/server/backup/transport/TransportConnection.java index 1009787bebe5..67ebb3e6a1ef 100644 --- a/services/backup/java/com/android/server/backup/transport/TransportConnection.java +++ b/services/backup/java/com/android/server/backup/transport/TransportConnection.java @@ -658,11 +658,13 @@ public class TransportConnection { * This class is a proxy to TransportClient methods that doesn't hold a strong reference to the * TransportClient, allowing it to be GC'ed. If the reference was lost it logs a message. */ - private static class TransportConnectionMonitor implements ServiceConnection { + @VisibleForTesting + static class TransportConnectionMonitor implements ServiceConnection { private final Context mContext; private final WeakReference mTransportClientRef; - private TransportConnectionMonitor(Context context, + @VisibleForTesting + TransportConnectionMonitor(Context context, TransportConnection transportConnection) { mContext = context; mTransportClientRef = new WeakReference<>(transportConnection); @@ -704,7 +706,13 @@ public class TransportConnection { /** @see TransportConnection#finalize() */ private void referenceLost(String caller) { - mContext.unbindService(this); + try { + mContext.unbindService(this); + } catch (IllegalArgumentException e) { + TransportUtils.log(Priority.WARN, TAG, + caller + " called but unbindService failed: " + e.getMessage()); + return; + } TransportUtils.log( Priority.INFO, TAG, -- cgit v1.2.3-59-g8ed1b