From 77ab7c0d9686250bd828d2e1bae49157b2a00cac Mon Sep 17 00:00:00 2001 From: Ed Jeffreys Date: Wed, 11 Nov 2026 15:21:07 +0000 Subject: [PATCH] fix(completion): compile the dumpfile's .zwc atomically - zrecompile writes the .zwc in place, so shells starting together read each other's half-written file: "zrecompile:zcompile:99 invalid zwc file" - compile to a per-shell temp .zwc and rename it over, like compdump does - test the compile, the stale recompile, and 12 shells racing it Co-Authored-By: Claude Opus 5.5 --- plugins/completion/completion.plugin.zsh | 17 ++++++-- tests/bats/completion.bats | 55 ++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 3 deletions(-) diff --git a/plugins/completion/completion.plugin.zsh b/plugins/completion/completion.plugin.zsh index abb327a..da23b2c 100644 --- a/plugins/completion/completion.plugin.zsh +++ b/plugins/completion/completion.plugin.zsh @@ -135,9 +135,20 @@ function run_compinit { zephyr-compaudit-warn &! fi - # Recompiles only if stale, and renames atomically, so concurrent shells are safe. - autoload -Uz zrecompile - zrecompile -q -p "$ZSH_COMPDUMP" &! + # Compile the dumpfile in the background, only when the .zwc is stale. Not with + # zrecompile: it writes the .zwc in place, so shells starting together (restored + # terminal sessions) read each other's half-written file and print "invalid zwc + # file". Compiling to a file of this shell's own and renaming it over means a + # reader sees the old .zwc or the new one, never part of one. compdump already + # writes the dumpfile itself that way. + { + local zwc=$ZSH_COMPDUMP.zwc tmp=$ZSH_COMPDUMP.$$.zwc + if [[ -s $ZSH_COMPDUMP && ( ! -s $zwc || $ZSH_COMPDUMP -nt $zwc ) ]]; then + zcompile -R -- $tmp $ZSH_COMPDUMP 2>/dev/null && + command mv -f -- $tmp $zwc || + command rm -f -- $tmp + fi + } &! } # Let's talk about compinit for a second... diff --git a/tests/bats/completion.bats b/tests/bats/completion.bats index 7240c27..0d98419 100644 --- a/tests/bats/completion.bats +++ b/tests/bats/completion.bats @@ -261,6 +261,61 @@ EOS assert_line "rebuilt: yes" } +# The .zwc is compiled by a disowned job, so these wait on a pipe instead: it only +# closes once every shell and every background job holding its write end has exited. +@test "the dumpfile is compiled to a .zwc, and recompiled once it is stale" { + write_file "$TEST_HOME/warm.zsh" \ + 'zstyle ":zephyr:plugin:completion" use-cache yes' \ + 'source $ZEPHYR_HOME/lib/bootstrap.zsh' \ + 'source $ZEPHYR_HOME/plugins/completion/completion.plugin.zsh' \ + 'run_compinit' + zephyr_plugin completion <<'EOS' +dump=$XDG_CACHE_HOME/zsh/zcompdump +zsh -f $HOME/warm.zsh | cat +print "compiled: $(zcompile -t $dump.zwc | head -1)" + +# Back-date rather than touch the dumpfile: -nt is only second-accurate on some +# systems, so a dumpfile changed the same second it was compiled looks up to date. +touch -t 200001010000 $dump.zwc +touch -t 200001010001 $HOME/stale +zsh -f $HOME/warm.zsh | cat +print "recompiled: $(zcompile -t $dump.zwc | head -1)" +print "fresh: $([[ $dump.zwc -nt $HOME/stale ]] && print yes || print no)" +print "leftovers: ${#${(@f)$(print -l $dump*.zwc(N) $dump.zwc.old(N))}:#$dump.zwc}" +EOS + assert_success + assert_line "compiled: zwc file (read) for zsh-$(zsh -fc 'print $ZSH_VERSION')" + assert_line "recompiled: zwc file (read) for zsh-$(zsh -fc 'print $ZSH_VERSION')" + assert_line "fresh: yes" + assert_line "leftovers: 0" +} + +# Restoring a terminal session starts many shells at once. When the .zwc was written +# in place, each read the others' half-written file: "invalid zwc file". +@test "shells starting together never see a half-written .zwc" { + write_file "$TEST_HOME/warm.zsh" \ + 'zstyle ":zephyr:plugin:completion" use-cache yes' \ + 'source $ZEPHYR_HOME/lib/bootstrap.zsh' \ + 'source $ZEPHYR_HOME/plugins/completion/completion.plugin.zsh' \ + 'run_compinit' + zephyr_plugin completion <<'EOS' +dump=$XDG_CACHE_HOME/zsh/zcompdump +zsh -f $HOME/warm.zsh | cat +for round in {1..10}; do + touch -t 200001010000 $dump.zwc + { for i in {1..12}; do zsh -f $HOME/warm.zsh & done; wait } 2>&1 | cat +done +print "valid: $(zcompile -t $dump.zwc | head -1)" +print "leftovers: ${#${(@f)$(print -l $dump*.zwc(N) $dump.zwc.old(N))}:#$dump.zwc}" +EOS + assert_success + refute_output_contains "invalid zwc" + refute_output_contains "can't open zwc" + refute_output_contains "mv:" + assert_line "valid: zwc file (read) for zsh-$(zsh -fc 'print $ZSH_VERSION')" + assert_line "leftovers: 0" +} + @test "run_compinit -f forces a rebuild" { zephyr_plugin completion <<'EOS' zstyle ':zephyr:plugin:completion' use-cache yes