in antlir/unshare.py [0:0]
def __exit__(self, exc_type, exc_val, exc_tb):
if self._namespace_to_file is not None:
# We need to close the files to let the namespaces be destroyed
for f in self._namespace_to_file.values():
try:
f.close()
# Covering this realistically seems really hard, since this
# should never fail. Manual test:
#
# >>> log = logging.getLogger('moo')
# >>> f = open('/proc/self/ns/mnt', 'rb')
# >>> try:
# ... 1/0
# ... except:
# ... log.exception(f'Closing namespace file {f.name}')
# ...
# Closing namespace file /proc/self/ns/mnt
# Traceback (most recent call last):
# File "<stdin>", line 2, in <module>
# ZeroDivisionError: division by zero
except BaseException: # pragma: no cover
log.exception(f"Closing namespace file {f.name}")
self._namespace_to_file = None
try:
if self._root_fd:
os.close(self._root_fd)
# Same coverage story as above for the `f.close()`
except BaseException: # pragma: no cover
log.exception(f"Closing root directory FD {self._root_fd}")
self._root_fd = None
if self._keepalive_proc:
try:
self._keepalive_proc.stdin.close() # "Normally" won't fail
if self._keepalive_proc.wait() != 0: # prag
log.warning(
"Unshare keepalive exited with {}".format(
self._keepalive_proc.returncode
)
)
finally:
self._keepalive_proc = None