From e8e19f6476b30ccf9f69925c10ce62efdba4e62a Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 21 Oct 2011 11:36:16 -0700 Subject: Cope with fstat(2) failures during close. Whatever the root cause of the fstat(2) failure, we should still attempt the requested close(2) --- even if it's just going to fail in the same way the fstat(2) did. Bug: 5407056 Change-Id: Ie988d1b1ebdb6b378fb537b2f56245fab568ee7c --- luni/src/main/java/libcore/io/BlockGuardOs.java | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) (limited to 'luni/src') diff --git a/luni/src/main/java/libcore/io/BlockGuardOs.java b/luni/src/main/java/libcore/io/BlockGuardOs.java index a0062e4..07ee520 100644 --- a/luni/src/main/java/libcore/io/BlockGuardOs.java +++ b/luni/src/main/java/libcore/io/BlockGuardOs.java @@ -48,26 +48,25 @@ public class BlockGuardOs extends ForwardingOs { } @Override public void close(FileDescriptor fd) throws ErrnoException { - if (S_ISSOCK(Libcore.os.fstat(fd).st_mode) && isLingerSocket(fd)) { - // If the fd is a socket with SO_LINGER set, we might block indefinitely. - // We allow non-linger sockets so that apps can close their network connections in - // methods like onDestroy which will run on the UI thread. - BlockGuard.getThreadPolicy().onNetwork(); - } - os.close(fd); - } - - private static boolean isLingerSocket(FileDescriptor fd) { try { - StructLinger linger = Libcore.os.getsockoptLinger(fd, SOL_SOCKET, SO_LINGER); - return linger.isOn() && linger.l_linger > 0; + if (S_ISSOCK(Libcore.os.fstat(fd).st_mode) && isLingerSocket(fd)) { + // If the fd is a socket with SO_LINGER set, we might block indefinitely. + // We allow non-linger sockets so that apps can close their network connections in + // methods like onDestroy which will run on the UI thread. + BlockGuard.getThreadPolicy().onNetwork(); + } } catch (ErrnoException ignored) { // We're called via Socket.close (which doesn't ask for us to be called), so we // must not throw here, because Socket.close must not throw if asked to close an // already-closed socket. Also, the passed-in FileDescriptor isn't necessarily // a socket at all. - return false; } + os.close(fd); + } + + private static boolean isLingerSocket(FileDescriptor fd) throws ErrnoException { + StructLinger linger = Libcore.os.getsockoptLinger(fd, SOL_SOCKET, SO_LINGER); + return linger.isOn() && linger.l_linger > 0; } @Override public void connect(FileDescriptor fd, InetAddress address, int port) throws ErrnoException { -- cgit v1.1