libprocstat kstack: fix race with thread creation

When collecting kernel stacks for a target process, if the process
adds a thread between the two calls to sysctl, ignore the additional
threads.  Previously, procstat would print only a useless error
message.  Now, it prints a consistent snapshot of the stacks.
We know that snapshot is already stale, but it could still be stale
even with a more complex fix to reallocate and retry, so such a fix
is hardly worth the effort.

Reported by:	Daniel.Mitchell@emc.com
MFC after:	1 week
Sponsored by:	Dell EMC Isilon
This commit is contained in:
Eric van Gyzen 2021-05-27 11:33:22 -05:00
parent 8c69d988a8
commit 427f12f150

View File

@ -2609,7 +2609,8 @@ procstat_getkstack_sysctl(pid_t pid, int *cntp)
warn("malloc(%zu)", len);
return (NULL);
}
if (sysctl(name, nitems(name), kkstp, &len, NULL, 0) == -1) {
if (sysctl(name, nitems(name), kkstp, &len, NULL, 0) == -1 &&
errno != ENOMEM) {
warn("sysctl: kern.proc.pid: %d", pid);
free(kkstp);
return (NULL);