From 0209c366627e98d6311629a0592c6e22be7d13e0 Mon Sep 17 00:00:00 2001 From: Chiachang Wang Date: Mon, 29 Nov 2021 17:42:08 +0800 Subject: [PATCH] Ensure created fd being closed If exception s thrown while calling bindSocket(fd), the temporary created ParcelFileDescriptor may not be closed as expected. Ensure it's closed eventually. Bug: 206042872 Test: atest FrameworksNetTests Change-Id: Icde895978ab9281006ffd56335d1247462d9da28 --- framework/src/android/net/Network.java | 29 +++++++++++++------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/framework/src/android/net/Network.java b/framework/src/android/net/Network.java index b3770eabdf..53f171a017 100644 --- a/framework/src/android/net/Network.java +++ b/framework/src/android/net/Network.java @@ -382,13 +382,14 @@ public class Network implements Parcelable { // Query a property of the underlying socket to ensure that the socket's file descriptor // exists, is available to bind to a network and is not closed. socket.getReuseAddress(); - final ParcelFileDescriptor pfd = ParcelFileDescriptor.fromDatagramSocket(socket); - bindSocket(pfd.getFileDescriptor()); - // ParcelFileDescriptor.fromSocket() creates a dup of the original fd. The original and the - // dup share the underlying socket in the kernel. The socket is never truly closed until the - // last fd pointing to the socket being closed. So close the dup one after binding the - // socket to control the lifetime of the dup fd. - pfd.close(); + + // ParcelFileDescriptor.fromDatagramSocket() creates a dup of the original fd. The original + // and the dup share the underlying socket in the kernel. The socket is never truly closed + // until the last fd pointing to the socket being closed. Try and eventually close the dup + // one after binding the socket to control the lifetime of the dup fd. + try (ParcelFileDescriptor pfd = ParcelFileDescriptor.fromDatagramSocket(socket)) { + bindSocket(pfd.getFileDescriptor()); + } } /** @@ -400,13 +401,13 @@ public class Network implements Parcelable { // Query a property of the underlying socket to ensure that the socket's file descriptor // exists, is available to bind to a network and is not closed. socket.getReuseAddress(); - final ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(socket); - bindSocket(pfd.getFileDescriptor()); - // ParcelFileDescriptor.fromSocket() creates a dup of the original fd. The original and the - // dup share the underlying socket in the kernel. The socket is never truly closed until the - // last fd pointing to the socket being closed. So close the dup one after binding the - // socket to control the lifetime of the dup fd. - pfd.close(); + // ParcelFileDescriptor.fromSocket() creates a dup of the original fd. The original and + // the dup share the underlying socket in the kernel. The socket is never truly closed + // until the last fd pointing to the socket being closed. Try and eventually close the dup + // one after binding the socket to control the lifetime of the dup fd. + try (ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(socket)) { + bindSocket(pfd.getFileDescriptor()); + } } /**