Concurrent chisels race on `<digest>.tmp`

Aperta
#340 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
1-2 giorni
Idoneità per principianti
55/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
go
Ambito
backend, devtools

Direzione di ricerca

Start by reading cache.go at the os.Create(c.filePath(digestKind, digest+".tmp")) call and trace how cache writes are closed, renamed, and removed on failure. Reproduce with two concurrent cuts sharing a cold cache using the supplied script. Done means concurrent fetches no longer interfere and cached blobs still match their digest.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

two chisel processes that miss the cache on the same blob at the same time write to the same temporary file, and one of them fails. the other may publish a torn file.

this happens since os.Create(c.filePath(digestKind, digest+".tmp")) in cache.go is a per-digest fixed name with no locks. with processes A and B fetching the same deb (or the same Packages index) into a cold cache:

  1. A creates <digest>.tmp and starts writing.
  2. B's os.Create truncates the same inode to zero. A keeps writing at its own offset, so the file has a hole of zeros at the front until B fills it.
  3. whoever closes first hashes its own stream (correct), renames the inode to <digest> and opens it. the other process may still be filling the hole, so the reader can see zeros where deb bytes should be; Cache.Open does not re-verify.
  4. the other process finishes and its rename of <digest>.tmp fails with no such file or directory, so the cut dies with cannot fetch from archive: rename ....

a variant: if B's download fails half way, fail removes <digest>.tmp from under A, and A's close fails the same way.

i've come across this when doing chisel-releases testing and cutting every slice of a release on every architecture. in order to make the cuts faster, arch: all debs and the per-suite indexes were shared by every worker and that's where i started getting races.

repro

two cuts at once on a cold shared cache, 20 times:

#!/usr/bin/env bash
set -e
work=$(mktemp -d)
git clone -q --depth 1 -b ubuntu-24.04 https://github.com/canonical/chisel-releases "$work/chisel-releases"

for run in $(seq 20); do
  rm -rf "$work/cache" "$work"/root-* && mkdir -p "$work/root-1" "$work/root-2"
  for i in 1 2; do
    XDG_CACHE_HOME="$work/cache" chisel cut \
      --release "$work/chisel-releases" \
      --root "$work/root-$i" \
      --arch amd64 base-files_base > "$work/out-$i" 2>&1 \
      || echo "run $run job $i: $(grep -m1 '^error:' "$work/out-$i")" &
    done
    wait  # wait for both cuts
    for f in "$work"/cache/chisel/sha256/[0-9a-f]*; do
        [ "$(sha256sum "$f" | cut -d' ' -f1)" = "$(basename "$f")" ] \
        || echo "run $run: torn blob $(basename "$f")"
    done
done

obv a bit random but in the run i've done just now 13 of the 20 runs lost one of the two cuts, every time on a Packages index (these the biggest blobs, so the race window is longest):

run 1 job 1: error: cannot fetch from archive: rename .../8f6f71ae...a30e5.tmp .../8f6f71ae...a30e5: no such file or directory
...

proposal

file, err = os.CreateTemp(c.filePath(digestKind, ""), digest+".tmp.*")

or a per-digest fslock which is what fetch does.

Lingua principale
Go
Stelle
434
Fork
74
Merge medio
1g 30m
PR unite (30g)
1

Preparare l'ambiente

  • Nessun Dockerfile né file Docker Compose
  • Ha un modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di canonical/chisel

Tutte le issue di canonical/chisel

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.