Merge "Ensure created fd being closed"

This commit is contained in:
Treehugger Robot
2021-12-01 10:49:26 +00:00
committed by Gerrit Code Review

View File

@@ -382,13 +382,14 @@ public class Network implements Parcelable {
// Query a property of the underlying socket to ensure that the socket's file descriptor // 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. // exists, is available to bind to a network and is not closed.
socket.getReuseAddress(); socket.getReuseAddress();
final ParcelFileDescriptor pfd = ParcelFileDescriptor.fromDatagramSocket(socket);
bindSocket(pfd.getFileDescriptor()); // ParcelFileDescriptor.fromDatagramSocket() creates a dup of the original fd. The original
// ParcelFileDescriptor.fromSocket() creates a dup of the original fd. The original and the // and the dup share the underlying socket in the kernel. The socket is never truly closed
// dup share the underlying socket in the kernel. The socket is never truly closed until the // until the last fd pointing to the socket being closed. Try and eventually close the dup
// last fd pointing to the socket being closed. So close the dup one after binding the // one after binding the socket to control the lifetime of the dup fd.
// socket to control the lifetime of the dup fd. try (ParcelFileDescriptor pfd = ParcelFileDescriptor.fromDatagramSocket(socket)) {
pfd.close(); 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 // 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. // exists, is available to bind to a network and is not closed.
socket.getReuseAddress(); socket.getReuseAddress();
final ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(socket); // ParcelFileDescriptor.fromSocket() creates a dup of the original fd. The original and
bindSocket(pfd.getFileDescriptor()); // the dup share the underlying socket in the kernel. The socket is never truly closed
// ParcelFileDescriptor.fromSocket() creates a dup of the original fd. The original and the // until the last fd pointing to the socket being closed. Try and eventually close the dup
// dup share the underlying socket in the kernel. The socket is never truly closed until the // one after binding the socket to control the lifetime of the dup fd.
// last fd pointing to the socket being closed. So close the dup one after binding the try (ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(socket)) {
// socket to control the lifetime of the dup fd. bindSocket(pfd.getFileDescriptor());
pfd.close(); }
} }
/** /**