procfs: Fix removing vanished processes in pidstats.reload_threads()

If a process disappears while iterating the loop in
pidstats.reload_threads(), we get a RuntimeError as shown below. This
is because we cannot remove an entry from a dictionary while iterating the
dictionary.

Reproducer:
1. Add the following line to the beginning of pidstats.reload_threads():
import pdb; pdb.set_trace()
2. Start some process
3. Start the python interpreter and proceed as follows:
[~/git/python-linux-procfs]$ python3
Python 3.6.8 (default, Jan  3 2019, 16:11:14)
[GCC 8.2.1 20181215 (Red Hat 8.2.1-6)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import procfs
>>> ps = procfs.pidstats()
>>> ps.reload_threads()
> /home/olysonek/git/python-linux-procfs/procfs/procfs.py(462)reload_threads()
-> for pid in self.processes.keys():
(Pdb) next
> /home/olysonek/git/python-linux-procfs/procfs/procfs.py(463)reload_threads()
-> try:

At this point, terminate the process started in step 2. Return to the
python interpreter:

(Pdb) continue
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/olysonek/git/python-linux-procfs/procfs/procfs.py", line 463, in reload_threads
    try:
RuntimeError: dictionary changed size during iteration

Signed-off-by: Ondřej Lysoněk <olysonek@redhat.com>
Signed-off-by: John Kacur <jkacur@redhat.com>
Signed-off-by: Jiri Kastner <jkastner@redhat.com>
1 file changed
tree: a56622569ff4ffb356f0f8e718fd0474a0d74d7b
  1. COPYING
  2. MANIFEST
  3. Makefile
  4. bitmasklist_test.py
  5. pflags
  6. procfs/
  7. rpm/
  8. setup.py