Skip to content

Commit

Permalink
fix #586 / set cpu_affinity() segfault: pre-emptively check if provid…
Browse files Browse the repository at this point in the history
…ed CPUs are valid before calling the C function
  • Loading branch information
giampaolo committed Jan 3, 2015
1 parent 5c7e53c commit f70137e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
3 changes: 2 additions & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ Bug tracker at https://github.com/giampaolo/psutil/issues
"localhost"
- #579: [Windows] fixed many compiler warnings.
- #585: [FreeBSD] net_connections() may raise KeyError.

- #586: [FreeBSD] cpu_affinity() segfaults on set in case an invalid CPU
number is provided.

2.2.1 - 2015-02-02
==================
Expand Down
9 changes: 8 additions & 1 deletion psutil/_psbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,14 @@ def cpu_affinity_get(self):

@wrap_exceptions
def cpu_affinity_set(self, cpus):
# Pre-emptively check if CPUs are valid because the C
# function has a weird behavior in case of invalid CPUs
#, see: https://github.com/giampaolo/psutil/issues/586
allcpus = tuple(range(len(per_cpu_times())))
for cpu in cpus:
if cpu not in allcpus:
raise ValueError("invalid CPU #%i (choose between %s)"
% (cpu, allcpus))
try:
cext.proc_cpu_affinity_set(self.pid, cpus)
except OSError as err:
Expand All @@ -414,7 +422,6 @@ def cpu_affinity_set(self, cpus):
# on because the set does not overlap with the thread's
# anonymous mask>>
if err.errno in (errno.EINVAL, errno.EDEADLK):
allcpus = tuple(range(len(per_cpu_times())))
for cpu in cpus:
if cpu not in allcpus:
raise ValueError("invalid CPU #%i (choose between %s)"
Expand Down

0 comments on commit f70137e

Please sign in to comment.