Merge "Dup fds to stop finalizers from invalidating them."

am: d7a02820ac

Change-Id: Ia0aedcf2cd8d56f8a8c31b3af95ae8a2ae0dedd0
This commit is contained in:
Lorenzo Colitti
2017-03-16 03:33:04 +00:00
committed by android-build-merger
2 changed files with 21 additions and 4 deletions

View File

@@ -22,11 +22,15 @@ import android.content.Intent;
import android.content.ServiceConnection;
import android.os.ConditionVariable;
import android.os.IBinder;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
import android.system.ErrnoException;
import android.system.Os;
import com.android.cts.net.hostside.IRemoteSocketFactory;
import java.io.FileDescriptor;
import java.io.IOException;
public class RemoteSocketFactoryClient {
private static final int TIMEOUT_MS = 5000;
@@ -76,9 +80,14 @@ public class RemoteSocketFactoryClient {
}
}
public FileDescriptor openSocketFd(
String host, int port, int timeoutMs) throws RemoteException {
return mService.openSocketFd(host, port, timeoutMs).getFileDescriptor();
public FileDescriptor openSocketFd(String host, int port, int timeoutMs)
throws RemoteException, ErrnoException, IOException {
// Dup the filedescriptor so ParcelFileDescriptor's finalizer doesn't garbage collect it
// and cause our fd to become invalid. http://b/35927643 .
ParcelFileDescriptor pfd = mService.openSocketFd(host, port, timeoutMs);
FileDescriptor fd = Os.dup(pfd.getFileDescriptor());
pfd.close();
return fd;
}
public String getPackageName() throws RemoteException {

View File

@@ -481,7 +481,11 @@ public class VpnTest extends InstrumentationTestCase {
private FileDescriptor openSocketFd(String host, int port, int timeoutMs) throws Exception {
Socket s = new Socket(host, port);
s.setSoTimeout(timeoutMs);
return ParcelFileDescriptor.fromSocket(s).getFileDescriptor();
// Dup the filedescriptor so ParcelFileDescriptor's finalizer doesn't garbage collect it
// and cause our fd to become invalid. http://b/35927643 .
FileDescriptor fd = Os.dup(ParcelFileDescriptor.fromSocket(s).getFileDescriptor());
s.close();
return fd;
}
private FileDescriptor openSocketFdInOtherApp(
@@ -511,7 +515,9 @@ public class VpnTest extends InstrumentationTestCase {
private void assertSocketStillOpen(FileDescriptor fd, String host) throws Exception {
try {
assertTrue(fd.valid());
sendRequest(fd, host);
assertTrue(fd.valid());
} finally {
Os.close(fd);
}
@@ -519,10 +525,12 @@ public class VpnTest extends InstrumentationTestCase {
private void assertSocketClosed(FileDescriptor fd, String host) throws Exception {
try {
assertTrue(fd.valid());
sendRequest(fd, host);
fail("Socket opened before VPN connects should be closed when VPN connects");
} catch (ErrnoException expected) {
assertEquals(ECONNABORTED, expected.errno);
assertTrue(fd.valid());
} finally {
Os.close(fd);
}