diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-install b/tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-install index 7da018df54..8990abf3dc 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-install +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-install @@ -178,22 +178,42 @@ unzip_alone() { return $rc } +# Takes the next unclaimed line number from a counter under a flock, and prints 0 when none is left. mkdir was tried for this +# and handed the same line to two workers now and then in the app's guest shell, so only the kernel's lock is trusted here. +claim_next() { + local n + while :; do + # A counter that cannot be read or written must fail the worker: a stale or empty number would give a package to two workers or to none. + n=$( { flock 8 || exit 1 + n=$(cat "$state/next") || exit 1 + case $n in ''|*[!0-9]*) exit 1 ;; esac + echo $((n + 1)) > "$state/next" || exit 1 + echo "$n"; } 8> "$state/claim.lock" ) || return 1 + [ "$n" -le "$count" ] || { echo 0; return; } + if [ ! -e "$state/$n.done" ]; then + : > "$state/claim.$n" + echo "$n" + return + fi + done +} + # Fetches every line of a list ("block file sum size") into $dir with up to $parallel connections. With a third # argument each package is also unpacked into the client as it lands. One connection tops out near half of what a # Wi-Fi link carries, so the packages arrive about twice as fast. Workers take the next unclaimed line themselves -# (mkdir is atomic) and only plain background jobs and wait on a pid are used, so nothing depends on job control. +# (claim_next) and only plain background jobs and wait on a pid are used, so nothing depends on job control. run_parallel() { local list=$1 dir=$2 unpack=${3:-} state index=0 block file sum size local -a blocks=() files=() sums=() sizes=() pids=() state=$work/parallel rm -rf "$state" mkdir -p "$state" + echo 1 > "$state/next" while read -r block file sum size; do index=$((index + 1)) blocks[index]=$block files[index]=$file sums[index]=$sum sizes[index]=$size if [ -n "$unpack" ] && [ -f "$dir/$file.unpacked" ]; then : > "$state/$index.done" - : > "$state/claim.$index" fi done < "$list" local count=$index total_bytes=0 @@ -203,10 +223,7 @@ run_parallel() { ( trap ': > "$state/exit.$slot"' EXIT while [ ! -e "$state/failed" ]; do - index=0 - for n in $(seq 1 "$count"); do - if mkdir "$state/claim.$n" 2>/dev/null; then index=$n; break; fi - done + index=$(claim_next) [ "$index" -gt 0 ] || exit 0 file=${files[index]} fetched="" diff --git a/tools/tests/test_steam_install.py b/tools/tests/test_steam_install.py index 2a74968c00..3364729efb 100644 --- a/tools/tests/test_steam_install.py +++ b/tools/tests/test_steam_install.py @@ -613,6 +613,46 @@ with open(log, 'a') as out: self.assertIn('downloading Steam: a_all (1/2), 0 of 2 MB\n', result.stdout) self.assertIn('downloading Steam: client_linuxarm64_linuxarm64 (2/2), 1 of 2 MB\n', result.stdout) + def test_workers_do_not_claim_packages_with_mkdir(self): + source = SCRIPT.read_text() + region = source[source.index('run_parallel() {'):source.index('fetch_manifest() {')] + self.assertNotIn('mkdir "$state/claim', region) + self.assertIn('claim_next', region) + + @unittest.skipUnless(shutil.which('flock'), 'needs flock') + def test_racing_workers_never_get_the_same_package(self): + source = SCRIPT.read_text() + start = source.index('claim_next() {') + function = source[start:source.index('\n}\n', start) + 3] + state = self.root / 'claims' + state.mkdir() + (state / 'next').write_text('1\n') + script = 'state=$1\ncount=$2\n' + function + 'while :; do n=$(claim_next); [ "$n" -gt 0 ] || exit 0; echo "$n"; done\n' + workers = [subprocess.Popen(['bash', '-c', script, 'bash', str(state), '300'], stdout=subprocess.PIPE, text=True) + for _ in range(8)] + claimed = [] + for worker in workers: + out, _ = worker.communicate(timeout=120) + self.assertEqual(0, worker.returncode) + claimed += [int(n) for n in out.split()] + self.assertEqual(sorted(claimed), list(range(1, 301)), 'a package was claimed twice or never') + + @unittest.skipUnless(shutil.which('flock'), 'needs flock') + def test_a_counter_that_cannot_be_used_fails_the_claim(self): + source = SCRIPT.read_text() + start = source.index('claim_next() {') + function = source[start:source.index('\n}\n', start) + 3] + for broken in ('directory', 'empty', 'text'): + state = self.root / f'claims-{broken}' + state.mkdir() + if broken == 'directory': + (state / 'next').mkdir() + else: + (state / 'next').write_text('' if broken == 'empty' else 'abc\n') + script = 'state=$1\ncount=5\n' + function + 'n=$(claim_next) && echo "claimed $n" || echo failed\n' + result = subprocess.run(['bash', '-c', script, 'bash', str(state)], text=True, capture_output=True, timeout=30) + self.assertEqual('failed', result.stdout.strip(), f'{broken}: {result.stdout!r} {result.stderr!r}') + def test_the_parallel_workers_use_no_process_substitution(self): source = SCRIPT.read_text() region = source[source.index('fetch_one() {'):source.index('fetch_manifest() {')]