Test failures under high load due to false lock release

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

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

評価

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

調査の方向性

The lock logic lives in flock.rs: a heartbeat thread bumps the .lock mtime every 500 ms, waiters treat locks older than 1.5 s as stale and take them over with File::create, and the holder's Drop deletes the file. Start by reading that code, then run the bash reproducer in the issue, which SIGSTOPs one test process and checks whether the other's Cargo.toml is used. Done means the reproducer no longer prints REPRODUCED. The fix direction (OS advisory locks via File::lock, Rust 1.89+) is a design decision the maintainer must confirm first.

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

説明

version

trybuild 1.0.122

OS

Linux

Problem

One of my projects encounters occasional failures in tests using trybuild when run on CI using nextest. The CI machine can be under very high CPU loads (100%). I debugged the problem with the help of LLMs and traced the root cause to trybuild's flock code:

All trybuild runs of one crate share target/tests/trybuild/<crate>/. Between processes, only the .lock file in flock.rs protects it. While a run holds the lock, a heartbeat thread bumps the file's mtime every 500 ms. A waiting process treats a lock older than 1.5 s as stale and takes it over with File::create.

-> On a highly loaded machine with many processes, the scheduler may delay the process for more than this time, which causes a false stale lock release and thus a race where one test steals the other's test directory. This then causes test failures.

Two related issues:

  • After a takeover, the original holder's Drop deletes the lock file the new holder uses, so a third process can enter.
  • The stale check and File::create are not atomic, so two waiters can take over at the same time.

How to reproduce

# Cargo.toml
[package]
name = "trybuild-race"
version = "0.0.0"
edition = "2021"
publish = false

[dev-dependencies]
trybuild = "=1.0.122"

[workspace]
// tests/a.rs
#[test]
fn ui_a() {
    trybuild::TestCases::new().compile_fail("tests/ui/a/*.rs");
}
// tests/b.rs
#[test]
fn ui_b() {
    trybuild::TestCases::new().compile_fail("tests/ui/b/*.rs");
}
// tests/ui/a/mismatched_types.rs 
fn main() { let _: u32 = "a"; }
// tests/ui/a/mismatched_types.stderr 
error[E0308]: mismatched types
 --> tests/ui/a/mismatched_types.rs:1:26
  |
1 | fn main() { let _: u32 = "a"; }
  |                    ---   ^^^ expected `u32`, found `&str`
  |                    |
  |                    expected due to this
// tests/ui/b/undefined.rs 
fn main() { undefined(); }
// tests/ui/b/undefined.stderr 
error[E0425]: cannot find function `undefined` in this scope
 --> tests/ui/b/undefined.rs:1:13
  |
1 | fn main() { undefined(); }
  |             ^^^^^^^^^ not found in this scope

Run each test in isolation:

# cargo test --test a
[...]
test tests/ui/a/mismatched_types.rs ... ok
test ui_a ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.12s

# cargo test --test b
  [...]
test tests/ui/b/undefined.rs ... ok
test ui_b ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.12s

Now simulate a stall of of two independent test processes (cargo nextest does this by default - this reproducer uses two independent cargo test invocations)

#!/usr/bin/env bash
# Reproduces two trybuild runs of the same crate overwriting each other's project.
#
# Test binaries `a` and `b` both run compile_fail tests, so they share
# target/tests/trybuild/trybuild-race/ and its `.lock` file.
# The script freezes `a` (SIGSTOP) while it holds the lock. This stands in for
# its heartbeat thread not being scheduled for >1.5 s on a loaded machine.
# `b` then treats the lock as stale and takes it over. When `a` resumes, it
# checks `b`'s Cargo.toml, finds no diagnostics for its own test case and
# reports "Expected test case to fail to compile, but it succeeded".
set -euo pipefail
set -m # job control: each background job gets its own process group
cd "$(dirname "$0")"

project=target/tests/trybuild/trybuild-race
cargo test --no-run --quiet
rm -rf "$project"

wait_for() { until grep -qs "$1" "$project/Cargo.toml"; do sleep 0.01; done; }

cargo test --quiet --test a >a.log 2>&1 &
a=$!
wait_for 'tests/ui/a/' # `a` holds the lock and has written its Cargo.toml
kill -STOP -- -"$a"

cargo test --quiet --test b >b.log 2>&1 &
b=$!
wait_for 'tests/ui/b/' # `b` took over the "stale" lock and overwrote Cargo.toml
kill -CONT -- -"$a"

status=0
wait "$a" || status=$?
wait "$b" || true
echo "===== test a (exit $status) ====="
cat a.log
echo "===== test b ====="
cat b.log
echo "====="
if grep -q 'Expected test case to fail to compile, but it succeeded' a.log; then
  echo "REPRODUCED: test a judged by test b's Cargo.toml"
else
  echo "not reproduced"
fi

Possible fixes

Rust 1.89 stabilized OS advisory locks (e.g.std::fs::File::lock) which may allow this to be simpler & more robust.

主要言語
Rust
スター
1k
フォーク
87
PR マージ指標
30日以内にマージされた PR はありません

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

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

dtolnay/trybuild のほかの issue

dtolnay/trybuild の issue をすべて見る

似ている issue

Rust の issue をもっと見る

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

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