Skip to content

fix: catch NoSuchProcess/ZombieProcess in get_misp_modules_pid - #883

Open
elhoim wants to merge 1 commit into
MISP:mainfrom
elhoim:fix/074-psutil-race-exception
Open

fix: catch NoSuchProcess/ZombieProcess in get_misp_modules_pid#883
elhoim wants to merge 1 commit into
MISP:mainfrom
elhoim:fix/074-psutil-race-exception

Conversation

@elhoim

@elhoim elhoim commented Aug 31, 2026

Copy link
Copy Markdown
Member

get_misp_modules_pid() in misp_modules/__init__.py only widens its exception handling to psutil.AccessDenied:

for pid in psutil.pids():
    try:
        if any("misp-modules" in x for x in psutil.Process(pid).cmdline()):
            return pid
    except psutil.AccessDenied:
        return None

psutil.pids() returns a snapshot; a process can exit between that enumeration and the Process(pid).cmdline() call. When that happens, psutil raises psutil.NoSuchProcess (or psutil.ZombieProcess for a process that has already exited but not been reaped), neither of which is caught here. The exception propagates out of get_misp_modules_pid().

This function is called from the "address already in use" diagnostic in __main__.py when the server fails to bind its port, to report which PID is already holding it. If the race hits, the diagnostic itself crashes with an unrelated psutil.NoSuchProcess traceback instead of reporting the real bind error, which is confusing for anyone debugging why misp-modules won't start.

Fix

Widen the except clause to (psutil.AccessDenied, psutil.NoSuchProcess, psutil.ZombieProcess) so a process that exits mid-scan is treated the same as one we can't inspect: return None and let the real "address already in use" message surface.

This is a pure exception-handling fix with no behaviour change to the success path.

Verification

  • flake8 clean on misp_modules/__init__.py
  • Full module test suite: 161 passed, 4 skipped, 5 subtests passed in 20.79s

Found during a review of the repository; other findings are being submitted as separate PRs.

🤖 Generated with Claude Code

https://claude.ai/code/session_018dfYpyaSZd1nxSRLr8suj8

…_pid

get_misp_modules_pid() scans all live pids and inspects each process's cmdline
to find a running misp-modules instance, catching only psutil.AccessDenied.
Between psutil.pids() enumerating a pid and Process(pid).cmdline() being
called, that process can exit, which raises psutil.NoSuchProcess (or
ZombieProcess for a process stuck in the zombie state); neither is caught, so
the exception propagates out of the function. This is invoked from the
"address already in use" diagnostic in __main__.py, so a process exiting
mid-scan crashes that diagnostic and hides the real bind error the user
needed to see.

Verified with flake8 on the changed file (clean) and the full pytest suite
against a live server on port 6774: 161 passed, 4 skipped, 5 subtests passed,
matching the recorded baseline.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018dfYpyaSZd1nxSRLr8suj8
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

Successfully merging this pull request may close these issues.

1 participant