diff --git a/services/core/java/com/android/server/vcn/VcnGatewayConnection.java b/services/core/java/com/android/server/vcn/VcnGatewayConnection.java index efe78377575e8..20c08eb2ce928 100644 --- a/services/core/java/com/android/server/vcn/VcnGatewayConnection.java +++ b/services/core/java/com/android/server/vcn/VcnGatewayConnection.java @@ -1474,7 +1474,13 @@ public class VcnGatewayConnection extends StateMachine { mVcnContext.getVcnNetworkProvider(), () -> { Slog.d(TAG, "NetworkAgent was unwanted"); - teardownAsynchronously(); + // If network agent has already been torn down, skip sending the + // disconnect. Unwanted() is always called, even when networkAgents + // are unregistered in teardownNetwork(), so prevent duplicate + // notifications. + if (mNetworkAgent != null) { + teardownAsynchronously(); + } } /* networkUnwantedCallback */, (status) -> { if (status == NetworkAgent.VALIDATION_STATUS_VALID) { diff --git a/tests/vcn/java/com/android/server/vcn/VcnGatewayConnectionConnectedStateTest.java b/tests/vcn/java/com/android/server/vcn/VcnGatewayConnectionConnectedStateTest.java index 54086c2a332f6..34c00182f855a 100644 --- a/tests/vcn/java/com/android/server/vcn/VcnGatewayConnectionConnectedStateTest.java +++ b/tests/vcn/java/com/android/server/vcn/VcnGatewayConnectionConnectedStateTest.java @@ -312,6 +312,9 @@ public class VcnGatewayConnectionConnectedStateTest extends VcnGatewayConnection verifySafeModeTimeoutNotifiesCallbackAndUnregistersNetworkAgent( mGatewayConnection.mConnectedState); + assertTrue(mGatewayConnection.isInSafeMode()); + assertFalse(mGatewayConnection.isQuitting()); + triggerChildOpened(); mTestLooper.dispatchAll();