Skip to content

Commit f70137e

Browse files
committedJan 3, 2015
fix #586 / set cpu_affinity() segfault: pre-emptively check if provided CPUs are valid before calling the C function
1 parent 5c7e53c commit f70137e

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed
 

‎HISTORY.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ Bug tracker at https://github.com/giampaolo/psutil/issues
1515
"localhost"
1616
- #579: [Windows] fixed many compiler warnings.
1717
- #585: [FreeBSD] net_connections() may raise KeyError.
18-
18+
- #586: [FreeBSD] cpu_affinity() segfaults on set in case an invalid CPU
19+
number is provided.
1920

2021
2.2.1 - 2015-02-02
2122
==================

‎psutil/_psbsd.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,14 @@ def cpu_affinity_get(self):
406406

407407
@wrap_exceptions
408408
def cpu_affinity_set(self, cpus):
409+
# Pre-emptively check if CPUs are valid because the C
410+
# function has a weird behavior in case of invalid CPUs
411+
#, see: https://github.com/giampaolo/psutil/issues/586
412+
allcpus = tuple(range(len(per_cpu_times())))
413+
for cpu in cpus:
414+
if cpu not in allcpus:
415+
raise ValueError("invalid CPU #%i (choose between %s)"
416+
% (cpu, allcpus))
409417
try:
410418
cext.proc_cpu_affinity_set(self.pid, cpus)
411419
except OSError as err:
@@ -414,7 +422,6 @@ def cpu_affinity_set(self, cpus):
414422
# on because the set does not overlap with the thread's
415423
# anonymous mask>>
416424
if err.errno in (errno.EINVAL, errno.EDEADLK):
417-
allcpus = tuple(range(len(per_cpu_times())))
418425
for cpu in cpus:
419426
if cpu not in allcpus:
420427
raise ValueError("invalid CPU #%i (choose between %s)"

0 commit comments

Comments
 (0)
Please sign in to comment.