Summary
Establish standardized singleton pattern guidelines for the entire kcenon ecosystem to prevent Static Destruction Order Fiasco (SDOF) issues across all systems.
Background
Multiple systems in the kcenon ecosystem use singleton patterns for various managers and loggers. The interaction between these singletons during static destruction has caused recurring issues:
Problem Statement
When multiple systems are integrated (e.g., network_system with thread_system), their singletons can be destroyed in unpredictable order, leading to:
free(): invalid pointer crashes
- Accessing destroyed objects
- Undefined behavior during process termination
Affected Singletons (Known)
| System |
Singleton |
Current Pattern |
Issue |
| thread_system |
thread_logger |
Intentional Leak ✅ |
#293 (Fixed) |
| thread_system |
thread_pool (implicit) |
Meyer's Singleton |
#295 |
| network_system |
io_context_thread_manager |
Meyer's Singleton |
#302, #304 |
| network_system |
logger_integration_manager |
Intentional Leak ✅ |
- |
| logger_system |
logger |
Unknown |
Needs audit |
| container_system |
Various managers |
Unknown |
Needs audit |
Proposed Guidelines
1. Default Pattern: Intentional Leak for Infrastructure Singletons
class MySingleton {
public:
static MySingleton& instance() {
// Intentionally leak to avoid SDOF
static MySingleton* instance = new MySingleton();
return *instance;
}
// Optional: explicit shutdown for resource cleanup
static void shutdown() {
// ... cleanup logic (logging, flushing, etc.)
}
private:
MySingleton() = default;
~MySingleton() = default; // Never called
};
2. When Intentional Leak is Required
- Loggers and logging infrastructure
- Thread pools and async managers
- Any singleton that might be accessed during other objects' destruction
- Global managers that coordinate cross-system resources
3. When Meyer's Singleton is Acceptable
- Pure data singletons with no dependencies
- Configuration managers that are only read during initialization
- Singletons that are guaranteed to be destroyed last
4. Documentation Requirements
Each singleton should document:
- Why this pattern was chosen
- Memory implications
- Shutdown procedure (if any)
5. Static Destruction Safety Checklist
For new singletons, verify:
Implementation Plan
Phase 1: Audit (All Systems)
Phase 2: Documentation
Phase 3: Migration
Acceptance Criteria
Related Issues
thread_system
network_system
References
Summary
Establish standardized singleton pattern guidelines for the entire kcenon ecosystem to prevent Static Destruction Order Fiasco (SDOF) issues across all systems.
Background
Multiple systems in the kcenon ecosystem use singleton patterns for various managers and loggers. The interaction between these singletons during static destruction has caused recurring issues:
Problem Statement
When multiple systems are integrated (e.g., network_system with thread_system), their singletons can be destroyed in unpredictable order, leading to:
free(): invalid pointercrashesAffected Singletons (Known)
thread_loggerthread_pool(implicit)io_context_thread_managerlogger_integration_managerloggerProposed Guidelines
1. Default Pattern: Intentional Leak for Infrastructure Singletons
2. When Intentional Leak is Required
3. When Meyer's Singleton is Acceptable
4. Documentation Requirements
Each singleton should document:
5. Static Destruction Safety Checklist
For new singletons, verify:
Implementation Plan
Phase 1: Audit (All Systems)
Phase 2: Documentation
SINGLETON_GUIDELINES.mdin common_systemPhase 3: Migration
shutdown()methods where explicit cleanup is neededAcceptance Criteria
SINGLETON_GUIDELINES.mdcreated in common_systemRelated Issues
thread_system
network_system
References