Files
meshing-around/modules
Martin Bogomolni 9f3446b605 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
2025-10-05 23:45:40 -07:00
..
2025-10-05 09:08:47 -07:00
2025-09-26 19:16:16 -07:00
2025-10-03 12:28:47 -07:00
2025-01-23 17:49:04 -08:00
2024-12-13 12:30:49 -08:00
2025-08-26 10:26:33 -07:00
2025-10-03 15:35:04 -07:00
2025-07-14 22:04:22 -07:00
2025-02-05 19:09:16 -08:00
2024-08-07 22:46:30 -07:00
2025-04-11 14:51:50 -07:00
2025-10-05 17:47:19 -07:00
2024-12-09 21:56:35 -08:00
2025-09-27 17:24:21 -07:00
2025-04-23 07:17:02 -07:00
2024-12-17 20:32:07 -08:00

Modules and Adding stuff

To help with code testing see etc/simulator.py to simulate a bot. I also enjoy meshtasticd(linux-native) in noradio with MQTT server and client to just emulate a mesh.

By following these steps, you can add a new bbs option to the bot.

  1. Define the Command Handler: Add a new function in mesh_bot.py to handle the new command. For example, if you want to add a command newcommand:

    def handle_newcommand(message, message_from_id, deviceID):
        return "This is a response from the new command."
    

    Additionally you can add a whole new module.py, I recommend doing this if you need to import more stuff, try and wedge it into similar spots if you can. You will need to import the file as well, look further at modules/system.py for more.

  2. Add the Command to the Auto Response: Update the auto_response function in mesh_bot.py to include the new command:

    def auto_response(message, snr, rssi, hop, pkiStatus, message_from_id, channel_number, deviceID, isDM):
        #...
        "newcommand": lambda: handle_newcommand(message, message_from_id, deviceID),
        #...
    
  3. Update the Trap List and Help: A quick way to do this is to edit the line 16/17 in modules/system.py to include the new command:

    #...
    trap_list = ("cmd", "cmd?", "newcommand")  # default trap list, with the new command added
    help_message = "Bot CMD?:newcommand, "
    #...
    

    If looking to merge the prefered way would be to update modules/system.py Adding this block below ping which ends around line 28:

    # newcommand Configuration
    newcommand_enabled = True  # settings.py handles the config.ini values; this is a placeholder
    if newcommand_enabled:
         trap_list_newcommand = ("newcommand",)
         trap_list = trap_list + trap_list_newcommand
         help_message = help_message + ", newcommand"
    
  4. Test the New Command: Run MeshBot and test the new command by sending a message with the command newcommand to ensure it responds correctly.

Running a Shell command

Using the above example and enabling the filemon module, you can make a command which calls a bash file to do things on the system.

def auto_response(message, snr, rssi, hop, pkiStatus, message_from_id, channel_number, deviceID, isDM):
    #...
    "switchON": lambda: call_external_script(message)

This would call the default script located in script/runShell.sh and return its output.