fix(runtime): recover safely from partial initialization

This commit is contained in:
Boki 2026-10-22 14:17:25 -04:00
parent a21a1b8cb8
commit 3d26da07f5
2 changed files with 190 additions and 40 deletions

View file

@ -13,6 +13,9 @@ import {
type TradingMode,
} from '../types';
const shutdownOrder = ['marketData', 'strategies', 'execution', 'storage'] as const;
type LifecycleService = (typeof shutdownOrder)[number];
export class ModeManager extends EventEmitter {
private mode: TradingMode = 'paper';
private config: ModeConfig | null = null;
@ -49,15 +52,26 @@ export class ModeManager extends EventEmitter {
await this.shutdown();
}
this.mode = config.mode;
this.config = config;
const attempted = new Set<LifecycleService>();
try {
this.mode = config.mode;
this.config = config;
// Create Rust trading engine with appropriate config
const engineConfig = this.createEngineConfig(config);
this.tradingEngine = new TradingEngine(config.mode, engineConfig);
// Create Rust trading engine with appropriate config
const engineConfig = this.createEngineConfig(config);
this.tradingEngine = new TradingEngine(config.mode, engineConfig);
// Initialize services for the mode
await this.initializeServices(config);
await this.initializeServices(config, attempted);
} catch (error) {
try {
await this.shutdownServices(attempted);
} catch {
// Every cleanup was attempted; preserve the original initialization error.
} finally {
this.clearState();
}
throw error;
}
this.isInitialized = true;
this.emit('modeChanged', config);
@ -103,19 +117,54 @@ export class ModeManager extends EventEmitter {
}
}
private async initializeServices(config: ModeConfig): Promise<void> {
private async initializeServices(
config: ModeConfig,
attempted: Set<LifecycleService>
): Promise<void> {
// Register before each await: an initializer may acquire resources then fail.
// Configure execution service
attempted.add('execution');
await this.dependencies.execution.initialize(config, defined(this.tradingEngine));
// Configure storage
attempted.add('storage');
await this.dependencies.storage.initialize(config);
if (config.mode === 'paper') {
attempted.add('strategies');
await this.dependencies.strategies.restoreStrategies();
}
// Admission is ready before a real-time feed can deliver events.
attempted.add('marketData');
await this.dependencies.marketData.initialize(config);
}
private async shutdownServices(attempted: ReadonlySet<LifecycleService>): Promise<void> {
let failed = false;
let failure: unknown;
for (const service of shutdownOrder) {
if (!attempted.has(service)) {
continue;
}
try {
await this.dependencies[service].shutdown();
} catch (error) {
if (!failed) {
failure = error;
}
failed = true;
}
}
if (failed) {
throw failure;
}
}
private clearState(): void {
this.tradingEngine = null;
this.config = null;
this.isInitialized = false;
}
getCurrentMode(): TradingMode {
return this.mode;
}
@ -161,36 +210,11 @@ export class ModeManager extends EventEmitter {
this.dependencies.logger.info(`Shutting down ${this.mode} mode...`);
// Shutdown services
let failure: unknown;
try {
await this.dependencies.marketData.shutdown();
} catch (error) {
failure = error;
}
try {
await this.dependencies.strategies.shutdown();
} catch (error) {
failure ??= error;
}
try {
await this.dependencies.execution.shutdown();
} catch (error) {
failure ??= error;
}
try {
await this.dependencies.storage.shutdown();
} catch (error) {
failure ??= error;
}
// Cleanup trading engine
this.tradingEngine = null;
this.isInitialized = false;
this.emit('shutdown');
if (failure) {
throw failure;
await this.shutdownServices(new Set(shutdownOrder));
} finally {
this.clearState();
this.emit('shutdown');
}
}
}