I know this pattern to read the umask in Python:
current_umask = os.umask(0) # line1
os.umask(current_umask) # line2
return current_umask # line3
But this is not thread-safe.
A thread which executes between line1 and line2 will have a different umask.
Is there a thread-safe way to read the umask in Python?
Related: https://bugs.python.org/issue35275
if your system has Umask
field in /proc/[pid]/status
, you could read from on it:
import os
def getumask():
pid = os.getpid()
with open(f'/proc/{pid}/status') as f:
for l in f:
if l.startswith('Umask'):
return int(l.split()[1], base=8)
return None
tested under CentOS 7.5, Debian 9.6.
or, you could add a thread lock :)