Skip to content

Fix cargo bench deadlock by isolating build script target directories#4

Merged
levicook merged 2 commits intomainfrom
fix/cargo-bench-deadlock
Jun 11, 2025
Merged

Fix cargo bench deadlock by isolating build script target directories#4
levicook merged 2 commits intomainfrom
fix/cargo-bench-deadlock

Conversation

@levicook
Copy link
Copy Markdown
Owner

Perfect branch name! Here's a PR title and description:

PR Title:

Fix cargo bench deadlock by isolating build script target directories

PR Description:

Problem

cargo bench was hanging indefinitely during compilation, blocking development of compute unit benchmarking workflows. The issue manifested as a silent hang with no error output, making it difficult to diagnose.

Root Cause

Through process sampling and system call tracing, we discovered the hang was caused by file lock (flock) contention:

  • Main cargo process holds locks on target/ directory
  • Build scripts spawn cargo build-sbf subprocesses
  • Subprocesses attempt to access the same target/ structure
  • Deadlock occurs when both try to acquire locks simultaneously

Solution

Isolated Build Directories: Use CARGO_TARGET_DIR environment variable to build programs in isolated temp directories, preventing lock contention entirely.

Refactored Build Logic: Extracted ~200 lines of duplicated code into a shared build_solana_program_internal() helper that handles:

  • Temp directory isolation
  • Workspace root detection via OUT_DIR parsing
  • Robust .so file discovery and copying
  • Aggressive error handling with clear diagnostics

Key Changes:

  • Build scripts now use isolated temp directories via CARGO_TARGET_DIR
  • Extract workspace root using /target/ string splitting from OUT_DIR
  • Copy all found .so files (no hardcoded program name assumptions)
  • Fail fast with clear error messages instead of silent failures
  • Fix .so file paths to use correct sbf-solana-solana/release/ location

Impact

Eliminates cargo bench deadlock - Builds complete reliably without hanging
Enables compute unit benchmarking - Unblocks CU measurement and optimization workflows
Improved maintainability - Consolidated build logic, easier to debug and extend
Better error handling - Clear diagnostics instead of mysterious include_bytes! failures

Testing

  • cargo build --workspace completes successfully
  • cargo bench cu_bench_spl_transfer --features cu_bench runs without hanging
  • Both Anchor and Pinocchio programs build and load correctly
  • No scattered target directories or build artifacts

This change provides a solid foundation for building intelligent transaction composition tools based on reliable compute unit measurements.

- Eliminate file lock contention by using isolated temp directories
- Extract shared build logic to prevent duplication (~200 lines)
- Add aggressive error handling with clear failure diagnostics
- Use CARGO_TARGET_DIR isolation instead of shared target/ directory
- Fix .so file paths to use correct sbf-solana-solana/release location
- Add cu_bench feature scaffolding for compute unit benchmarking
- Consolidate build_anchor_program and build_pinocchio_program logic

This resolves indefinite hanging in `cargo bench` caused by flock
contention between main cargo process and build script subprocesses.
The refactored build system is more robust, maintainable, and enables
reliable benchmarking for compute unit measurement workflows.
@levicook levicook self-assigned this Jun 11, 2025
- Move build_solana_program_internal from lib.rs to build_internal.rs
- Add cargo clean step before building to ensure fresh artifacts
- Update function calls in anchor and pinocchio modules to use new path
- Gate build_internal module with appropriate feature flags
- Remove redundant documentation reference in anchor build function
- Improve file extension checking with is_some_and for clarity

This refactoring improves code organization by separating build utilities
from the main library interface while enhancing build reliability.
@levicook levicook merged commit ceb3248 into main Jun 11, 2025
2 checks passed
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.

1 participant