← Back to Pull requests

fix TOCTOU race when reading xcbuilddata bundle #3295

Closed
unknown closed this pull request without merging on Apr 29, 2026 19:57 UTC
View on GitHub
unknown unknown commented on Mar 27, 2026 04:30 UTC
Author

Summary

Fixes a race condition in calculate_output_groups.py that causes intermittent build failures when Xcode deletes .xcbuilddata bundles concurrently with the pre-build script.

Problem

The wait_for_build_request() function uses glob.iglob() (a lazy generator) with os.path.getctime() as the key for max(). Because the generator yields paths lazily, there's a window between when a path is yielded and when getctime() is called during which Xcode's build system can delete or replace the .xcbuilddata bundle. This results in a FileNotFoundError that bypasses the _wait_for_value() retry loop and fails the build.

This particularly affects CI environments where DerivedData is reused across builds—stale .xcbuilddata bundles from previous runs are present when a new build starts, and Xcode begins replacing them concurrently with the pre-build script.

Example failure:

Build description signature: 8999db0ef50cb36294751fc27f4dc82e
Build description path: /Users/ec2-user/Library/Developer/Xcode/DerivedData/MyApp-bqspktvzncwdrrfbzlfbtjjpxguv/Build/Intermediates.noindex/XCBuildData/8999db0ef50cb36294751fc27f4dc82e.xcbuilddata
note: Run script build phase 'Generate Bazel Dependencies' will be run during every build because the option to run the script phase "Based on dependency analysis" is unchecked. (in target 'BazelDependencies' from project 'MyApp')
PhaseScriptExecution Generate\ Bazel\ Dependencies /Users/ec2-user/Library/Developer/Xcode/DerivedData/MyApp-bqspktvzncwdrrfbzlfbtjjpxguv/Build/Intermediates.noindex/MyApp.build/rules_xcodeproj/Script-FF0100000000000000000004.sh (in target 'BazelDependencies' from project 'MyApp')
cd /var/lib/buildkite-agent/bazel-v8/a23a3d7f05a33a445e382baf0a41d6e8/rules_xcodeproj.noindex/build_output_base/execroot/_main
/bin/sh -c /Users/ec2-user/Library/Developer/Xcode/DerivedData/MyApp-bqspktvzncwdrrfbzlfbtjjpxguv/Build/Intermediates.noindex/MyApp.build/rules_xcodeproj/Script-FF0100000000000000000004.sh
error: Failed to calculate labels and target ids from PIFCache:
Traceback (most recent call last):
File "/path/to/MyApp.xcodeproj/rules_xcodeproj/bazel/calculate_output_groups.py", line 383, in _main
build_request = _get_build_request(
File "/path/to/MyApp.xcodeproj/rules_xcodeproj/bazel/calculate_output_groups.py", line 140, in _get_build_request
return _wait_for_value(
File "/path/to/MyApp.xcodeproj/rules_xcodeproj/bazel/calculate_output_groups.py", line 30, in _wait_for_value
value = calculate_value()
File "/path/to/MyApp.xcodeproj/rules_xcodeproj/bazel/calculate_output_groups.py", line 116, in wait_for_build_request
xcbuilddata = max(
File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/genericpath.py", line 65, in getctime
return os.stat(filename).st_ctime
FileNotFoundError: [Errno 2] No such file or directory: '/Users/ec2-user/Library/Developer/Xcode/DerivedData/MyApp-bqspktvzncwdrrfbzlfbtjjpxguv/Build/Intermediates.noindex/XCBuildData/6b62e9ec6732598a060e57bdb109a782.xcbuilddata'
Please file a bug report here: https://github.com/MobileNativeFoundation/rules_xcodeproj/issues/new?template=bug.md
** BUILD FAILED **

Solution

Wrap both getctime() call sites in try/except blocks that catch:

  • ValueError (empty glob, no .xcbuilddata files yet)

  • FileNotFoundError/OSError (TOCTOU race—file deleted between glob and getctime)
  • When these exceptions occur, return None to allow _wait_for_value() to retry properly instead of crashing.

    Testing

  • Validated in our CI environment -- no more flakes with this change
  • Diff: 457 lines (+342 / -115) across 5 files Reviewer: brentleyjones Review duration: 48446m
    All Vouch review confidence checks passed
    Residual risk 0.20 = Change Risk (0.46) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.46 · 457 lines diff across 5 files
    0.46
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.367
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #3295"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 45.5% (MEDIUM) · Higher = larger blast radius
    45.5%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 19.7% · Safe to merge
    19.7%