Bug Report: API service fails to start with the `--api-debug-mode` option on MacOS
Summary
The API service fails to start with the --api-debug-mode option on MacOS. The error message is OSError: [Errno 9] Bad file descriptor.
This appears to be related to Python 3.12’s multiprocessing spawn behavior on macOS when combined with the Flask/Werkzeug development reloader.
Linux and macOS are supported by Nettacker in the Installation Guide.
Steps to reproduce
Start the API service with the command:
poetry run nettacker --start-api --api-host 127.0.0.1 --api-port 8080 --api-debug-mode
The error was returned and the service failed to start:
File "<string>", line 1, in <module>
File "/opt/homebrew/Cellar/[email protected]/3.12.12/Frameworks/Python.framework/Versions/3.12/lib/python3.12/multiprocessing/spawn.py", line 121, in spawn_main
parent_sentinel = os.dup(pipe_handle)
^^^^^^^^^^^^^^^^^^^
OSError: [Errno 9] Bad file descriptor
Suggested Fix
Add use_reloader=False when the debug mode is enabled with options.api_debug_mode as True in the code:
https://github.com/OWASP/Nettacker/blob/ed9c8cad306fdf96b26b54175c30ab218bb84851/nettacker/api/engine.py#L587
Or only support Python versions without this issue. Or consider changing the multiprocessing behavior.
Environment
- macOS (Apple Silicon, Homebrew Python)
- Python 3.12
- Nettacker 0.4.0
- Installed and executed via Poetry
I looked into this. The issue is in start_api_subprocess() in nettacker/api/engine.py.
When debug=True is passed to app.run(), Flask/Werkzeug enables the development reloader, which spawns a child process via multiprocessing.spawn. On macOS with Python 3.12, the spawn method fails with OSError: Bad file descriptor because the parent sentinel fd can't be duplicated in the subprocess context.
The fix is straightforward -- pass use_reloader=False to both app.run() calls:
app.run(
host=options.api_hostname,
port=options.api_port,
debug=options.api_debug_mode,
use_reloader=False,
ssl_context=...,
threaded=True,
)
This preserves debug mode features (detailed error pages, stack traces) while avoiding the reloader subprocess issue. The reloader is redundant here anyway since start_api_server() already manages the Flask process via multiprocessing.Process.
Happy to submit a PR if a maintainer confirms. @arkid15r @securestep9
The bug is legit. I am not sure about the fix. Shouldn't be do use_reloader = False, only when options.api_debug_mode is set and not always?
The bug is legit. I am not sure about the fix. Shouldn't be do
use_reloader = False, only whenoptions.api_debug_modeis set and not always?
The crash is tied to the multiprocessing.spawn behavior in Python 3.12 on macOS. This happens whenever the reloader is active, regardless of the debug setting, because the reloader attempts to manage a child process that conflicts with the environment.
While we could make the fix conditional (OS/version-specific), I opted to disable it because 'auto-reload on code change' isn't a core requirement for a security tool like Nettacker. By setting use_reloader=False, we ensure stable startup on all platforms while still keeping other debug features fully functional when debug mode is on.
@pUrGe12 Good point. I looked at this more carefully.
Flask's app.run() already defaults use_reloader=False when debug=False. So setting use_reloader=False unconditionally is functionally equivalent to a conditional approach -- it's a no-op when debug is off.
That said, I agree being explicit is cleaner. Here's my updated proposal -- unconditional use_reloader=False with a comment explaining why:
app.run(
host=options.api_hostname,
port=options.api_port,
debug=options.api_debug_mode,
use_reloader=False, # Disabled: conflicts with multiprocessing.Process in start_api_server()
ssl_context=...,
threaded=True,
)
The reloader is inherently incompatible with the multiprocessing.Process wrapper in start_api_server(), regardless of debug mode. Making it conditional (use_reloader=False if options.api_debug_mode else ...) would be misleading since the reloader should never run when the server is already managed by an external process manager.
Happy to go either way once a maintainer confirms the direction. @arkid15r @securestep9