Concurrent chisels race on `<digest>.tmp`

オープン
#340 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
1〜2日
初心者へのやさしさ
55/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
go
領域
backend, devtools

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Go
スター
434
フォーク
74
平均マージ
1日 30分
マージ済み PR(30日)
1

環境構築

  • Dockerfile・Docker Compose ファイルなし
  • プルリクエストのテンプレートあり
  • コントリビューションガイドなし

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

canonical/chisel のほかの issue

canonical/chisel の issue をすべて見る

似ている issue

Go の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。