From 2a56a105be98e286150c8413dd93b4eaf8347b11 Mon Sep 17 00:00:00 2001 From: scuttletoby <123843277+scuttletoby@users.noreply.github.com> Date: Tue, 17 Nov 2026 20:42:58 +0100 Subject: [PATCH 1/3] pc-agents setup: repair an interrupted dpkg with no audit output; LC_ALL=C; test fixes (#349) - Run `dpkg --configure -a` also when /var/lib/dpkg/updates holds a file named all digits: apt-get install refuses on it while `dpkg --audit` names nothing when the journal says installed. - Run apt-get and dpkg under LC_ALL=C so a lock message reads the same in every language. - LOCKED no longer matches `unable to lock`, which follows any lock failure, not only another apt-get holding the lock. - The fake `dpkg --audit` exits 0 when it names a package, as real dpkg does; a row for an audit that fails and names nothing. - A desk test row for the [experimental] key regex (space and tab only). Co-Authored-By: Claude Opus 5.5 --- fork-kit/desk/tests/windows.test.py | 6 ++- fork-kit/pc-agents/README.md | 6 ++- fork-kit/pc-agents/sbin/pc-agents | 32 +++++++++++---- fork-kit/pc-agents/tests/fake_distro.py | 54 +++++++++++++++++-------- fork-kit/pc-agents/tests/setup.test.py | 49 +++++++++++++++++++++- 5 files changed, 118 insertions(+), 29 deletions(-) diff --git a/fork-kit/desk/tests/windows.test.py b/fork-kit/desk/tests/windows.test.py index cf704146d1b2e..d15d4c36aa191 100644 --- a/fork-kit/desk/tests/windows.test.py +++ b/fork-kit/desk/tests/windows.test.py @@ -303,7 +303,11 @@ def test_no_change_means_the_file_is_left_alone(self): path = os.path.join(d, '.wslconfig') for old, notes in (('[wsl2]\nmemory = 9GB\nNetworkingMode=NAT\n', []), ('[wsl2]\nmemory=9GB\nnetworkingMode=nat\nmemory=12GB\n', - ['[wsl2] memory=12GB: a repeated line, removed'])): + ['[wsl2] memory=12GB: a repeated line, removed']), + # WSL trims space and tab only: a key after a non-breaking space or a form feed is not + # networkingMode to WSL, so it is not removed (and the check does not FAIL on it either) + ('[wsl2]\nmemory=9GB\nnetworkingMode=nat\n[experimental]\n networkingMode=mirrored\n' + '\fdnsTunneling=true\n', [])): with self.subTest(old): with open(path, 'w', encoding='utf-8', newline='\n') as f: f.write(old) diff --git a/fork-kit/pc-agents/README.md b/fork-kit/pc-agents/README.md index 3bbdb8d5d9775..bcda1c5b00054 100644 --- a/fork-kit/pc-agents/README.md +++ b/fork-kit/pc-agents/README.md @@ -236,8 +236,10 @@ allowed (connection tracking). - **Packages:** setup installs `nftables`, `libatomic1` and `unzip` when `dpkg` does not list one as installed (held counts as installed; a distro set up by an older kit gets the two new ones at its next run; no T3 restart for them). It installs them after it writes its files, so `apt-get` resolves through the machine file's `nameservers` - as they are now, and before it loads the firewall. A package install stopped half-way (`dpkg --audit` names it) is - finished first (`dpkg --configure -a`, run only then). Nothing asks a question: a changed config file is kept and + as they are now, and before it loads the firewall. A package install stopped half-way (`dpkg --audit` names it, or + `/var/lib/dpkg/updates` holds a file named all digits, which `apt-get install` refuses on as well) is finished first + (`dpkg --configure -a`, run only then). `apt-get` and `dpkg` run under `LC_ALL=C`, so a lock reads the same in + every language. Nothing asks a question: a changed config file is kept and debconf takes its defaults. A lock another `apt-get` or `dpkg` holds (apt-daily on a new distro) is waited for, up to 5 minutes a step: `apt-get` waits for dpkg's lock itself, and setup runs `dpkg`, `apt-get update` and `apt-get install` again while they fail on the locks they take without a wait. A failed step stops setup, naming diff --git a/fork-kit/pc-agents/sbin/pc-agents b/fork-kit/pc-agents/sbin/pc-agents index d160dbfb97fca..03ba1f4540998 100755 --- a/fork-kit/pc-agents/sbin/pc-agents +++ b/fork-kit/pc-agents/sbin/pc-agents @@ -192,8 +192,23 @@ def install_t3(c, version, done): return None -# What dpkg and apt-get say when another one (apt-daily, unattended-upgrades) holds a lock they take without a wait -LOCKED = re.compile(r'could not get lock|unable to lock|(is|was) locked by another process', re.I) +# What dpkg and apt-get say (under LC_ALL=C) when another one (apt-daily, unattended-upgrades) holds a lock they take +# without a wait. Not apt's `Unable to lock directory` or dpkg's `unable to lock`: those follow any lock failure (a +# lock file that cannot be opened too), which waiting does not cure. +LOCKED = re.compile(r'could not get lock|(is|was) locked by another process', re.I) + + +def dpkg_interrupted(): + """True when a dpkg run stopped before it saved its status: /var/lib/dpkg/updates holds a file named all digits + (apt-get install then refuses: "dpkg was interrupted"). `dpkg --audit` does not name the packages of such a + journal when their last state is installed. A folder that cannot be read counts as interrupted.""" + try: + names = os.listdir(k.ROOT + '/var/lib/dpkg/updates') + except FileNotFoundError: + return False + except OSError: + return True + return any(re.fullmatch(r'[0-9]+', n) for n in names) def run_locked(argv, env): @@ -210,9 +225,10 @@ def run_locked(argv, env): def install_packages(): """Install the k.PACKAGES that are not (fully) installed; held counts as installed. A dpkg run stopped half-way - (`dpkg --audit` names a package) is finished first (apt-get refuses until it is). Nothing asks a question (stdin - is /dev/null): a changed config file is kept, debconf takes its defaults, and a lock apt-daily holds is waited - for (run_locked). Returns why not, or None.""" + (`dpkg --audit` names a package, or dpkg_interrupted()) is finished first (apt-get refuses until it is). Nothing + asks a question (stdin is /dev/null): a changed config file is kept, debconf takes its defaults, and a lock + apt-daily holds is waited for (run_locked). apt and dpkg run under LC_ALL=C: LOCKED reads their English (sudo + keeps the caller's LANG). Returns why not, or None.""" r = k.run(['/usr/bin/dpkg-query', '-W', '-f=${Package}\t${Status}\n', *k.PACKAGES]) have = {p for p, _, status in (line.partition('\t') for line in r.stdout.splitlines()) if status.endswith(' ok installed')} @@ -220,11 +236,11 @@ def install_packages(): if not missing: return None say('installing ' + ' '.join(missing)) - quiet = {'DEBIAN_FRONTEND': 'noninteractive'} + quiet = {'DEBIAN_FRONTEND': 'noninteractive', 'LC_ALL': 'C'} busy = (' Another apt-get or dpkg still runs (apt-daily, on a new distro) after 5 minutes: ' 'wait for it to end, then run setup again') - r = k.run(['/usr/bin/dpkg', '--audit']) # reads only: no lock - if r.returncode or r.stdout.strip(): + r = k.run(['/usr/bin/dpkg', '--audit'], env={'LC_ALL': 'C'}) # reads only: no lock; exits 0 when it names one + if r.returncode or r.stdout.strip() or dpkg_interrupted(): r = run_locked(['/usr/bin/dpkg', '--force-confdef', '--force-confold', '--configure', '-a'], quiet) if r.returncode: return (f'cannot install {" ".join(missing)}: dpkg --configure -a failed ({r.stderr.strip()[-300:]}).' diff --git a/fork-kit/pc-agents/tests/fake_distro.py b/fork-kit/pc-agents/tests/fake_distro.py index 2a1a3a0b75e75..38d9619db1e26 100644 --- a/fork-kit/pc-agents/tests/fake_distro.py +++ b/fork-kit/pc-agents/tests/fake_distro.py @@ -81,20 +81,32 @@ 'esac\n', # apt-get [-o OPTION]... update|install -y PKG...: logs the rest (and the options, in /log/apt-get-options); # installs each package in /log/packages (nftables brings the fake nft kept in /log/nft-package). As real apt-get: - # refuses while a package is unpacked (dpkg was interrupted). /log/apt-locked-update (or -install) holds how many - # more runs find the lists (or archives) lock held, as while apt-daily runs: real apt-get does not wait for those. - # /log/apt-fail makes it fail; /log/dns holds the resolv.conf line `update` needs to reach the mirror; with - # /log/questions it fails unless noninteractive and keeping changed config files. + # refuses while a package is unpacked, and `install` while /var/lib/dpkg/updates holds a file named all digits + # (dpkg was interrupted). /log/apt-locked-update (or -install) holds how many more runs find the lists (or + # archives) lock held, as while apt-daily runs: real apt-get does not wait for those. Its lock messages are in + # German under a German locale (LC_ALL, else LC_MESSAGES, else LANG), as libapt's translations; /log/apt-denied + # makes the lock file one it may not open (not root). /log/apt-fail makes it fail; /log/dns holds the resolv.conf + # line `update` needs to reach the mirror; with /log/questions it fails unless noninteractive and keeping changed + # config files. 'usr/bin/apt-get': 'R="${0%/usr/bin/apt-get}"; o=; while [ "$1" = -o ]; do o="$o $2"; shift 2; done\n' 'echo "$*" >>"$R/log/apt-get"; echo "${o# }" >>"$R/log/apt-get-options"\n' + '[ "$1" = update ] && d=/var/lib/apt/lists || d=/var/cache/apt/archives\n' + '[ -e "$R/log/apt-denied" ] && { echo "E: Could not open lock file $d/lock - open (13: ' + 'Permission denied)" >&2; echo "E: Unable to lock directory $d/" >&2; exit 100; }\n' 'n=$(cat "$R/log/apt-locked-$1" 2>/dev/null || echo 0); if [ "$n" -gt 0 ]; then\n' - ' echo $((n - 1)) >"$R/log/apt-locked-$1"; [ "$1" = update ] && d=/var/lib/apt/lists || ' - 'd=/var/cache/apt/archives\n' - ' echo "E: Could not get lock $d/lock. It is held by process 4242 (apt-get) (test)" >&2\n' - ' echo "E: Unable to lock directory $d/" >&2; exit 100; fi\n' + ' echo $((n - 1)) >"$R/log/apt-locked-$1"\n' + ' case "${LC_ALL:-${LC_MESSAGES:-$LANG}}" in de*)\n' + ' echo "E: Sperre $d/lock konnte nicht erlangt werden. Sie wird von Prozess 4242 (apt-get) ' + 'gehalten (test)" >&2\n' + ' echo "E: Das Verzeichnis $d/ kann nicht gesperrt werden." >&2;;\n' + ' *) echo "E: Could not get lock $d/lock. It is held by process 4242 (apt-get) (test)" >&2\n' + ' echo "E: Unable to lock directory $d/" >&2;; esac; exit 100; fi\n' '[ -e "$R/log/apt-fail" ] && { echo "E: no network (test)" >&2; exit 100; }\n' 'grep -qs unpacked "$R"/log/packages/* && { echo "E: dpkg was interrupted, you must manually ' 'run \'dpkg --configure -a\' to correct the problem." >&2; exit 100; }\n' + '[ "$1" != install ] || ! ls "$R/var/lib/dpkg/updates" 2>/dev/null | grep -qxE "[0-9]+" || ' + '{ echo "E: dpkg was interrupted, you must manually run \'dpkg --configure -a\' to correct the ' + 'problem." >&2; exit 100; }\n' '[ ! -e "$R/log/questions" ] || { [ "$DEBIAN_FRONTEND" = noninteractive ] && case "$o" in ' '*Dpkg::Options::=--force-confold*) :;; *) false;; esac; } || ' '{ echo "E: end of file on stdin at conffile prompt (test)" >&2; exit 100; }\n' @@ -107,23 +119,31 @@ 'usr/bin/dpkg-query': 'R="${0%/usr/bin/dpkg-query}"; echo "$*" >>"$R/log/dpkg-query"; shift 2; rc=0\n' 'for p; do if [ -e "$R/log/packages/$p" ]; then printf "%s\\t%s\\n" "$p" "$(cat "$R/log/packages/$p")"\n' ' else echo "dpkg-query: no packages found matching $p" >&2; rc=1; fi; done; exit $rc\n', - # dpkg --audit: names each package an apt-get stopped half-way left unpacked (no lock: it only reads). - # dpkg [--force-...]... --configure -a: finishes them. /log/dpkg-locked holds how many more runs find the lock - # held (real dpkg does not wait); /log/questions as for apt-get. + # dpkg --audit: names each package an apt-get stopped half-way left unpacked (no lock: it only reads) and, as + # real dpkg, exits 0 all the same; /log/dpkg-audit-fail makes it fail (exit 2) and name nothing, as on a status + # file it cannot parse. It does not name a package whose last state, in a journal file under + # /var/lib/dpkg/updates, is installed. dpkg [--force-...]... --configure -a: finishes them and merges that journal + # (removes its files). /log/dpkg-locked holds how many more runs find the lock held (real dpkg does not wait), + # with the message in German under a German locale; /log/questions as for apt-get. 'usr/bin/dpkg': 'R="${0%/usr/bin/dpkg}"; echo "$*" >>"$R/log/dpkg"\n' - 'if [ "$1" = --audit ]; then rc=0; for f in "$R"/log/packages/*; do grep -qs unpacked "$f" || ' - 'continue\n' - ' [ $rc = 1 ] || echo "The following packages have been unpacked but not yet configured:"\n' - ' echo " ${f##*/}"; rc=1; done; exit $rc; fi\n' + 'if [ "$1" = --audit ]; then [ ! -e "$R/log/dpkg-audit-fail" ] || ' + '{ echo "dpkg: error: parsing file \'/var/lib/dpkg/status\' (test)" >&2; exit 2; }; h=\n' + ' for f in "$R"/log/packages/*; do grep -qs unpacked "$f" || continue\n' + ' [ -n "$h" ] || echo "The following packages have been unpacked but not yet configured:"\n' + ' echo " ${f##*/}"; h=1; done; exit 0; fi\n' 'n=$(cat "$R/log/dpkg-locked" 2>/dev/null || echo 0); if [ "$n" -gt 0 ]; then\n' ' echo $((n - 1)) >"$R/log/dpkg-locked"\n' - ' echo "dpkg: error: dpkg frontend lock was locked by another process with pid 4242 (test)" >&2\n' + ' case "${LC_ALL:-${LC_MESSAGES:-$LANG}}" in de*) echo "dpkg: Fehler: dpkg-Frontend-Sperre wurde ' + 'von einem anderen Prozess mit PID 4242 gesperrt (test)" >&2;;\n' + ' *) echo "dpkg: error: dpkg frontend lock was locked by another process with pid 4242 (test)" >&2;; ' + 'esac\n' ' exit 2; fi\n' '[ ! -e "$R/log/questions" ] || { [ "$DEBIAN_FRONTEND" = noninteractive ] && case "$*" in ' '*--force-confold*) :;; *) false;; esac; } || ' '{ echo "dpkg: error: end of file on stdin at conffile prompt (test)" >&2; exit 1; }\n' - 'for f in "$R"/log/packages/*; do [ ! -e "$f" ] || sed -i "s/unpacked/installed/" "$f"; done\n', + 'for f in "$R"/log/packages/*; do [ ! -e "$f" ] || sed -i "s/unpacked/installed/" "$f"; done\n' + 'rm -f "$R"/var/lib/dpkg/updates/[0-9]*\n', 'usr/sbin/nft': 'R="${0%/usr/sbin/nft}"\ncase "$1" in -f) cp "$R$2" "$R/log/nft-loaded";;\n' 'list) cat "$R/log/nft-loaded" 2>/dev/null || exit 1;; esac\n', } diff --git a/fork-kit/pc-agents/tests/setup.test.py b/fork-kit/pc-agents/tests/setup.test.py index a7a38cdd7d9f3..067d9bfdea9e2 100644 --- a/fork-kit/pc-agents/tests/setup.test.py +++ b/fork-kit/pc-agents/tests/setup.test.py @@ -387,11 +387,36 @@ def test_dpkg_and_apt_get_ask_nothing(self): def test_dpkg_configure_runs_only_when_something_is_half_installed(self): """`dpkg --configure -a` takes dpkg's lock without waiting: on a fresh distro with nothing half-installed - it is not run, so unattended-upgrades holding that lock is left to apt-get's own wait (#321).""" + it is not run, so unattended-upgrades holding that lock is left to apt-get's own wait (#321). A journal file + whose name is not all digits is one apt-get does not look at either (#349).""" + self.write('/var/lib/dpkg/updates/tmp.i', '') self.ready() self.assertEqual(self.log('dpkg'), ['--audit']) self.assertEqual(self.log('apt-get'), ['update', 'install -y libatomic1 unzip']) + def test_dpkg_configure_runs_when_its_audit_fails(self): + """A `dpkg --audit` that fails and names nothing (a status file it cannot parse) is not taken as all clear: + `dpkg --configure -a` runs (#349).""" + self.write('/log/dpkg-audit-fail', '') + self.ready() + self.assertEqual(self.log('dpkg'), ['--audit', DPKG]) + + def test_an_interrupted_dpkg_that_names_nothing_is_finished(self): + """dpkg stopped after its last status change but before saving it (`wsl --shutdown` during + unattended-upgrades): its journal under /var/lib/dpkg/updates says installed, so `dpkg --audit` names + nothing, yet apt-get install refuses ("dpkg was interrupted") while that folder holds a file named all + digits, on every run. Setup finishes it first, as apt asks; the next run has nothing to finish (#349).""" + self.write('/var/lib/dpkg/updates/0003', 'Package: unzip\nStatus: install ok installed\n') + self.write('/var/lib/dpkg/updates/tmp.i', '') + self.ready() + self.assertEqual(self.log('dpkg'), ['--audit', DPKG]) + self.assertEqual(os.listdir(self.path('/var/lib/dpkg/updates')), ['tmp.i']) + self.assertEqual(self.log('apt-get'), ['update', 'install -y libatomic1 unzip']) + os.unlink(self.path('/log/packages/unzip')) # and a later run with a package to install + code, out = self.setup_run() + self.assertEqual(code, 0, out) + self.assertEqual(self.log('dpkg'), ['--audit', DPKG, '--audit']) + def test_a_lock_apt_daily_holds_is_waited_for(self): """A new distro where apt-daily runs: apt-get update's lists lock, install's archives lock and dpkg's own lock are taken without a wait (only DPkg::Lock::Timeout's frontend lock waits), so setup tries again (#321).""" @@ -404,6 +429,28 @@ def test_a_lock_apt_daily_holds_is_waited_for(self): self.assertEqual(self.read('/log/packages/libatomic1'), 'install ok installed\n') self.assertEqual(self.check()[0], 0) + def test_a_lock_is_told_apart_under_any_locale(self): + """sudo keeps LANG and LC_*, and apt and dpkg ship translations: setup runs them under LC_ALL=C, so a lock + held while the owner's locale is German is still waited for (#349).""" + env = {n: v for n, v in os.environ.items() if n not in ('LC_ALL', 'LC_MESSAGES')} + with mock.patch.dict(os.environ, {**env, 'LANG': 'de_DE.UTF-8'}, clear=True): + self.write('/log/packages/unzip', 'install ok unpacked') + for name in ('dpkg-locked', 'apt-locked-update', 'apt-locked-install'): + self.write('/log/' + name, '2') + self.ready() + self.assertEqual(self.log('dpkg'), ['--audit'] + [DPKG] * 3) + self.assertEqual(self.log('apt-get'), ['update'] * 3 + ['install -y libatomic1 unzip'] * 3) + + def test_a_lock_file_it_may_not_open_is_not_waited_for(self): + """apt-get adds `Unable to lock directory` to any lock failure, also one no other apt-get causes (here the + lock file cannot be opened): that one is not tried again for 5 minutes, nor named as a busy apt (#349).""" + self.write('/log/apt-denied', '') + code, out = self.setup_run() + self.assertEqual(code, 1, out) + self.assertIn('Permission denied', out) + self.assertNotIn('Another apt-get or dpkg still runs', out) + self.assertEqual(self.log('apt-get'), ['update']) + def test_a_lock_held_past_the_wait_is_named_with_its_own_fix(self): """A lock still held after the wait (5 minutes on a machine): the advice is to wait for the other apt-get or dpkg, not to fix the network; nothing is installed, and the next run goes on.""" From 8814e29ce1ef15c11fb11878460bfc571303953b Mon Sep 17 00:00:00 2001 From: scuttletoby <123843277+scuttletoby@users.noreply.github.com> Date: Tue, 17 Nov 2026 21:43:05 +0100 Subject: [PATCH 2/3] start-desk: read .wslconfig section headers as WSL does; dpkg journal test gaps (#349) The section-header regex now follows WSL's ParseSection: space or tab, then a letter-led alphanumeric name in brackets, then space/tab and an optional # comment. A bracket line WSL rejects no longer starts a section (rows for [experimental] # comment, [ wsl2 ], [WSL2], a tab, a vertical tab, and a header rejected after its name). pc-agents tests: no updates folder, an empty one, 0003.new, and an unreadable folder (a file, mode 000). Co-Authored-By: Claude Opus 5.5 --- fork-kit/desk/README.md | 3 +- fork-kit/desk/tests/windows.test.py | 53 +++++++++++++++++++++++++ fork-kit/desk/windows/start-desk.ps1 | 15 ++++++- fork-kit/pc-agents/tests/fake_distro.py | 2 +- fork-kit/pc-agents/tests/setup.test.py | 35 +++++++++++++--- 5 files changed, 99 insertions(+), 9 deletions(-) diff --git a/fork-kit/desk/README.md b/fork-kit/desk/README.md index 8836e147557cb..08f89ba9786cf 100644 --- a/fork-kit/desk/README.md +++ b/fork-kit/desk/README.md @@ -117,7 +117,8 @@ leaves a mark in `%LOCALAPPDATA%\fork-desk`, so the next run asks no takeover qu three quarters of the RAM, at most the RAM less 6 GB, never under 4 GB; one that leaves Windows under 2 GB is refused; without `-MemoryGB`, a higher one already there, raised by hand, is kept up to the RAM less 2 GB; one WSL cannot read, such as `26 GB` or `26gb`, is rewritten, since WSL would run on half the RAM), swap (`-SwapGB`, 4), NAT and DNS tunnelling, for every WSL distro (read and written as UTF-8 without a -BOM; a dated copy of the old file is kept, other keys and sections stay; an old `networkingMode` or `dnsTunneling` +BOM; a dated copy of the old file is kept, other keys and sections stay; a section header is read as WSL reads it, +so a line WSL rejects, such as `[ wsl2 ]`, starts no section and the key under it is not taken as set; an old `networkingMode` or `dnsTunneling` line under `[experimental]`, the form before WSL 2.0 that can still win over `[wsl2]`, is removed, and the check FAILs while one is there; the question lists each change, old -> new, and the file is written only when it lists one, so "no change" means the file is left alone); the distro's own settings the WSL2 walls diff --git a/fork-kit/desk/tests/windows.test.py b/fork-kit/desk/tests/windows.test.py index d15d4c36aa191..5d5539c4395e4 100644 --- a/fork-kit/desk/tests/windows.test.py +++ b/fork-kit/desk/tests/windows.test.py @@ -366,6 +366,59 @@ def test_wslconfig_merge_removes_old_experimental_networking_keys(self): '[experimental] dnsTunneling=false: removed', 'changed=True', 'left=sparseVhd', 'after=0'): self.assertIn(line, r.stdout) + @unittest.skipUnless(shutil.which('pwsh'), 'pwsh is not installed') + def test_section_headers_are_read_as_wsl_reads_them(self): + """lens #354: WSL's ParseSection (configfile.cpp) takes space or tab, '[', a letter then letters or digits, + ']', space or tab, then the end or a '#' comment, in any case. On a '[' line it rejects, the keys after it go + under the old section's length of the letters it had read: none read keeps the section.""" + d = tempfile.mkdtemp(prefix='desk-wslconfig-test-') + self.addCleanup(shutil.rmtree, d, True) + path = os.path.join(d, '.wslconfig') + want = '[wsl2]\nmemory=9GB\nnetworkingMode=nat\nguiApplications=false\n' + # (old file, the [wsl2] memory WSL reads, [experimental] keys WSL reads, the file after the merge) + rows = ( + ('[wsl2]\nmemory=9GB\n[experimental] # from an old guide\ndnsTunneling=false\n', '9GB', 'dnsTunneling', + want + '[experimental] # from an old guide\n'), + ('[experimental]\nautoProxy=true\n[ wsl2 ]\nmemory=4GB\n', '', 'autoProxy,memory', + '[experimental]\nautoProxy=true\n[ wsl2 ]\nmemory=4GB\n' + want), + ('[WSL2]\nmemory=9GB\nnetworkingMode=nat\nguiApplications=false\n', '9GB', '', None), + ('\t[wsl2]\nmemory=9GB\nnetworkingMode=nat\nguiApplications=false\n', '9GB', '', None), + # a vertical tab is not space to WSL: the line is rejected, so this is a repeated [wsl2] key + (want + '\v[experimental]\nnetworkingMode=mirrored\n', '9GB', '', want + '\v[experimental]\n'), + # rejected after the name: WSL keeps the old section's length (4) of it, 'expe' here, so neither section + ('[wsl2]\nmemory=9GB\n[experimental] old\nnetworkingMode=mirrored\n', '9GB', '', + want + '[experimental] old\nnetworkingMode=mirrored\n'), + # ... and from [boot] (also 4 letters) into wsl2 + ('[boot]\nsystemd=true\n[wsl2] old\nmemory=9GB\nnetworkingMode=nat\nguiApplications=false\n', '9GB', '', + None), + ) + script = os.path.join(d, 'merge.ps1') + with open(script, 'w', encoding='ascii') as f: + f.write(wslconfig_part() + '\n$want = [ordered]@{ memory = "9GB"; networkingMode = "nat"; ' + 'guiApplications = "false" }\n$l = Read-WslConfig $args[0]\n' + '"memory=$((Wsl2Keys $l)[\'memory\'])"\n' + '"experimental=$((SectionKeys $l \'experimental\').Keys | Sort-Object | Join-String -Separator \',\')"\n' + '"changed=$(Merge-WslConfig $args[0] $want)"\n' + '"after=$(@(Changes (Read-WslConfig $args[0]) $want).Count)"\n' + '"now=$((Wsl2Keys (Read-WslConfig $args[0]))[\'memory\'])"\n') + for old, memory, experimental, new in rows: + with self.subTest(old): + with open(path, 'w', encoding='utf-8', newline='\n') as f: + f.write(old) + r = subprocess.run(['pwsh', '-NoProfile', '-NonInteractive', '-File', script, path], + capture_output=True, text=True, timeout=120, stdin=subprocess.DEVNULL) + self.assertEqual(r.returncode, 0, r.stdout + r.stderr) + out = r.stdout.splitlines() + self.assertIn(f'memory={memory}', out) # what the check at the end reads + self.assertIn(f'experimental={experimental}', out) + self.assertIn(f'changed={new is not None}', out) + self.assertEqual(slurp(path), old if new is None else new) + self.assertIn('after=0', out) + self.assertIn('now=9GB', out) + for n in os.listdir(d): + if n.startswith('.wslconfig.bak-'): + os.unlink(os.path.join(d, n)) + @unittest.skipUnless(shutil.which('pwsh'), 'pwsh is not installed') def test_the_memory_cap_kept_is_one_wsl_reads_within_its_bounds(self): """Without -MemoryGB, a higher cap a person set (more agents or jobs in WSL) is neither lowered nor a FAIL, nor diff --git a/fork-kit/desk/windows/start-desk.ps1 b/fork-kit/desk/windows/start-desk.ps1 index d1bf095f42c19..63ee51aa4dbf3 100644 --- a/fork-kit/desk/windows/start-desk.ps1 +++ b/fork-kit/desk/windows/start-desk.ps1 @@ -121,12 +121,22 @@ function Read-WslConfig([string]$path) { # networkingMode and dnsTunneling were [experimental] keys before WSL 2.0; an old line there can still win over # [wsl2], so Merge-WslConfig removes them there (the [wsl2] ones are what the desk needs) and the check FAILs on them. $Experimental = @('networkingMode', 'dnsTunneling') +# The section after line $l, as WSL's ParseSection reads a header: space or tab, an open bracket, a letter then letters +# or digits, a close bracket, space or tab, then the end or a # comment; any case. A bracket line it rejects keeps the +# length of $sec of the letters read (none: $sec as it was; fewer pad with NUL and match no section). $null: no bracket. +function NextSection([string]$l, [string]$sec) { + if ($l -cnotmatch '^[ \t]*\[(([A-Za-z][A-Za-z0-9]*)(\][ \t]*(#.*)?$)?)?') { return $null } + if ($Matches[3]) { return $Matches[2].ToLower() } + if ($Matches[2]) { return $Matches[2].ToLower().PadRight($sec.Length, [char]0).Substring(0, $sec.Length) } + $sec +} # A key=value line, trimmed of space and tab only, as WSL's config parser does: a non-breaking space or a form feed # stays in the value (WSL then rejects a memory= value, so it is rewritten), where .NET's \s would drop it. function SectionKeys([string[]]$lines, [string]$section) { # the keys of one section only $keys = @{}; $sec = '' foreach ($l in $lines) { - if ($l -match '^\s*\[(.+)\]\s*$') { $sec = $Matches[1].Trim().ToLower(); continue } + $next = NextSection $l $sec + if ($null -ne $next) { $sec = $next; continue } if ($sec -eq $section -and $l -match '^[ \t]*([^=#; \t]+)[ \t]*=[ \t]*(.*?)[ \t]*$') { $keys[$Matches[1]] = $Matches[2] } } $keys @@ -139,7 +149,8 @@ function MergeLines([string[]]$lines, $want) { $out = [Collections.Generic.List[string]]::new(); $notes = [Collections.Generic.List[string]]::new(); $sec = ''; $done = @{} function AddMissing { foreach ($k in $want.Keys) { if (-not $done[$k]) { $out.Add("$k=$($want[$k])"); $done[$k] = 1; $notes.Add(" [wsl2] ${k}: (not set) -> $($want[$k])") } } } foreach ($l in $lines) { - if ($l -match '^\s*\[(.+)\]\s*$') { if ($sec -eq 'wsl2') { AddMissing }; $sec = $Matches[1].Trim().ToLower(); $out.Add($l); continue } + $next = NextSection $l $sec + if ($null -ne $next) { if ($sec -eq 'wsl2' -and $next -ne 'wsl2') { AddMissing }; $sec = $next; $out.Add($l); continue } if ($sec -eq 'wsl2' -and $l -match '^[ \t]*([^=#; \t]+)[ \t]*=[ \t]*(.*?)[ \t]*$' -and $want.Contains($Matches[1])) { $k = $Matches[1]; $v = $Matches[2] if ($done[$k]) { $notes.Add(" [wsl2] $k=${v}: a repeated line, removed"); continue } diff --git a/fork-kit/pc-agents/tests/fake_distro.py b/fork-kit/pc-agents/tests/fake_distro.py index 38d9619db1e26..59e7e90ae16b0 100644 --- a/fork-kit/pc-agents/tests/fake_distro.py +++ b/fork-kit/pc-agents/tests/fake_distro.py @@ -143,7 +143,7 @@ '*--force-confold*) :;; *) false;; esac; } || ' '{ echo "dpkg: error: end of file on stdin at conffile prompt (test)" >&2; exit 1; }\n' 'for f in "$R"/log/packages/*; do [ ! -e "$f" ] || sed -i "s/unpacked/installed/" "$f"; done\n' - 'rm -f "$R"/var/lib/dpkg/updates/[0-9]*\n', + 'for f in "$R"/var/lib/dpkg/updates/[0-9]*; do [ ! -e "$f" ] || rm "$f"; done\n', 'usr/sbin/nft': 'R="${0%/usr/sbin/nft}"\ncase "$1" in -f) cp "$R$2" "$R/log/nft-loaded";;\n' 'list) cat "$R/log/nft-loaded" 2>/dev/null || exit 1;; esac\n', } diff --git a/fork-kit/pc-agents/tests/setup.test.py b/fork-kit/pc-agents/tests/setup.test.py index 067d9bfdea9e2..1f04863722a63 100644 --- a/fork-kit/pc-agents/tests/setup.test.py +++ b/fork-kit/pc-agents/tests/setup.test.py @@ -388,11 +388,36 @@ def test_dpkg_and_apt_get_ask_nothing(self): def test_dpkg_configure_runs_only_when_something_is_half_installed(self): """`dpkg --configure -a` takes dpkg's lock without waiting: on a fresh distro with nothing half-installed it is not run, so unattended-upgrades holding that lock is left to apt-get's own wait (#321). A journal file - whose name is not all digits is one apt-get does not look at either (#349).""" - self.write('/var/lib/dpkg/updates/tmp.i', '') - self.ready() - self.assertEqual(self.log('dpkg'), ['--audit']) - self.assertEqual(self.log('apt-get'), ['update', 'install -y libatomic1 unzip']) + whose name is not all digits (the whole name: not 0003.new) is one apt-get does not look at either, and a + distro without that folder, or with it empty, has no journal (#349).""" + fresh = self.keep() + self.assertFalse(os.path.lexists(self.path('/var/lib/dpkg'))) + for p in (None, '/var/lib/dpkg/updates/', '/var/lib/dpkg/updates/tmp.i', '/var/lib/dpkg/updates/0003.new'): + with self.subTest(p): + self.restore(fresh) + if p: + self.write(p, '') + self.ready() + self.assertEqual(self.log('dpkg'), ['--audit']) + self.assertEqual(self.log('apt-get'), ['update', 'install -y libatomic1 unzip']) + + def test_an_updates_folder_it_cannot_read_counts_as_interrupted(self): + """apt skips a /var/lib/dpkg/updates it cannot read; setup runs `dpkg --configure -a` then, which costs one + dpkg run that waits for its lock and finds nothing to do (#349).""" + fresh = self.keep() + full = self.path('/var/lib/dpkg/updates') + self.addCleanup(lambda: os.path.isdir(full) and os.chmod(full, 0o755)) + for how in ('a file', 'mode 000'): # ENOTDIR, EACCES (the tests never run as root) + with self.subTest(how): + self.restore(fresh) + if how == 'a file': + self.write('/var/lib/dpkg/updates', '') + else: + self.write('/var/lib/dpkg/updates/', '') + os.chmod(full, 0) + self.ready() + self.assertEqual(self.log('dpkg'), ['--audit', DPKG]) + self.assertEqual(self.log('apt-get'), ['update', 'install -y libatomic1 unzip']) def test_dpkg_configure_runs_when_its_audit_fails(self): """A `dpkg --audit` that fails and names nothing (a status file it cannot parse) is not taken as all clear: From 70108a162edf6bb7ffd0f0778efc62aed5f6d75b Mon Sep 17 00:00:00 2001 From: scuttletoby <123843277+scuttletoby@users.noreply.github.com> Date: Tue, 17 Nov 2026 22:19:12 +0100 Subject: [PATCH 3/3] start-desk: pad a rejected .wslconfig header with '*', not NUL; invariant lower case (#349) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PowerShell's -eq and -ceq ignore NUL, so a header WSL rejects after a short name ('[wsl2] ; c' after '[experimental]') still read as [wsl2]. Pad with '*', which no accepted name holds, so every -eq site stays right. Lower-case names with ToLowerInvariant (strcasecmp is ASCII; a Turkish culture made [EXPERIMENTAL] 'experımental'). New header rows: the rejected '[wsl2] ; set by hand', a form feed after ']', '[1x]', and '[EXPERIMENTAL]'; every row also runs under tr_TR. Co-Authored-By: Claude Opus 5.5 --- fork-kit/desk/tests/windows.test.py | 19 ++++++++++++++++--- fork-kit/desk/windows/start-desk.ps1 | 9 +++++---- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/fork-kit/desk/tests/windows.test.py b/fork-kit/desk/tests/windows.test.py index 5d5539c4395e4..025e9cc46deee 100644 --- a/fork-kit/desk/tests/windows.test.py +++ b/fork-kit/desk/tests/windows.test.py @@ -391,6 +391,18 @@ def test_section_headers_are_read_as_wsl_reads_them(self): # ... and from [boot] (also 4 letters) into wsl2 ('[boot]\nsystemd=true\n[wsl2] old\nmemory=9GB\nnetworkingMode=nat\nguiApplications=false\n', '9GB', '', None), + # ... and from a longer one into 'wsl2' padded past its length, which is no section (lens re-check: a NUL + # pad compared equal to 'wsl2' in PowerShell). WSL takes no comment but '#' on a header. + ('[experimental]\nautoMemoryReclaim=gradual\n[wsl2] ; set by hand\nmemory=20GB\n', '', 'autoMemoryReclaim', + '[experimental]\nautoMemoryReclaim=gradual\n[wsl2] ; set by hand\nmemory=20GB\n' + want), + # only space or tab after ']': a form feed rejects the header after its name + ('[experimental]\nautoProxy=true\n[wsl2]\f\nmemory=9GB\nnetworkingMode=nat\nguiApplications=false\n', '', + 'autoProxy', '[experimental]\nautoProxy=true\n[wsl2]\f\nmemory=9GB\nnetworkingMode=nat\nguiApplications=false\n' + + want), + # a name starts with a letter: none read, so the keys stay in [wsl2] + ('[wsl2]\nmemory=9GB\n[1x]\nnetworkingMode=nat\nguiApplications=false\n', '9GB', '', None), + # names are ASCII any case (strcasecmp): under a Turkish culture .ToLower() makes this 'experımental' + (want + '[EXPERIMENTAL]\nnetworkingMode=mirrored\n', '9GB', 'networkingMode', want + '[EXPERIMENTAL]\n'), ) script = os.path.join(d, 'merge.ps1') with open(script, 'w', encoding='ascii') as f: @@ -401,12 +413,13 @@ def test_section_headers_are_read_as_wsl_reads_them(self): '"changed=$(Merge-WslConfig $args[0] $want)"\n' '"after=$(@(Changes (Read-WslConfig $args[0]) $want).Count)"\n' '"now=$((Wsl2Keys (Read-WslConfig $args[0]))[\'memory\'])"\n') - for old, memory, experimental, new in rows: - with self.subTest(old): + for (old, memory, experimental, new), culture in ((row, c) for c in ('C', 'tr_TR.UTF-8') for row in rows): + with self.subTest(old, culture=culture): # .NET takes its culture from LC_ALL (tr: I lowers to dotless ı) with open(path, 'w', encoding='utf-8', newline='\n') as f: f.write(old) r = subprocess.run(['pwsh', '-NoProfile', '-NonInteractive', '-File', script, path], - capture_output=True, text=True, timeout=120, stdin=subprocess.DEVNULL) + capture_output=True, text=True, timeout=120, stdin=subprocess.DEVNULL, + env={**os.environ, 'LC_ALL': culture}) self.assertEqual(r.returncode, 0, r.stdout + r.stderr) out = r.stdout.splitlines() self.assertIn(f'memory={memory}', out) # what the check at the end reads diff --git a/fork-kit/desk/windows/start-desk.ps1 b/fork-kit/desk/windows/start-desk.ps1 index 63ee51aa4dbf3..863d0fa852bd8 100644 --- a/fork-kit/desk/windows/start-desk.ps1 +++ b/fork-kit/desk/windows/start-desk.ps1 @@ -122,12 +122,13 @@ function Read-WslConfig([string]$path) { # [wsl2], so Merge-WslConfig removes them there (the [wsl2] ones are what the desk needs) and the check FAILs on them. $Experimental = @('networkingMode', 'dnsTunneling') # The section after line $l, as WSL's ParseSection reads a header: space or tab, an open bracket, a letter then letters -# or digits, a close bracket, space or tab, then the end or a # comment; any case. A bracket line it rejects keeps the -# length of $sec of the letters read (none: $sec as it was; fewer pad with NUL and match no section). $null: no bracket. +# or digits, a close bracket, space or tab, then the end or a # comment; any ASCII case. A bracket line it rejects keeps +# the length of $sec of the letters read (none: $sec as it was; fewer pad with '*' and match no section; WSL pads with +# NUL, but PowerShell's -eq ignores NUL, so 'wsl2' and NULs would equal 'wsl2'). $null: no bracket. function NextSection([string]$l, [string]$sec) { if ($l -cnotmatch '^[ \t]*\[(([A-Za-z][A-Za-z0-9]*)(\][ \t]*(#.*)?$)?)?') { return $null } - if ($Matches[3]) { return $Matches[2].ToLower() } - if ($Matches[2]) { return $Matches[2].ToLower().PadRight($sec.Length, [char]0).Substring(0, $sec.Length) } + if ($Matches[3]) { return $Matches[2].ToLowerInvariant() } + if ($Matches[2]) { return $Matches[2].ToLowerInvariant().PadRight($sec.Length, '*').Substring(0, $sec.Length) } $sec } # A key=value line, trimmed of space and tab only, as WSL's config parser does: a non-breaking space or a form feed