Fixed subtle bugs related to file descriptor recycling.
Added code to protect against situations that may occur when a
Looper callback has the side-effect of closing the file descriptor that
it is watching before it returns. This code pattern is very
convenient for implementation but it does expose issues in how
the list of callbacks is maintained. In particular, we
need to watch out for file descriptors which have been reused.
This change may resolve previously unexplained ANRs associated with
log messages such as: "Error modifying epoll events for fd 44, errno=2"
Bug: 10349083
Change-Id: I20eedf6ffbdeda382653ca0104962505194741b0
diff --git a/include/utils/Looper.h b/include/utils/Looper.h
index dae7833..5722c8e 100644
--- a/include/utils/Looper.h
+++ b/include/utils/Looper.h
@@ -420,6 +420,7 @@
struct Request {
int fd;
int ident;
+ int seq;
sp<LooperCallback> callback;
void* data;
};
@@ -458,6 +459,7 @@
// Locked list of file descriptor monitoring requests.
KeyedVector<int, Request> mRequests; // guarded by mLock
+ int mNextRequestSeq;
// This state is only used privately by pollOnce and does not require a lock since
// it runs on a single thread.
@@ -466,6 +468,7 @@
nsecs_t mNextMessageUptime; // set to LLONG_MAX when none
int pollInner(int timeoutMillis);
+ int removeFd(int fd, int seq);
void awoken();
void pushResponse(int events, const Request& request);