Repository navigation
feat: [#164] implement ReentrantMutex<RefCell<UserOutput>> pattern - #166
Conversation
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.
- 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
|
ACK f071644 |
da2ce7
left a comment
There was a problem hiding this comment.
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.
Overview
This PR implements the
ReentrantMutex<RefCell<UserOutput>>pattern to resolve same-thread deadlock issues described in #164. The implementation allows nested calls toUserOutputmethods within the same thread while maintaining thread safety.Problem Statement
The previous
Arc<Mutex<UserOutput>>pattern caused deadlocks when:Solution
Replaced
Arc<Mutex<UserOutput>>withArc<ReentrantMutex<RefCell<UserOutput>>>throughout the codebase:parking_lotcrate)Key Changes
Core Implementation
Arc<Mutex<UserOutput>>→Arc<ReentrantMutex<RefCell<UserOutput>>>.lock().unwrap()→.lock().borrow_mut()for mutable operationsparking_lot = { version = "0.12.3", features = ["std"] }Updated Components
src/bootstrap/container.rs)Testing
tests/user_output_reentrancy.rsTechnical Details
Access Patterns
Type Definitions
Architecture Decision
This change is documented in
docs/decisions/reentrant-mutex-useroutput-pattern.mdwith:Quality Validation
✅ All pre-commit checks passed:
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:
Future Considerations
Related Issues
Closes #164
Checklist