Skip to content

Commit

Permalink
Skip parsing of zombie processes.
Browse files Browse the repository at this point in the history
  • Loading branch information
ovalenti committed Dec 9, 2024
1 parent d1fb856 commit 400f5c1
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion collector/lib/ProcfsScraper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,26 @@ bool GetSocketINodes(int dirfd, uint64_t pid, UnorderedSet<SocketInfo>* sock_ino
return true;
}

// IsZombieProcess fetches the current state of the process pointed to by dirfd, and
// - returns true, if the process state is 'Z'
// - returns false otherwise
bool IsZombieProcess(int dirfd) {
FileHandle stat_file(FDHandle(openat(dirfd, "stat", O_RDONLY)), "r");
if (!stat_file.valid()) {
return false;
}

char linebuf[512];

if (fgets(linebuf, sizeof(linebuf), stat_file.get()) == nullptr) {
return false;
}

auto state = ExtractProcessState(linebuf);

return state && *state == 'Z';
}

// GetContainerID retrieves the container ID of the process represented by dirfd. The container ID is extracted from
// the cgroup.
std::optional<std::string> GetContainerID(int dirfd) {
Expand Down Expand Up @@ -492,14 +512,17 @@ bool ReadContainerConnections(const char* proc_path, std::shared_ptr<ProcessStor
continue;
}

if (IsZombieProcess(dirfd)) {
continue;
}

auto container_id = GetContainerID(dirfd);
if (!container_id) {
continue;
}

uint64_t netns_inode;
if (!GetNetworkNamespace(dirfd, &netns_inode)) {
// TODO ROX-13962: Improve logging to indicate when a process is defunct.
COUNTER_INC(CollectorStats::procfs_could_not_get_network_namespace);
CLOG_THROTTLED(ERROR, std::chrono::seconds(10)) << "Could not determine network namespace: " << StrError();
continue;
Expand Down

0 comments on commit 400f5c1

Please sign in to comment.