@@ -1042,8 +1042,6 @@ ThreadLister::Result ThreadLister::ListThreads(
10421042
10431043 Result result = Ok;
10441044 for (bool first_read = true ;; first_read = false ) {
1045- // Resize to max capacity if it was downsized by IsAlive.
1046- buffer_.resize (buffer_.capacity ());
10471045 CHECK_GE (buffer_.size (), 4096 );
10481046 uptr read = internal_getdents (
10491047 descriptor, (struct linux_dirent *)buffer_.data (), buffer_.size ());
@@ -1088,14 +1086,25 @@ ThreadLister::Result ThreadLister::ListThreads(
10881086 }
10891087}
10901088
1089+ const char *ThreadLister::LoadStatus (int tid) {
1090+ auto cleanup = at_scope_exit ([&] {
1091+ // Resize back to capacity if it is downsized by `ReadFileToVector`.
1092+ buffer_.resize (buffer_.capacity ());
1093+ });
1094+ if (!ReadFileToVector (status_path_.data (), &buffer_) || buffer_.empty ())
1095+ return nullptr ;
1096+ buffer_.push_back (' \0 ' );
1097+ return buffer_.data ();
1098+ }
1099+
10911100bool ThreadLister::IsAlive (int tid) {
10921101 // /proc/%d/task/%d/status uses same call to detect alive threads as
10931102 // proc_task_readdir. See task_state implementation in Linux.
1094- if (!ReadFileToVector (status_path_.data (), &buffer_) || buffer_.empty ())
1095- return false ;
1096- buffer_.push_back (0 );
10971103 static const char kPrefix [] = " \n PPid:" ;
1098- const char *field = internal_strstr (buffer_.data (), kPrefix );
1104+ const char *status = LoadStatus (tid);
1105+ if (!status)
1106+ return false ;
1107+ const char *field = internal_strstr (status, kPrefix );
10991108 if (!field)
11001109 return false ;
11011110 field += internal_strlen (kPrefix );
0 commit comments