New submission from STINNER Victor <vstin...@python.org>:

os.environ[key] = value has to keep internally the string "key=value\0" after 
putenv("key=value\0") has been called, since the glibc doesn't copy the string. 
Python has to manage the string memory.

Internally, the posix module uses a "posix_putenv_garbage" dictionary mapping 
key (bytes) to value (bytes). Values are "key=value\0" strings.

The bpo-35381 issue converted the os ("posix" in practice) module PEP 384: 
"Remove all static state from posixmodule": commit 
b3966639d28313809774ca3859a347b9007be8d2. The _posix_clear() function is now 
called by _PyImport_Cleanup().

Problem: the glibc is not aware that Python is exiting and that the memory of 
the environment variable has been released. Next access to environment 
variables ("environ" C variable, putenv, setenv, unsetenv, ...) can crash. 
Sometimes, it doesn't crash even if the memory has been released, because 
free() does not always dig immediately holes in the heap memory (the complex 
problelm of memory fragmentation).

The posix module should notify the glibc that the memory will be released 
before releasing the memory, to avoid keeping dangling pointers in the 
"environ" C variable.

The following crash in the Elements module is an example of crash introduced by 
commit b3966639d28313809774ca3859a347b9007be8d2 which introduced this issue:
https://bugzilla.redhat.com/show_bug.cgi?id=1791761

----------
components: Interpreter Core
messages: 360309
nosy: vstinner
priority: normal
severity: normal
status: open
title: The os module should unset() environment variable at exit
versions: Python 3.9

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue39395>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to