Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graceful shutdown when stopping with Ctrl+C #65

Closed
theova opened this issue Nov 1, 2023 · 1 comment
Closed

Graceful shutdown when stopping with Ctrl+C #65

theova opened this issue Nov 1, 2023 · 1 comment

Comments

@theova
Copy link

theova commented Nov 1, 2023

When running a mininet without a cli (net.disableCli()) and hitting Ctrl-C, I get the following trace:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/psutil/_pslinux.py", line 1653, in wrapper
    return fun(self, *args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/psutil/_common.py", line 480, in wrapper
    raise raise_from(err, None)
  File "<string>", line 3, in raise_from
  File "/usr/local/lib/python3.8/dist-packages/psutil/_common.py", line 478, in wrapper
    return fun(self)
  File "/usr/local/lib/python3.8/dist-packages/psutil/_pslinux.py", line 1695, in _parse_stat_file
    data = bcat("%s/%s/stat" % (self._procfs_path, self.pid))
  File "/usr/local/lib/python3.8/dist-packages/psutil/_common.py", line 813, in bcat
    return cat(fname, fallback=fallback, _open=open_binary)
  File "/usr/local/lib/python3.8/dist-packages/psutil/_common.py", line 801, in cat
    with _open(fname) as f:
  File "/usr/local/lib/python3.8/dist-packages/psutil/_common.py", line 765, in open_binary
    return open(fname, "rb", buffering=FILE_READ_BUFFER_SIZE)
FileNotFoundError: [Errno 2] No such file or directory: '/proc/2442/stat'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/psutil/__init__.py", line 361, in _init
    self.create_time()
  File "/usr/local/lib/python3.8/dist-packages/psutil/__init__.py", line 719, in create_time
    self._create_time = self._proc.create_time()
  File "/usr/local/lib/python3.8/dist-packages/psutil/_pslinux.py", line 1653, in wrapper
    return fun(self, *args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/psutil/_pslinux.py", line 1863, in create_time
    ctime = float(self._parse_stat_file()['create_time'])
  File "/usr/local/lib/python3.8/dist-packages/psutil/_pslinux.py", line 1660, in wrapper
    raise NoSuchProcess(self.pid, self._name)
psutil.NoSuchProcess: process no longer exists (pid=2442)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "network.py", line 187, in <module>
    net.stopNetwork()
  File "/home/p4/p4-tools/p4-utils/p4utils/mininetlib/network_API.py", line 1160, in stopNetwork
    self.stop_exec_scripts()
  File "/home/p4/p4-tools/p4-utils/p4utils/mininetlib/network_API.py", line 1154, in stop_exec_scripts
    kill_proc_tree(pid)
  File "/home/p4/p4-tools/p4-utils/p4utils/utils/helper.py", line 481, in kill_proc_tree
    parent = psutil.Process(pid)
  File "/usr/local/lib/python3.8/dist-packages/psutil/__init__.py", line 332, in __init__
    self._init(pid)
  File "/usr/local/lib/python3.8/dist-packages/psutil/__init__.py", line 373, in _init
    raise NoSuchProcess(pid, msg='process PID not found')
psutil.NoSuchProcess: process PID not found (pid=2442)

The error comes from the following lines:

def kill_proc_tree(pid, sig=signal.SIGKILL, include_parent=True,
timeout=None, on_terminate=None):
"""Kills a process tree (including children).
Args:
pid (int) : PID of the parent process
sig (int) : signal used to kill the tree
include_parent (bool) : whether to kill the parent process or not
timeout (int or float) : time to wait for a process to terminate
on_terminate (types.FunctionType): callback function executed as soon as a child terminates.
Returns:
tuple: ``(gone, still_alive)``.
"""
assert pid != os.getpid(), "won't kill myself"
parent = psutil.Process(pid)
children = parent.children(recursive=True)
if include_parent:

An approach to fix this problem is to check whether the process actually exists:

    try:
        parent = psutil.Process(pid)
    except psutil.NoSuchProcess:
        return (None, None)

However, I'm not sure what should be returned in case that there is no process; returning (None, None) is most probably not the best way to do it.

@theova
Copy link
Author

theova commented Nov 1, 2023

The problem originated from

    subprocess.call(["pkill", "mx"])
    net.stopNetwork()
```.

Swapping the two lines fixes it.

@theova theova closed this as completed Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant