FS#67157 - {arch-install-scripts} pacstrap doesn't wait for pacman to exit on SIGINT

Attached to Project: Arch Linux
Opened by Daan De Meyer (DaanDeMeyer) - Tuesday, 30 June 2020, 18:39 GMT
Last edited by Eli Schwartz (eschwartz) - Thursday, 06 August 2020, 17:29 GMT
Task Type Bug Report
Category Arch Projects
Status Closed
Assigned To Dave Reisner (falconindy)
Eli Schwartz (eschwartz)
Architecture All
Severity Low
Priority Normal
Reported Version
Due in Version Undecided
Due Date Undecided
Percent Complete 100%
Votes 0
Private No

Details

Description:

When running mkosi (https://github.com/systemd/mkosi.git) which in turns runs pacstrap when bootstrapping an Arch Linux image, interrupting mkosi (Ctrl+C) during the pacman installation phase makes mkosi's cleanup phase fail as it's unable to unmount <root>/var/cache/pacman/pkg. The umount operation fails with "OSError: [Errno 16] Device or resource busy: 'pkg'". We also see some more output from pacman after the subprocess.run(pacstrap) call in python has already finished.

The problem seems to be that pacman is still running after the pacstrap script exits. pacman correctly receives the SIGINT signal (which we can see from the interrupted message in the output) but unshare doesn't seem to wait until it exits. I tried adding a few 'wait' calls in pacstrap but that doesn't fix the problem. Removing the --fork option from the pacstrap call does fix the problem.

I checked the util-linux source code and unshare doesn't seem to handle the SIGINT and SIGTERM signals. It simply exits when it receives either one without waiting for the child to exit. I made an issue in the util-linux issue tracker as well but maybe we can check if we need the unshare call in pacstrap. It was added in e1a84d105699969a41136ddc0b3f7816ae907bfc but there's no motivation as to why it is necessary. At least for the mkosi use case, everything seems to work fine without it.

Output:

( 91/112) installing device-mapper [############################################################################] 100%
( 92/112) installing popt [############################################################################] 100%
( 93/112) installing json-c [############################################################################] 100%
( 94/112) installing argon2 [############################################################################] 100%
( 95/112) installing cryptsetup [############################################################################] 100%
( 96/112) installing dbus [############################################################################] 100%
( 97/112) installing libmnl [############################################################################] 100%
( 98/112) installing libnftnl [############################################################################] 100%
( 99/112) installing libnl [############################################################################] 100%
(100/112) installing libusb [############################################################################] 100%
(101/112) installing libpcap [############################################################################] 100%
(102/112) installing libnfnetlink [############################################################################] 100%
(103/112) installing libnetfilter_conntrack [############################################################################] 100%
(104/112) installing iptables [############################################################################] 100%
^C05/112) installing kbd [----------------------------------------------------------------------------] 0%
Interrupt signal received
‣ Unmounting Package Cache...
umount: /var/tmp/mkosi-k7xvvu5s/root/var/cache/pacman/pkg: target is busy.
‣ Unmounting Package Cache complete.
‣ Unmounting image...
umount: /var/tmp/mkosi-k7xvvu5s/root/var/cache/pacman/pkg: target is busy.
‣ Unmounting image complete.
‣ Detaching image file...
‣ Detaching image file complete.
Traceback (most recent call last):
File "../mkosi/mkosi", line 5143, in <module>
main()
File "../mkosi/mkosi", line 5137, in main
run_verb(a)
File "../mkosi/mkosi", line 5114, in run_verb
build_stuff(args)
File "../mkosi/mkosi", line 4939, in build_stuff
raw, tar, root_hash = build_image(args, workspace, do_run_build_script=False, cleanup=True)
File "../mkosi/mkosi", line 4732, in build_image
install_distribution(args, workspace.name,
File "../mkosi/mkosi", line 2385, in install_distribution
install[args.distribution](args, workspace, do_run_build_script)
File "/usr/lib/python3.8/contextlib.py", line 75, in inner
return func(*args, **kwds)
File "../mkosi/mkosi", line 2183, in install_arch
run_pacstrap(packages)
File "../mkosi/mkosi", line 2180, in run_pacstrap
run(cmdline + list(packages), check=True)
File "../mkosi/mkosi", line 103, in run
raise KeyboardInterrupt()
KeyboardInterrupt
(105/112) installing kbd [############################################################################] 100%
Traceback (most recent call last):
File "/usr/lib/python3.8/weakref.py", line 642, in _exitfunc
f()
File "/usr/lib/python3.8/weakref.py", line 566, in __call__
return info.func(*info.args, **(info.kwargs or {}))
File "/usr/lib/python3.8/tempfile.py", line 818, in _cleanup
cls._rmtree(name)
File "/usr/lib/python3.8/tempfile.py", line 814, in _rmtree
_shutil.rmtree(name, onerror=onerror)
File "/usr/lib/python3.8/shutil.py", line 715, in rmtree
_rmtree_safe_fd(fd, path, onerror)
File "/usr/lib/python3.8/shutil.py", line 652, in _rmtree_safe_fd
_rmtree_safe_fd(dirfd, fullname, onerror)
File "/usr/lib/python3.8/shutil.py", line 652, in _rmtree_safe_fd
_rmtree_safe_fd(dirfd, fullname, onerror)
File "/usr/lib/python3.8/shutil.py", line 652, in _rmtree_safe_fd
_rmtree_safe_fd(dirfd, fullname, onerror)
[Previous line repeated 1 more time]
File "/usr/lib/python3.8/shutil.py", line 656, in _rmtree_safe_fd
onerror(os.rmdir, fullname, sys.exc_info())
File "/usr/lib/python3.8/shutil.py", line 654, in _rmtree_safe_fd
os.rmdir(entry.name, dir_fd=topfd)
OSError: [Errno 16] Device or resource busy: 'pkg'
This task depends upon

Closed by  Eli Schwartz (eschwartz)
Thursday, 06 August 2020, 17:29 GMT
Reason for closing:  Fixed
Additional comments about closing:  OP request: util-linux version with my merged PR has been released. I can confirm the issue is fixed on my end
Comment by Dave Reisner (falconindy) - Tuesday, 30 June 2020, 18:43 GMT
PID namespace use in pacstrap probably mirrors its use in arch-chroot. See 2be79c6259cfbf9ebcd258a68fea3ec79f532e32 for rationale.
Comment by Eli Schwartz (eschwartz) - Tuesday, 30 June 2020, 19:03 GMT
That probably should have been a unique commit, yes, but I confirm I specifically added it to ensure it works like arch-chroot. pacman runs install scripts which invoke gnupg, and IIRC I think the usage I added here made it more likely gnupg lingering issues would occur.
Comment by Daan De Meyer (DaanDeMeyer) - Tuesday, 30 June 2020, 20:08 GMT
I opened a (basic) pull request for util-linux that fixes the issue: https://github.com/karelzak/util-linux/pull/1087

Loading...