Repository navigation
Description
Overview
Remove the Arc<Mutex<UserOutput>> pattern throughout the codebase and replace it with direct ownership/borrowing to eliminate deadlock risks and simplify the architecture.
Specification
See detailed specification: docs/issues/164-refactor-useroutput-remove-mutex.md
📋 Background
This refactoring addresses a critical reentrancy deadlock issue discovered in CreateTemplateCommandController and implements the architectural decision documented in ADR: Remove UserOutput Mutex.
Current Problems
- Reentrancy Deadlock: Tests hang indefinitely when
UserOutputmutex is acquired through nested calls - Architectural Mismatch: Complex concurrency mechanisms for a sequential, single-user application
- Timeout Workarounds: Defensive programming patterns that shouldn't be necessary
- Complex Lock Management: Error-prone manual lock scoping throughout codebase
🏗️ Architecture Requirements
DDD Layer: Presentation (primary), Application (secondary)
Module Path: src/presentation/user_output/, command controllers, progress reporters
Pattern: Direct ownership model with mutable borrowing
Module Structure Requirements
- Follow DDD layer separation (see docs/codebase-architecture.md)
- Respect dependency flow rules (dependencies flow toward domain)
- Use appropriate module organization (see docs/contributing/module-organization.md)
Architectural Constraints
- No business logic in presentation layer
- Error handling follows project conventions (see docs/contributing/error-handling.md)
- Testing strategy aligns with layer responsibilities
Anti-Patterns to Avoid
- ❌ Mixing concerns across layers
- ❌ Domain layer depending on infrastructure
- ❌ Monolithic modules with multiple responsibilities
Implementation Plan
Phase 1: Core UserOutput Refactor
- Remove
Arc<Mutex<>>wrapper fromUserOutput - Update
ExecutionContextto ownUserOutputdirectly - Modify command signatures to accept
&mut UserOutput - Remove timeout mechanisms and deadlock prevention code
Phase 2: ProgressReporter Simplification
- Refactor
ProgressReporterto use direct&mut UserOutputaccess - Remove mutex-related error handling in progress reporting
- Simplify progress update methods
Phase 3: Command Controller Updates
- Update all command controllers to use
&mut UserOutput - Remove scoped lock management code
- Simplify error handling paths
Phase 4: Test Updates
- Remove mutex mocking in tests
- Update test fixtures to use direct
UserOutput - Verify no deadlock scenarios can occur in new architecture
Acceptance Criteria
Note for Contributors: These criteria define what the PR reviewer will check. Use this as your pre-review checklist before submitting the PR to minimize back-and-forth iterations.
Quality Checks:
- Pre-commit checks pass:
./scripts/pre-commit.sh
Architecture Validation:
- Zero
Arc<Mutex<UserOutput>>usage in codebase - All command handlers use
&mut UserOutputor ownedUserOutput - No mutex-related timeout or deadlock prevention code
-
ProgressReporteruses direct access patterns
Functional Validation:
- All existing functionality preserved (user experience unchanged)
- All tests pass with no mutex mocking
- E2E tests demonstrate no deadlock scenarios
- Performance improved (no lock contention overhead)
Documentation:
- ADR implementation section updated with results
- Code comments explain new ownership patterns
- Any breaking changes documented
Related Issues
- Root Cause: ProgressReporter deadlock investigation (resolved with this refactor)
- ADR: Remove UserOutput Mutex
References
- Rust Book: Ownership and Borrowing
- Rust Anti-patterns: Shared Mutability
- Development Principles - Observability and simplicity
Activity
docs: [#162] link ADR to GitHub issue #164 for UserOutput refactor
docs: [#162] add specification document for UserOutput mutex removal …
feat: [#162] add ProgressReporter wrapper methods to prevent UserOutp…
Merge #163: feat: [#162] Implement controllers layer with ExecutionCo…
josecelano commentedon Nov 11, 2025
Analysis Update: Deadlock Root Cause and Solution Options
After deeper analysis, I want to clarify the deadlock issue and explain why our initial approach needs revision.
1. Why We Have Deadlock
The deadlock occurs due to reentrancy - the same thread trying to acquire the same mutex twice:
This isn't a concurrency issue between multiple threads - it's a single thread trying to lock the same resource twice, which
std::sync::Mutexdoesn't allow.2. Why We Made UserOutput Singleton (Creating This Problem)
We centralized UserOutput in the Container for a valid architectural reason: ensuring consistent theming/formatting across the entire application.
Before this change, different parts of the application could create their own
UserOutputinstances with different configurations, leading to inconsistent user experience. By making it a singleton in the Container, we guarantee:However, this introduced the shared mutable state problem - multiple services need mutable access to the same
UserOutputinstance.3. Our Initial Idea (Remove Mutex) and Why It Doesn't Work
Our first approach was to remove
Arc<Mutex<UserOutput>>entirely and use direct ownership:Why this doesn't work:
The "sink pattern" insight is key - the output destination must be shared and mutable, so we can't eliminate synchronization entirely.
4. Two Promising Alternatives We're Considering
Option A: ReentrantMutex (Recommended)
Use
parking_lot::ReentrantMutexinstead ofstd::sync::Mutex:Benefits:
Option B: Restructure ProgressReporter (More Complex)
Remove UserOutput storage from ProgressReporter and pass it as parameters:
Benefits:
Drawbacks:
Recommendation
I recommend Option A (ReentrantMutex) because:
The change would involve:
parking_lotdependencystd::sync::Mutex<UserOutput>withparking_lot::ReentrantMutex<UserOutput>What are your thoughts on this approach?
cc @da2ce7
feat: [#164] implement ReentrantMutex<RefCell<UserOutput>> pattern
refactor: [#164] remove unused variables in environment creation tests
refactor: [#164] extract test dependencies helper in destroy handler …
refactor: [#164] move single-type dependency tests to dedicated modules
fix: [#164] standardize all relative paths to use explicit ./ prefix
Merge #175: feat: [#174] implement provision console command