Skip to content

Commit 19d3272

Browse files
committed
index: a refresh beside another process waits while an index the home reads has no packages, judged as a reader judges it (mcpp#927)
1 parent 8c44dac commit 19d3272

6 files changed

Lines changed: 126 additions & 11 deletions

File tree

‎.agents/docs/2026-11-12-ecosystem-architecture-optimization-design.md‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1148,14 +1148,21 @@ warning: the configuration changed (debug 1 → lines): 7a72… is built in full
11481148
|---|---|---|
11491149
| P1 | `programs = "package"`:一个包一个程序,以第一个 glob 的固定目录命名,定义 `main` 的文件至多一个;按文件运行的各部分以文件在程序目录(其文件的公共目录)之下的路径命名,不同子目录中的同名文件因此不冲突(目录模式下即文件名,与此前相同);`"directory"` 留下固定目录中的文件时给出 note,不形成任何组时为 `degraded`(`--strict` 拒绝) | mcpp `test_targets.cppm`;e2e 的 C++ 用例 |
11501150
| P2 的汇总 | `finished in X (plan · programs · build · run)`:plan 为总时长减去其余各段,programs 不足 5 ms 时不列 | mcpp `execute.cppm`;`tests/e2e/test/streams.cpp` |
1151-
| P3 | 拿不到刷新锁、且所声明的索引目录有缺失的进程,等待刷新锁(显示 `Waiting`)后再读;索引完整时仍跳过刷新、立即读取 | mcpp `xlings.cpp` |
1151+
| P3 | 拿不到刷新锁、且 home 读取的索引有缺失的进程,等待刷新锁(显示 `Waiting`)后再读;索引完整时仍跳过刷新、立即读取 | mcpp `xlings.cpp`;`tests/unit/test_xlings.cpp`(`XlingsIndexLock`,2026-11-22 补) |
11521152
| P4 | 文档:`docs/08-testing.md` 的"测试构建的体积"一节(中英) | — |
11531153
| P6 | 警告给出旧目录的大小与回收命令 | mcpp `execute.cppm` |
11541154

11551155
**P3 的范围比 18.2 小。** 18.2 的方案是读取持共享锁、刷新在暂存目录中进行后交换。#927 报告的现象只在"索引目录不存在"时出现:
11561156
索引存在时,刷新以交换替换整棵树,读取者读到的是交换前或交换后的一棵完整的树。因此实施只关闭这一个窗口,读路径不加锁。
11571157
不支持原子交换的文件系统上,"移开、改名"之间的窗口与目录不存在是同一情形,读取者同样等待。
11581158

1159+
**P3 的缺失判断(2026-11-22 补)。** 初版只判断 home 声明的索引,且以目录是否存在为准;读取者报告没有本地索引的依据
1160+
却是 `<index>/pkgs`(`index_status_for`)。两者不一致留下两个窗口:provider 自己的索引(`xim`)第一次取回时不在判断之内,
1161+
另一进程已建立而未写入的声明目录被判为存在。缺失的判断改为与读取者同一个函数(`index_present`),范围为 provider 自己的
1162+
索引与声明的索引(`every_index_present`);索引状态、刷新标记与新鲜度的判断也改用它。单元测试在本进程中持有索引锁(flock
1163+
与 LockFileEx 锁的是打开的文件,同一进程的第二个持有者同样被拒绝),断言三种情形:索引完整时立即返回;声明的索引没有包、
1164+
或 provider 自己的索引缺失时等待,锁释放后返回。
1165+
11591166
**P2 的另两项不在本版。** `-p` 的预构建取快速路径、同一次调用内复用解析:二者改变 `prepare_build` 的入口契约(解析结果的生命周期
11601167
与键),在本轮之外;当前代价是 `-p` 多一次完整规划。
11611168

‎CHANGELOG.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44
> Each `## [<version>]` section is that release's notes. Entries are written in English
55
> from 2026.9.28.3 on; earlier entries remain as written.
66

7+
## [Unreleased]
8+
9+
### Fixed
10+
11+
- A process that finds another refreshing the package index waits for it when an index the
12+
home reads has no packages yet, and judges that as a reader does (`<index>/pkgs`): the
13+
provider's own index (`xim`) is one of them, and a directory the other process has made and
14+
not yet filled is absent. Before, only the declared directories were judged, by existence,
15+
so a reader beside a first fetch skipped the refresh and reported no local package index
16+
(mcpp#927, design 2026-11-12 §18 P3).
17+
718
## [2026.11.21.1] - 2026-11-21
819

920
### Added

‎docs/05-dependencies.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,13 @@ after any failed refresh. An install through xlings is stopped when xlings
478478
writes nothing, not even its heartbeat, for 300 seconds. Terminating mcpp
479479
terminates the xlings process it started.
480480

481+
Processes that share a home refresh the index once. A process that finds another
482+
refreshing it reads the local index at once, unless an index the home reads (the
483+
provider's own, `xim`, or one the home declares) has no packages yet; it then
484+
waits for that refresh and reads what it fetched, instead of reporting no local
485+
package index *(2026.11.13.1+; the provider's own index and a directory not yet
486+
filled, @NEXT@+, mcpp#927)*.
487+
481488
Run any command with `-v` to see the decision for each dependency and why.
482489

483490
### Offline builds and mcpp fetch (2026.11.21.1+)

‎docs/zh/05-dependencies.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,11 @@ mcpp index status # local state: state, age and revision
400400
失败之后一样,继续使用本地索引。经由 xlings 的安装,在 xlings 连续 300 秒
401401
没有任何输出(包括心跳)时被终止。终止 mcpp 会一并终止它启动的 xlings 进程。
402402

403+
共用一个 home 的进程只刷新一次索引。发现另一个进程正在刷新的进程立即读取本地
404+
索引,除非 home 读取的某个索引(provider 自己的 `xim`,或 home 声明的)还没有
405+
任何包;这时它等待那次刷新结束,读取其取回的内容,而不是报告没有本地索引
406+
(2026.11.13.1 起;provider 自己的索引与尚未写入的目录,@NEXT@ 起,mcpp#927)。
407+
403408
对任意命令加 `-v`,可以看到每个依赖的判定结果与原因。
404409

405410
### 离线构建与 mcpp fetch(2026.11.21.1 起)

‎src/provider/xlings/xlings.cpp‎

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ std::filesystem::path index_pkgs_dir(const std::filesystem::path& indexDir) {
7777
return indexDir / "pkgs";
7878
}
7979

80+
// AN INDEX IS PRESENT WHEN ITS PACKAGES ARE. A reader that finds none reports no local package
81+
// index (`index_status_for`), and a refresh beside another process waits for it
82+
// (`every_index_present`): one judgement, so that a directory another process has made and not
83+
// yet filled is absent to both (mcpp#927).
84+
bool index_present(const std::filesystem::path& indexDir) {
85+
std::error_code ec;
86+
return std::filesystem::exists(index_pkgs_dir(indexDir), ec) && !ec;
87+
}
88+
8089
std::filesystem::path index_refresh_marker(const std::filesystem::path& indexDir) {
8190
return indexDir / ".mcpp-index-updated";
8291
}
@@ -119,7 +128,7 @@ bool official_index_cache_matches_package_file(const Env& env,
119128
}
120129

121130
void mark_index_refreshed(const std::filesystem::path& indexDir) {
122-
if (!std::filesystem::exists(index_pkgs_dir(indexDir))) return;
131+
if (!index_present(indexDir)) return;
123132
std::error_code ec;
124133
std::filesystem::create_directories(indexDir, ec);
125134
auto marker = index_refresh_marker(indexDir);
@@ -145,7 +154,7 @@ void mark_known_indexes_refreshed(const Env& env) {
145154

146155
bool is_index_dir_fresh(const std::filesystem::path& indexDir, std::int64_t ttlSeconds) {
147156
std::error_code ec;
148-
if (!std::filesystem::exists(index_pkgs_dir(indexDir))) return false;
157+
if (!index_present(indexDir)) return false;
149158

150159
auto marker = index_refresh_marker(indexDir);
151160
if (!std::filesystem::exists(marker)) return false;
@@ -197,11 +206,9 @@ std::optional<std::string> read_index_revision(const std::filesystem::path& inde
197206

198207
IndexStatus index_status_for(const std::filesystem::path& indexDir,
199208
std::int64_t ttlSeconds) {
200-
std::error_code ec;
201-
bool present = std::filesystem::exists(index_pkgs_dir(indexDir), ec) && !ec;
202209
return IndexStatus{
203210
.dir = indexDir,
204-
.present = present,
211+
.present = index_present(indexDir),
205212
.fresh = is_index_dir_fresh(indexDir, ttlSeconds),
206213
.ageSeconds = index_age_seconds(indexDir),
207214
.rev = read_index_revision(indexDir),
@@ -2198,6 +2205,13 @@ int update_index(const Env& env, bool quiet) {
21982205
}
21992206

22002207
namespace {
2208+
// Every index a reader of this home reads, as it judges one: the provider's own and each declared.
2209+
bool every_index_present(const Env& env) {
2210+
return index_present(official_index_dir(env))
2211+
&& std::ranges::all_of(paths::declared_indices(env),
2212+
[](const auto& d) { return index_present(d.dir); });
2213+
}
2214+
22012215
int update_index_unguarded(const Env& env, bool quiet) {
22022216
// Offline is absolute: no caller gets to reach the network by going around
22032217
// the decision layer. Reported as success so a build that can still resolve
@@ -2226,11 +2240,12 @@ int update_index_unguarded(const Env& env, bool quiet) {
22262240
// was still fetching, or exchanging on a volume without an atomic
22272241
// exchange, and reported "no local package index" for a home that had
22282242
// one a moment later. Such a reader waits for the refresh it skipped;
2229-
// one with an index skips as before.
2230-
bool absent = false;
2231-
for (const auto& d : paths::declared_indices(env))
2232-
if (!std::filesystem::exists(d.dir, lockEc)) absent = true;
2233-
if (!absent) {
2243+
// one with every index skips as before. Every index is the provider's
2244+
// own (xim) and each the home declares, judged as a reader judges one
2245+
// (`index_present`): the declared directories alone, by existence,
2246+
// let a reader skip beside a first fetch of xim, and beside a declared
2247+
// directory made and not yet filled.
2248+
if (every_index_present(env)) {
22342249
mcpp::log::verbose("index",
22352250
"another process is refreshing the index — skipping this one");
22362251
return 0;

‎tests/unit/test_xlings.cpp‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import mcpplibs.mtest;
44
import mcpplibs.os.platform;
55
import mcpp.provider.xlings;
66
import mcpp.invocation;
7+
import mcpp.effects;
8+
import mcpp.home.files;
79

810
using namespace mcpplibs::mtest;
911

@@ -839,3 +841,71 @@ UTest a_projects_install_takes_the_homes_turn{"XlingsState/AProjectsInstallTakes
839841
std::filesystem::remove_all(home);
840842
std::filesystem::remove_all(project);
841843
}};
844+
845+
// ─── A refresh beside another process (mcpp#927, design 2026-11-12 §18 P3) ───
846+
//
847+
// The test holds the index lock as another process would. A second holder in this process is
848+
// refused as one in another would be (flock and LockFileEx lock an open file, not a process), so
849+
// the refresh below never reaches a sync: it skips, or it waits for this lock.
850+
namespace {
851+
852+
// A home whose provider's index (xim) and one declared index (`mcpplibs`) are laid out as a reader
853+
// finds them: `<index>/pkgs`.
854+
std::filesystem::path home_with_indices() {
855+
auto home = make_tempdir("mcpp-xlings-index-lock");
856+
std::ofstream(home / ".xlings.json")
857+
<< R"({"index_repos": [{"name": "mcpplibs", "url": "https://example.invalid/index.git"}]})";
858+
std::filesystem::create_directories(home / "data" / "xim-pkgindex" / "pkgs");
859+
std::filesystem::create_directories(home / "data" / "mcpplibs" / "pkgs");
860+
return home;
861+
}
862+
863+
// Whether a refresh started while this process holds the home's index lock waited for it: it had
864+
// not returned after a second and a half. `fill` then completes the home, as the other process's
865+
// fetch would, and the lock is released; the refresh returns 0 either way.
866+
bool refresh_waited_beside_another(const std::filesystem::path& home, const std::function<void()>& fill) {
867+
std::future<int> refresh; // declared first, so that it ends after the lock is released
868+
auto held = mcpp::home::FileLock::try_acquire(home / "data");
869+
expect(fatal(held.has_value())) << "the index lock could not be taken";
870+
expect(fatal(!mcpp::home::FileLock::try_acquire(home / "data").has_value()))
871+
<< "a second holder in this process was not refused: the refresh would run a sync";
872+
const mcpp::provider::xlings::Env env{.home = home};
873+
refresh = std::async(std::launch::async, [env] { return mcpp::provider::xlings::update_index(env, true); });
874+
const bool waited = refresh.wait_for(std::chrono::milliseconds{1500}) == std::future_status::timeout;
875+
fill();
876+
held.reset();
877+
expect(fatal(refresh.wait_for(std::chrono::seconds{30}) == std::future_status::ready))
878+
<< "the refresh did not return once the lock was released";
879+
expect(eq(refresh.get(), 0));
880+
return waited;
881+
}
882+
883+
} // namespace
884+
885+
UTest skips_beside_another_when_every_index_is_present{"XlingsIndexLock/SkipsBesideAnotherWhenEveryIndexIsPresent", [] {
886+
if (mcpp::effects::offline_mode()) skip("offline: a refresh returns before the lock");
887+
auto home = home_with_indices();
888+
expect(!refresh_waited_beside_another(home, [] {})) << "a refresh waited although every index was in place";
889+
std::filesystem::remove_all(home);
890+
}};
891+
892+
// The window #927 reported: a declared index's directory made by the other process's fetch and not
893+
// yet filled is absent to a reader, so it is absent to the refresh that would skip.
894+
UTest waits_beside_another_for_an_index_not_yet_filled{"XlingsIndexLock/WaitsBesideAnotherForAnIndexNotYetFilled", [] {
895+
if (mcpp::effects::offline_mode()) skip("offline: a refresh returns before the lock");
896+
auto home = home_with_indices();
897+
std::filesystem::remove_all(home / "data" / "mcpplibs" / "pkgs");
898+
expect(refresh_waited_beside_another(home, [&] { std::filesystem::create_directories(home / "data" / "mcpplibs" / "pkgs"); }))
899+
<< "a refresh skipped beside another while a declared index had no packages";
900+
std::filesystem::remove_all(home);
901+
}};
902+
903+
// The provider's own index is read as a declared one is: a first fetch of it is waited for too.
904+
UTest waits_beside_another_for_the_providers_own_index{"XlingsIndexLock/WaitsBesideAnotherForTheProvidersOwnIndex", [] {
905+
if (mcpp::effects::offline_mode()) skip("offline: a refresh returns before the lock");
906+
auto home = home_with_indices();
907+
std::filesystem::remove_all(home / "data" / "xim-pkgindex");
908+
expect(refresh_waited_beside_another(home, [&] { std::filesystem::create_directories(home / "data" / "xim-pkgindex" / "pkgs"); }))
909+
<< "a refresh skipped beside another while the provider's own index was absent";
910+
std::filesystem::remove_all(home);
911+
}};

0 commit comments

Comments
 (0)