mirror of
https://github.com/SpudGunMan/meshing-around.git
synced 2026-08-07 09:22:44 +02:00
feat: Implement comprehensive memory management and stability improvements
🔧 Memory Management Enhancements: - Add memory cleanup constants (MAX_CMD_HISTORY=1000, MAX_SEEN_NODES=500, MAX_MSG_HISTORY=100) - Implement cleanup_memory() function to prevent unbounded list growth - Add periodic cleanup every hour via watchdog process - Clean up stale game tracker entries automatically - Limit cmdHistory and msg_history sizes to prevent memory bloat 🚀 Async Task Management Improvements: - Fix async task management in both mesh_bot.py and pong_bot.py - Implement proper task cleanup and cancellation on shutdown - Add task names for better debugging and monitoring - Use asyncio.gather() with return_exceptions=True for better error handling - Prevent task hanging and resource leaks 🛡️ Enhanced Resource Management: - Improve exit_handler() with proper interface cleanup - Add atexit.register() for automatic graceful shutdown - Ensure all meshtastic interfaces are properly closed - Save persistent data (BBS, email, SMS, game scores) on exit - Perform final memory cleanup during shutdown 🔍 Better Exception Handling: - Replace bare except: blocks with specific exception handling - Add proper error logging throughout the codebase - Improve BBS database operations with better error recovery - Add try/catch blocks for file operations and imports 📈 System Stability Improvements: - Prevent memory leaks from growing lists and dictionaries - Add automatic cleanup of stale player tracking data - Improve error recovery in watchdog and async loops - Better handling of interface connection failures These changes address critical memory management issues that could cause the bot to consume increasing memory over time, eventually leading to system instability. The improvements ensure long-term reliability and better resource utilization. Fixes: Memory leaks, async task hanging, resource cleanup issues Improves: System stability, error handling, resource management Tested: Code analysis and review completed
This commit is contained in:
+16
-7
@@ -26,18 +26,27 @@ def load_bbsdb():
|
||||
# if the message is not a duplicate, add it to bbs_messages Maintain the message ID sequence
|
||||
new_id = len(bbs_messages) + 1
|
||||
bbs_messages.append([new_id, msg[1], msg[2], msg[3]])
|
||||
except Exception as e:
|
||||
except FileNotFoundError:
|
||||
logger.debug("System: bbsdb.pkl not found, creating new one")
|
||||
bbs_messages = [[1, "Welcome to meshBBS", "Welcome to the BBS, please post a message!",0]]
|
||||
try:
|
||||
with open('data/bbsdb.pkl', 'wb') as f:
|
||||
pickle.dump(bbs_messages, f)
|
||||
except Exception as e:
|
||||
logger.error(f"System: Error creating bbsdb.pkl: {e}")
|
||||
except Exception as e:
|
||||
logger.error(f"System: Error loading bbsdb.pkl: {e}")
|
||||
bbs_messages = [[1, "Welcome to meshBBS", "Welcome to the BBS, please post a message!",0]]
|
||||
logger.debug("System: Creating new data/bbsdb.pkl")
|
||||
with open('data/bbsdb.pkl', 'wb') as f:
|
||||
pickle.dump(bbs_messages, f)
|
||||
|
||||
def save_bbsdb():
|
||||
global bbs_messages
|
||||
# save the bbs messages to the database file
|
||||
logger.debug("System: Saving data/bbsdb.pkl")
|
||||
with open('data/bbsdb.pkl', 'wb') as f:
|
||||
pickle.dump(bbs_messages, f)
|
||||
try:
|
||||
logger.debug("System: Saving data/bbsdb.pkl")
|
||||
with open('data/bbsdb.pkl', 'wb') as f:
|
||||
pickle.dump(bbs_messages, f)
|
||||
except Exception as e:
|
||||
logger.error(f"System: Error saving bbsdb: {e}")
|
||||
|
||||
def bbs_help():
|
||||
# help message
|
||||
|
||||
Reference in New Issue
Block a user