Skip to content

Commit

Permalink
libprocstat kstack: fix race with thread creation
Browse files Browse the repository at this point in the history
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:	[email protected]
MFC after:	1 week
Sponsored by:	Dell EMC Isilon

(cherry picked from commit 427f12f)
  • Loading branch information
vangyzen committed Mar 2, 2022
1 parent 94df301 commit d895236
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/libprocstat/libprocstat.c
Original file line number Diff line number Diff line change
Expand Up @@ -2610,7 +2610,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);
Expand Down

0 comments on commit d895236

Please sign in to comment.