Asana / python-asana

Official Python client library for the Asana API v1
MIT License
303 stars 105 forks source link

Do not spawn uname(1) every time version info is needed #151

Open wdoekes opened 2 years ago

wdoekes commented 2 years ago

On Unixes, there is a os.uname() call which provides the same info and we don't need to fork()+execve() to get it.

That uname(1) call wastes resources and trips IDSes with its unexpected fork().

Before:

$ strace -feexecve python3 -c "import os; import platform; __version__ = 'x'; print({'language': 'Python', 'version': __version__, 'language_version': platform.python_version(), 'os': platform.system(), 'os_version': platform.release()})"
execve("/usr/bin/python3", ["python3", "-c", "import os; import platform; __ve"...], 0x7ffc828c5c38 /* 63 vars */) = 0
strace: Process 4112873 attached
[pid 4112873] execve("/usr/local/sbin/uname", ["uname", "-p"], 0x7fff0c485da8 /* 63 vars */) = -1 ENOENT (No such file or directory)
[pid 4112873] execve("/usr/local/bin/uname", ["uname", "-p"], 0x7fff0c485da8 /* 63 vars */) = -1 ENOENT (No such file or directory)
[pid 4112873] execve("/usr/sbin/uname", ["uname", "-p"], 0x7fff0c485da8 /* 63 vars */) = -1 ENOENT (No such file or directory)
[pid 4112873] execve("/usr/bin/uname", ["uname", "-p"], 0x7fff0c485da8 /* 63 vars */) = -1 ENOENT (No such file or directory)
[pid 4112873] execve("/sbin/uname", ["uname", "-p"], 0x7fff0c485da8 /* 63 vars */) = -1 ENOENT (No such file or directory)
[pid 4112873] execve("/bin/uname", ["uname", "-p"], 0x7fff0c485da8 /* 63 vars */) = 0
[pid 4112873] +++ exited with 0 +++
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=4112873, si_uid=1000, si_status=0, si_utime=0, si_stime=0} ---
{'language': 'Python', 'version': 'x', 'language_version': '3.8.10', 'os': 'Linux', 'os_version': '5.4.0-109-generic'}
+++ exited with 0 +++

After:

$ strace -feexecve python3 -c "import os; import platform; __version__ = 'x'; print({'language': 'Python', 'version': __version__, 'language_version': platform.python_version(), 'os': (hasattr(os, 'uname') and os.uname().sysname or platform.system()), 'os_version': (hasattr(os, 'uname') and os.uname().release or platform.release())})"
execve("/usr/bin/python3", ["python3", "-c", "import os; import platform; __ve"...], 0x7ffdf3b12e18 /* 63 vars */) = 0
{'language': 'Python', 'version': 'x', 'language_version': '3.8.10', 'os': 'Linux', 'os_version': '5.4.0-109-generic'}
+++ exited with 0 +++
wdoekes commented 2 years ago

Anything I can do to get this moving?