Skip to content

feat: [#164] implement ReentrantMutex<RefCell<UserOutput>> pattern - #166

Merged
josecelano merged 5 commits into
mainfrom
164-refactor-useroutput-remove-mutex
Nov 12, 2025
Merged

josecelano merged 5 commits into
mainfrom
164-refactor-useroutput-remove-mutex

Conversation

@josecelano

Copy link
Copy Markdown
Member

Overview

This PR implements the ReentrantMutex<RefCell<UserOutput>> pattern to resolve same-thread deadlock issues described in #164. The implementation allows nested calls to UserOutput methods within the same thread while maintaining thread safety.

Problem Statement

The previous Arc<Mutex<UserOutput>> pattern caused deadlocks when:

  • A command handler acquired the UserOutput mutex
  • Called a step that also needed to acquire the same mutex
  • The second acquisition blocked indefinitely on the same thread

Solution

Replaced Arc<Mutex<UserOutput>> with Arc<ReentrantMutex<RefCell<UserOutput>>> throughout the codebase:

  • ReentrantMutex: Allows same-thread multiple acquisitions (from parking_lot crate)
  • RefCell: Provides runtime borrow checking for interior mutability
  • Arc: Enables sharing across threads

Key Changes

Core Implementation

  • 🔄 Type Pattern: Arc<Mutex<UserOutput>> → Arc<ReentrantMutex<RefCell<UserOutput>>>
  • 🔄 Access Pattern: .lock().unwrap() → .lock().borrow_mut() for mutable operations
  • 📦 Dependency: Added parking_lot = { version = "0.12.3", features = ["std"] }

Updated Components

  • Bootstrap Container (src/bootstrap/container.rs)
  • Application Layer (command handlers, steps, services)
  • Presentation Layer (controllers, dispatch, progress reporting)
  • Documentation Examples (19 doctests updated with correct type patterns)

Testing

  • ✅ Reentrancy Tests: Added comprehensive tests in tests/user_output_reentrancy.rs
  • ✅ Unit Tests: 1,196 tests passing
  • ✅ Integration Tests: 30 tests passing
  • ✅ Documentation Tests: 271 tests passing (all doctest examples fixed)
  • ✅ E2E Tests: Both provision/destroy and configuration test suites passing

Technical Details

Access Patterns

// Before (deadlock-prone)
let output = self.user_output.lock().unwrap();
output.success("Done!");

// After (reentrant-safe)  
let output = self.user_output.lock();
output.borrow_mut().success("Done!");

Type Definitions

// Container provides reentrant-wrapped UserOutput
pub fn user_output(&self) -> Arc<ReentrantMutex<RefCell<UserOutput>>> {
    self.user_output.clone()
}

Architecture Decision

This change is documented in docs/decisions/reentrant-mutex-useroutput-pattern.md with:

  • Problem analysis and root cause identification
  • Solution evaluation and trade-offs
  • Implementation approach and patterns
  • Testing strategy and validation

Quality Validation

✅ All pre-commit checks passed:

  • Dependencies: No unused dependencies (cargo machete)
  • Linting: All linters passed (stable & nightly toolchains)
  • Tests: All test suites passing
  • Documentation: All examples compile and demonstrate correct usage
  • E2E Tests: Infrastructure and configuration validation complete

Breaking Changes

❌ None - This is an internal implementation change that maintains all existing public APIs.

Migration Notes

No migration required for users of the application. The change is transparent to:

  • Command-line interface users
  • External integrations
  • Existing workflows and scripts

Future Considerations

  • Performance: ReentrantMutex has minimal overhead compared to standard Mutex
  • Maintainability: RefCell borrow checking provides runtime safety guarantees
  • Extensibility: Pattern supports future nested UserOutput scenarios

Related Issues

Closes #164

Checklist

  • Implementation complete and tested
  • All tests passing (unit, integration, documentation, E2E)
  • Documentation examples updated and validated
  • Architecture decision recorded
  • Pre-commit checks passed
  • No breaking changes to public APIs
  • Performance impact minimal

Replace Arc<Mutex<UserOutput>> with Arc<ReentrantMutex<RefCell<UserOutput>>>
throughout codebase to resolve same-thread reentrancy deadlock.

Key changes:
- Add parking_lot dependency for ReentrantMutex support
- Update Container, ExecutionContext, and all controllers
- Convert ProgressReporter to use ReentrantMutex pattern
- Fix test infrastructure compatibility with parking_lot
- Add comprehensive reentrancy verification tests
- Update error handling to use ReentrantMutex pattern
- Add architectural decision record for pattern choice

All core functionality maintained while eliminating deadlock risk.
Tests verify same-thread multiple lock acquisitions work correctly.
@josecelano
josecelano requested a review from da2ce7 November 12, 2025 09:07
- Remove unused _user_output variables from all test functions
- Remove unused _working_dir parameter from create_test_context function
- Remove unused imports (TestUserOutput and std::path::Path)
- Simplify test setup by removing unnecessary user output instances
…tests

- Add create_test_dependencies() helper function to eliminate code duplication
- Remove repeated UserOutput, RepositoryFactory, and SystemClock creation in tests
- Improve test maintainability and readability
- All 4 test functions now use the centralized helper function
- Add comprehensive documentation for the helper function
- Systematic analysis of all test modules in src/presentation/user_output/core.rs
- Moved 22 unit tests to appropriate struct modules:
  - 6 type-safe wrapper tests → sinks/writers.rs
  - 16 verbosity filter tests → verbosity.rs
- Removed 7 duplicate theme tests from core.rs
- Preserved 8 integration test modules in core.rs (~65+ tests)
- All tests continue to pass, no functionality lost
- Clear separation: unit tests (single struct) in modules, integration tests (multiple structs) in core.rs
- Reduced core.rs from 2396 to 2282 lines while maintaining comprehensive test coverage
- Move 29 tests from core.rs to appropriate module files:
  - 3 tests → progress.rs (ProgressMessage tests)
  - 1 test → success.rs (SuccessMessage tests)
  - 1 test → error.rs (ErrorMessage tests)
  - 2 tests → result.rs (ResultMessage tests)
  - 1 test → warning.rs (WarningMessage tests)
  - 1 test → channel.rs (Channel enum tests)
  - 10 tests → steps.rs (StepsMessage tests)
  - 9 tests → info_block.rs (InfoBlockMessage tests)
  - 2 tests → composite.rs (CompositeSink tests)
  - 1 test → telemetry.rs (TelemetrySink tests)

- Remove all duplicate tests from core.rs (350+ lines reduction)
- Improve test organization by placing tests with their related types
- Maintain 100% test coverage with all 137 user_output tests passing
- Clean up module structure for better maintainability
@josecelano josecelano self-assigned this Nov 12, 2025
@josecelano
josecelano marked this pull request as ready for review November 12, 2025 11:16
@josecelano

Copy link
Copy Markdown
Member Author

ACK f071644

@josecelano
josecelano merged commit f4d9804 into main Nov 12, 2025
@josecelano josecelano mentioned this pull request Nov 12, 2025
14 tasks done

@da2ce7 da2ce7 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.

This is a post-merge review of the change from #166 as it stands at f782b40b5 on the default branch.

What still runs. The shared output service is Arc<ReentrantMutex<RefCell<UserOutput>>>. It is built once in src/bootstrap/container.rs:94-96 (field at :58) and handed to every controller, to ProgressReporter (src/presentation/cli/views/progress/mod.rs:69) and to VerboseProgressListener. ProgressReporter writes through with_output (progress/mod.rs:109-118), which takes the lock and then calls try_borrow_mut. The other call sites take the lock and call borrow_mut() within one statement. The files have since moved under src/presentation/cli/, and later commits changed which channel progress lines go to, but the locking code is unchanged from #166. Both reentrancy tests still run: tests/user_output_reentrancy.rs and src/presentation/cli/tests/reentrancy_fix_test.rs.

[Minor] src/presentation/cli/views/progress/mod.rs:113-116 maps a failed try_borrow_mut to ProgressReporterError::UserOutputMutexPoisoned. The doc and message of that variant (:29-37) say the mutex was poisoned by a panic in another thread. parking_lot mutexes never poison, so at this commit the variant has only one cause: a second write on the same thread while a RefMut is still alive. That is the nested case this change was written to handle. Take out, the Arc that was passed to ProgressReporter::new. A caller that holds let g = out.lock(); let _w = g.borrow_mut(); and then calls progress.complete(..) gets Err(UserOutputMutexPoisoned). The user is then told about a cross-thread panic that never happened. Smallest fix: rename the variant (for example UserOutputAlreadyBorrowed) and make its doc and message name the real cause, which is that output was already being written on this thread.

[Minor] The tests named for nesting never nest. it_should_not_deadlock_when_nested_user_output_calls_occur (src/presentation/cli/tests/reentrancy_fix_test.rs:17-52) drops both the borrow and the guard at :31-32 before it locks again at :42. it_should_handle_complex_nested_output_when_testing_reentrancy releases the guard at :71 before it uses the reporter. Both tests would pass unchanged on std::sync::Mutex, even though the comments at :34-41 say that would deadlock. Only tests/user_output_reentrancy.rs:47-55 holds one guard while taking a second, and it drops the borrow first (:48-51). No test covers a write made while a borrow is still alive. In that case ProgressReporter returns the error above, and both VerboseProgressListener::with_output (src/presentation/cli/views/progress/verbose_listener.rs:58) and handle_error (src/presentation/cli/error.rs:97) panic with already borrowed. Smallest fix: make the in-crate tests keep the first guard across the reporter calls. Then add one test that holds lock().borrow_mut(), calls progress.complete(..) and asserts the error, so the limit of the pattern is tested.

[Nit] docs/decisions/reentrant-mutex-useroutput-pattern.md:123-137 says the pattern is single-thread only because RefCell is not Sync (:127). That contradicts :104 of the same record and the container doc at src/bootstrap/container.rs:117-118. parking_lot implements Sync for ReentrantMutex<T> when T: Send, so the Arc can be shared across threads. Other threads block on the lock, and only re-entry on the same thread gets through. The alternative type at :134 would not change any of this. Smallest fix: replace the section with an accurate statement. Access from other threads is serialised, a second guard on the same thread is safe, and a second live borrow on the same thread is not.

Checked and found sound: no production call site holds a guard or a borrow across another output call or an .await. Every use outside ProgressReporter is scoped to one statement: src/presentation/cli/controllers/destroy/handler.rs:228, exists/handler.rs:146, purge/handler.rs:219-225, test/handler.rs:201-206 and src/presentation/cli/error.rs:96-100. The closures in ProgressReporter and VerboseProgressListener call only UserOutput methods. None of the failure modes above can therefore be reached from the CLI at this commit. Removing the poisoned-mutex fallback from handle_error loses nothing, because parking_lot cannot poison. Switching MockClock to parking_lot::Mutex (src/testing/mock_clock.rs) keeps the same behaviour. The message builders in src/presentation/cli/views/messages/steps.rs and info_block.rs, and the tests added to src/presentation/cli/views/sinks/writers.rs, match the code they cover.

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.

Refactor UserOutput to remove Arc<Mutex<>> pattern

2 participants