Test failures under high load due to false lock release
まだ誰も着手していません。
評価
調査の方向性
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 を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
dtolnay/trybuild のほかの issue
-
難易度 3/5 1〜2日 初心者へのやさしさ 55/100
-
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 50/100
-
難易度 4/5 3〜5日 初心者へのやさしさ 42/100
-
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
dtolnay/trybuild の issue をすべて見る
似ている issue
-
priority: low
難易度 2/5 1〜3時間 初心者へのやさしさ 67/100
storytold/photocraft#15350 ·
メンテナーはふだん 1 日以内に返信
-
area:davinci
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
ubugeeei-prod/vize#13167 ·
メンテナーはふだん 1 日以内に返信
-
area:ui enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
transit-audio/transit#738 ·
メンテナーはふだん 1 日以内に返信
-
enhancement good first issue help wanted
難易度 2/5 1〜3時間 初心者へのやさしさ 91/100
hocinehacherouf/liberarr#78 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 1 日以内に返信