Auto-exit in --headless --exec mode when script completes #127

Closed
opened 2025-10-31 02:48:26 +00:00 by john · 0 comments
Owner

Problem

Currently, ./mcrogueface --headless --exec <script> hangs indefinitely after the script completes. The game loop continues running because there's no mechanism to detect script completion and exit automatically.

This requires workarounds like:

  • External timeout commands to kill hung processes
  • Explicit mcrfpy.exit() calls in every script
  • Manual process cleanup

Root Cause

File: src/main.cpp:197-203

When --exec is used, the code calls engine->run() which starts an infinite loop (while (running) in GameEngine.cpp:197). The running flag never becomes false unless:

  • Window is closed (not applicable in headless mode)
  • Script explicitly calls mcrfpy.exit()

Proposed Solution

Auto-exit when no more Python work is pending in headless + exec mode:

  1. Add auto_exit_after_exec flag to McRogueFaceConfig
  2. Set this flag when both --headless and --exec are present
  3. In game loop, check if timers.empty() and auto_exit_after_exec is true
  4. Exit automatically when no timers remain

Implementation Details

Files to modify:

  • src/McRogueFaceConfig.h - Add flag
  • src/main.cpp:197-203 - Set flag appropriately
  • src/GameEngine.cpp:191-291 - Check condition in game loop

Exit condition:

if (config.auto_exit_after_exec && timers.empty()) {
    running = false;
}

Benefits

  • Documentation generation scripts work without explicit exit calls
  • Testing scripts don't need timeout wrappers
  • Clean process termination
  • Backward compatible (existing scripts with mcrfpy.exit() continue working)

Edge Cases Handled

  • Scripts with timers wait for completion before exiting
  • Scripts with mcrfpy.exit() exit immediately (auto-exit never triggers)
  • Scripts wanting continuous rendering can keep a dummy timer alive
  • Interactive mode unaffected (only applies to headless + exec)

Success Criteria

# Should exit cleanly without hanging
./build/mcrogueface --headless --exec tools/generate_dynamic_docs.py
echo $?  # Should be 0
## Problem Currently, `./mcrogueface --headless --exec <script>` hangs indefinitely after the script completes. The game loop continues running because there's no mechanism to detect script completion and exit automatically. This requires workarounds like: - External `timeout` commands to kill hung processes - Explicit `mcrfpy.exit()` calls in every script - Manual process cleanup ## Root Cause **File:** `src/main.cpp:197-203` When `--exec` is used, the code calls `engine->run()` which starts an infinite loop (`while (running)` in `GameEngine.cpp:197`). The `running` flag never becomes `false` unless: - Window is closed (not applicable in headless mode) - Script explicitly calls `mcrfpy.exit()` ## Proposed Solution Auto-exit when no more Python work is pending in headless + exec mode: 1. Add `auto_exit_after_exec` flag to `McRogueFaceConfig` 2. Set this flag when both `--headless` and `--exec` are present 3. In game loop, check if `timers.empty()` and `auto_exit_after_exec` is true 4. Exit automatically when no timers remain ## Implementation Details **Files to modify:** - `src/McRogueFaceConfig.h` - Add flag - `src/main.cpp:197-203` - Set flag appropriately - `src/GameEngine.cpp:191-291` - Check condition in game loop **Exit condition:** ```cpp if (config.auto_exit_after_exec && timers.empty()) { running = false; } ``` ## Benefits - Documentation generation scripts work without explicit exit calls - Testing scripts don't need `timeout` wrappers - Clean process termination - Backward compatible (existing scripts with `mcrfpy.exit()` continue working) ## Edge Cases Handled - Scripts with timers wait for completion before exiting - Scripts with `mcrfpy.exit()` exit immediately (auto-exit never triggers) - Scripts wanting continuous rendering can keep a dummy timer alive - Interactive mode unaffected (only applies to headless + exec) ## Success Criteria ```bash # Should exit cleanly without hanging ./build/mcrogueface --headless --exec tools/generate_dynamic_docs.py echo $? # Should be 0 ```
john added the
Minor Feature
Tiny Feature
Major Feature
labels 2025-10-31 02:48:26 +00:00
john closed this issue 2025-10-31 02:53:04 +00:00
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: john/McRogueFace#127
No description provided.