better error handling in MemoryIdler for inside jails
authorNathan Bronson <ngbronson@fb.com>
Tue, 5 Dec 2017 00:39:00 +0000 (16:39 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Tue, 5 Dec 2017 00:39:49 +0000 (16:39 -0800)
Summary:
/proc may be unavailable in restricted environments, which can
cause pthread_getattr_np to fail.  This diff adds checking to the return
code from that function.

Reviewed By: yfeldblum

Differential Revision: D6473789

fbshipit-source-id: de016d32f29edf8410808dfb491987692f39e768

folly/detail/MemoryIdler.cpp

index 8cf1fb3..8e1467d 100644 (file)
@@ -25,6 +25,7 @@
 #include <folly/portability/PThread.h>
 #include <folly/portability/SysMman.h>
 #include <folly/portability/Unistd.h>
+#include <folly/synchronization/CallOnce.h>
 
 #include <limits.h>
 #include <stdio.h>
@@ -92,13 +93,22 @@ static size_t pageSize() {
 }
 
 static void fetchStackLimits() {
+  int err;
   pthread_attr_t attr;
-  pthread_getattr_np(pthread_self(), &attr);
+  if ((err = pthread_getattr_np(pthread_self(), &attr))) {
+    // some restricted environments can't access /proc
+    static folly::once_flag flag;
+    folly::call_once(flag, [err]() {
+      LOG(WARNING) << "pthread_getaddr_np failed errno=" << err;
+    });
+
+    tls_stackSize = 1;
+    return;
+  }
   SCOPE_EXIT { pthread_attr_destroy(&attr); };
 
   void* addr;
   size_t rawSize;
-  int err;
   if ((err = pthread_attr_getstack(&attr, &addr, &rawSize))) {
     // unexpected, but it is better to continue in prod than do nothing
     FB_LOG_EVERY_MS(ERROR, 10000) << "pthread_attr_getstack error " << err;