Skip to content
Snippets Groups Projects
Commit 5b98fb05 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Ensure created fd being closed"

parents 132ff9ef 0209c366
No related branches found
No related tags found
No related merge requests found
......@@ -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());
}
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment