mitmproxy icon indicating copy to clipboard operation
mitmproxy copied to clipboard

mitmproxy doesn't exit without connection to web UI

Open catap opened this issue 1 year ago • 7 comments

Problem Description

Mitmproxy doesn't exit on Ctrl-C inside commond line without connection to web UI

Steps to reproduce the behavior:

  1. Run mitmweb
  2. Close tab or window which was open to visit http://127.0.0.1:8081/
  3. Press Ctrl - C in terminal
  4. Nothing happens
  5. Open http://127.0.0.1:8081/ in browser
  6. It exists

System Information

Mitmproxy: 10.4.2
Python:    3.11.9
OpenSSL:   LibreSSL 3.9.0
Platform:  OpenBSD-7.6-amd64-64bit-ELF

catap avatar Aug 25 '24 17:08 catap

This issue is likely specific to (Open)BSD. At least I can't reproduce it on Linux and macOS.

mhils avatar Aug 26 '24 08:08 mhils

@mhils quite probably. Special that I had worked on port for OpenBSD for mitmproxy for a few moths, and this was imported last week.

Any suggestion how can I debug it future to provide some useful traces?

catap avatar Aug 26 '24 11:08 catap

Here's our SIGTERM handling: https://github.com/mitmproxy/mitmproxy/blob/332f222994deb27bb0ab68618b23f270b3bdfe2d/mitmproxy/tools/main.py#L116-L128

I would assume the handler is never called?

mhils avatar Aug 26 '24 13:08 mhils

What happens when you --no-web-open-browser? My guess is that's a focus issue with the terminal and you need to manually click into it or something.

Prinzhorn avatar Aug 26 '24 15:08 Prinzhorn

@Prinzhorn when I run mitmweb --no-web-open-browser it indeed doesn't open web browser, and it ignores Ctrl - C. But if I go to http://127.0.0.1:8081/ after I had pressed C-c, it exists. Without visiting the web UI it stucks.

@mhils yeah, signal never fire the callback. But if I use loop.add_signal_handler it works like a charm.

catap avatar Aug 26 '24 19:08 catap

Thanks! Do you want to send a patch that tries loop.add_signal_handler first and then falls back to signal.signal on exception? :)

mhils avatar Aug 27 '24 01:08 mhils

@mhils here it is: https://github.com/mitmproxy/mitmproxy/pull/7130

Feel free to push into branch anything, if you think that I had missed it.

catap avatar Aug 27 '24 14:08 catap