From df26cda358cdbbfc1214d9374c9641769151f1eb Mon Sep 17 00:00:00 2001 From: Patrick Rohr Date: Tue, 25 Feb 2025 09:14:11 -0800 Subject: [PATCH] Get rid of volatile mIsAborted This has always been race-y as a socket exception could coincide with a call to #close(). Since it is safe to run the full code paths for both "regular" exceptions and exceptions triggered by the socket closing, mIsAborted is not actually needed. This CL gets rid of it in favor of simplicity. Test: TH Change-Id: Ic597ee917daffd6315c349dc435e5533b686bc2d --- service/src/com/android/server/L2capNetworkProvider.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/service/src/com/android/server/L2capNetworkProvider.java b/service/src/com/android/server/L2capNetworkProvider.java index f6ca26b1d9..9d070cd598 100644 --- a/service/src/com/android/server/L2capNetworkProvider.java +++ b/service/src/com/android/server/L2capNetworkProvider.java @@ -434,7 +434,6 @@ public class L2capNetworkProvider { private class ConnectThread extends Thread { private final L2capNetworkSpecifier mSpecifier; private final BluetoothSocket mSocket; - private volatile boolean mIsAborted = false; public ConnectThread(L2capNetworkSpecifier specifier, BluetoothSocket socket) { super("L2capNetworkProvider-ConnectThread"); @@ -451,11 +450,12 @@ public class L2capNetworkProvider { if (!success) closeBluetoothSocket(mSocket); }); } catch (IOException e) { - Log.e(TAG, "Failed to connect", e); - if (mIsAborted) return; - + Log.w(TAG, "BluetoothSocket was closed or #connect failed", e); + // It is safe to call BluetoothSocket#close() multiple times. closeBluetoothSocket(mSocket); mHandler.post(() -> { + // Note that if the Socket was closed, this call is a noop as the + // ClientNetworkRequest has already been removed. declareAllNetworkRequestsUnfulfillable(mSpecifier); }); } @@ -463,7 +463,6 @@ public class L2capNetworkProvider { public void abort() { HandlerUtils.ensureRunningOnHandlerThread(mHandler); - mIsAborted = true; // Closing the BluetoothSocket is the only way to unblock connect() because it calls // shutdown on the underlying (connected) SOCK_SEQPACKET. // It is safe to call BluetoothSocket#close() multiple times.