[New-bugs-announce] [issue38762] Logging displays wrong "processName" if "sys.modules" is cleared in child process

Delgan report at bugs.python.org
Sun Nov 10 16:53:15 EST 2019


New submission from Delgan <delgan.py at gmail.com>:

Hi.

In order to display the process name in logs, the "logging" module checks for the presence of "multiprocessing" in "sys.modules" before calling "current_process()". If "multiprocessing" is not found in "sys.modules", it assumes that the current process is the main one.

See : https://github.com/python/cpython/blob/af46450bb97ab9bd38748e75aa849c29fdd70028/Lib/logging/__init__.py#L340-L341

However, nothing prevents a child process to delete "sys.module['multiprocessing']", which causes the process name to be wrongly displayed as "MainProcess".

I attached a reproducible example, but this is straightforward to understand. Although it should not happen very often in practice, it is still theoretically possible. Obviously, one could say "just don't clear sys.modules", but I suppose there might exist tools doing such thing for good reasons (like resetting the test environment).

Issues which lead to the current implementation:
- issue4301
- issue7120
- issue8200

Possible fixes: 
- Force import "multiprocessing.current_process()" even if not already loaded
- Add function "os.main_pid()" and set "processName" to "MainProcess" only if "os.getpid() == os.main_pid()"

----------
components: Library (Lib)
files: test.py
messages: 356338
nosy: Delgan
priority: normal
severity: normal
status: open
title: Logging displays wrong "processName" if "sys.modules" is cleared in child process
type: behavior
versions: Python 3.9
Added file: https://bugs.python.org/file48705/test.py

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38762>
_______________________________________


More information about the New-bugs-announce mailing list