Skip to content

Add some calls to addTempRoot()#527

Merged
edolstra merged 1 commit into
mainfrom
more-addTempRoot
Jun 30, 2026
Merged

Add some calls to addTempRoot()#527
edolstra merged 1 commit into
mainfrom
more-addTempRoot

Conversation

@edolstra

@edolstra edolstra commented Jun 30, 2026

Copy link
Copy Markdown
Collaborator

Motivation

Based on upstream 0b29401.

Context

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability when reading cached string values with context, reducing false cache invalidation in some cases.
    • Made path reuse during fetch and substitution more robust, helping avoid failures when resolving existing store content.

@coderabbitai

coderabbitai Bot commented Jun 30, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

This change adds garbage-collection root protection for store paths in two locations: AttrCursor::getStringWithContext in eval-cache.cc temp-roots resolved context paths before checking validity, and Input::getAccessorUnchecked in fetchers.cc temp-roots a substituted path before ensuring it exists.

Changes

Temp-root store paths before validity checks

Layer / File(s) Summary
Root cached context paths before validity check
src/libexpr/eval-cache.cc
Non-null resolved StorePath values are temp-rooted via addTempRoot before isValidPath is called; null pointers no longer invalidate the cached context.
Root substituted store path before ensurePath
src/libfetchers/fetchers.cc
Adds store.addTempRoot(*storePath) immediately before store.ensurePath(*storePath) in the substitution success path.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

A burrow once vanished, swept clean by the gc,
So this rabbit dug roots, deep and steady and free.
Now paths I resolve get a tug on the line,
Held fast before checking, "Yes, valid, you're mine!"
Hop on, little store path, no more disappearing for thee. 🐇🌱

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title matches the main change: adding addTempRoot() calls in cache and fetcher paths.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch more-addTempRoot

Comment @coderabbitai help to get the list of available commands.

@edolstra edolstra enabled auto-merge June 30, 2026 20:35

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/libfetchers/fetchers.cc`:
- Line 381: The fast-path in makeStorePathFromURI still bypasses the
temporary-root protection, so the store can be returned from makeStoreAccessor()
without rooting it. Update the makeStorePathFromURI flow to add the temp root
before the early storePath && store.isValidPath(*storePath) return path, keeping
the protection consistent with the substitution branch and preserving the
existing requireStoreObjectAccessor() / queryPathInfo() behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 63a8f6d0-d9f8-4c20-9aa8-7122e63461ca

📥 Commits

Reviewing files that changed from the base of the PR and between d89726d and c143382.

📒 Files selected for processing (2)
  • src/libexpr/eval-cache.cc
  • src/libfetchers/fetchers.cc

/* If not, try to substitute the input. */
if (storePath) {
try {
store.addTempRoot(*storePath);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Root storePath before the fast-path validity check too.

Line 381 protects only the substitution branch. The earlier storePath && store.isValidPath(*storePath) fast path can still return through makeStoreAccessor() without a temp root, leaving the same GC race before requireStoreObjectAccessor() / queryPathInfo().

🐛 Proposed fix
     std::optional<StorePath> storePath;
     if (isFinal() && getNarHash())
         storePath = computeStorePath(store);
+    bool storePathTempRooted = false;
+    auto addStorePathTempRoot = [&]() {
+        if (storePath && !storePathTempRooted) {
+            store.addTempRoot(*storePath);
+            storePathTempRooted = true;
+        }
+    };
 
     auto makeStoreAccessor = [&]() -> std::pair<ref<SourceAccessor>, Input> {
         auto accessor = store.requireStoreObjectAccessor(*storePath);
@@
-    if (storePath && store.isValidPath(*storePath)) {
-        debug("using input '%s' in '%s'", to_string(), store.printStorePath(*storePath));
-        return makeStoreAccessor();
+    if (storePath) {
+        addStorePathTempRoot();
+        if (store.isValidPath(*storePath)) {
+            debug("using input '%s' in '%s'", to_string(), store.printStorePath(*storePath));
+            return makeStoreAccessor();
+        }
     }
@@
     if (storePath) {
         try {
-            store.addTempRoot(*storePath);
+            addStorePathTempRoot();
             store.ensurePath(*storePath);
             return makeStoreAccessor();
         }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
store.addTempRoot(*storePath);
std::optional<StorePath> storePath;
if (isFinal() && getNarHash())
storePath = computeStorePath(store);
bool storePathTempRooted = false;
auto addStorePathTempRoot = [&]() {
if (storePath && !storePathTempRooted) {
store.addTempRoot(*storePath);
storePathTempRooted = true;
}
};
auto makeStoreAccessor = [&]() -> std::pair<ref<SourceAccessor>, Input> {
auto accessor = store.requireStoreObjectAccessor(*storePath);
@@
- if (storePath && store.isValidPath(*storePath)) {
- debug("using input '%s' in '%s'", to_string(), store.printStorePath(*storePath));
if (storePath) {
addStorePathTempRoot();
if (store.isValidPath(*storePath)) {
debug("using input '%s' in '%s'", to_string(), store.printStorePath(*storePath));
return makeStoreAccessor();
}
}
@@
if (storePath) {
try {
addStorePathTempRoot();
store.ensurePath(*storePath);
return makeStoreAccessor();
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/libfetchers/fetchers.cc` at line 381, The fast-path in
makeStorePathFromURI still bypasses the temporary-root protection, so the store
can be returned from makeStoreAccessor() without rooting it. Update the
makeStorePathFromURI flow to add the temp root before the early storePath &&
store.isValidPath(*storePath) return path, keeping the protection consistent
with the substitution branch and preserving the existing
requireStoreObjectAccessor() / queryPathInfo() behavior.

@github-actions

Copy link
Copy Markdown

@github-actions github-actions Bot temporarily deployed to pull request June 30, 2026 20:40 Inactive
@edolstra edolstra added this pull request to the merge queue Jun 30, 2026
Merged via the queue into main with commit f2fd616 Jun 30, 2026
33 checks passed
@edolstra edolstra deleted the more-addTempRoot branch June 30, 2026 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants