Skip to content

Register for signals only when running in the main thread #1086

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bmbouter opened this issue Feb 10, 2022 · 14 comments · Fixed by #1087
Closed

Register for signals only when running in the main thread #1086

bmbouter opened this issue Feb 10, 2022 · 14 comments · Fixed by #1087
Assignees
Labels
Bug Bug report in proxy server

Comments

@bmbouter
Copy link

Describe the bug
We have a simple pytest fixture that sets up a proxy.py. Here's a copy/paste of it:

import proxy
proxypy_args = ["--num-workers", "4", "--hostname", host, "--port", str(port)]
with proxy.Proxy(input_args=proxypy_args):
    yield

When running it locally it works perfectly. When running it in the Github Actions CI environment it fails ocassionally, but enough that it's a problem. Here's the traceback we have gotten several times:

_____________ ERROR at setup of test_sync_https_through_http_proxy _____________
[gw7] linux -- Python 3.8.12 /opt/hostedtoolcache/Python/3.8.12/x64/bin/python

unused_port = <function unused_port.<locals>._unused_port at 0x7f6295b373a0>

    @pytest.fixture
    def http_proxy(unused_port):
        host = cfg.aiohttp_fixtures_origin
        port = unused_port()
        proxypy_args = [
            "--num-workers",
            "4",
            "--hostname",
            host,
            "--port",
            str(port),
        ]
    
        proxy_data = ProxyData(host=host, port=port)
    
>       with proxy.Proxy(input_args=proxypy_args):

/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/pulp_smash/pulp3/pytest_plugin/__init__.py:164: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/proxy/proxy.py:171: in __enter__
    self.setup()
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/proxy/proxy.py:258: in setup
    self._register_signals()
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/proxy/proxy.py:309: in _register_signals
    signal.signal(signal.SIGINT, self._handle_exit_signal)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

signalnum = <Signals.SIGINT: 2>
handler = <function Proxy._handle_exit_signal at 0x7f629ddd8e50>

    @_wraps(_signal.signal)
    def signal(signalnum, handler):
>       handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler))
E       ValueError: signal only works in main thread

/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/signal.py:47: ValueError

To Reproduce
I don't have a great reproducer. It's just pytest on github actions.

Version information

  • OS: Ubuntu (github issues), but pip installed
  • proxy.py==2.4.0
@bmbouter bmbouter added the Bug Bug report in proxy server label Feb 10, 2022
@bmbouter
Copy link
Author

The issue here seems to be on proxy shutdown. What I don't understand is what is introducing threading. We use the generic pytest runner so I don't see how it could be threaded. Does proxy.py use threading in how I'm running it?

@bmbouter
Copy link
Author

Per the docs, I expected it to be using threadless because it's python3.8+ and linux, but maybe it's not? I'm not specifying --threadless but maybe I should? How can I confirm which mode proxy.py is running in?

@abhinavsingh
Copy link
Owner

abhinavsingh commented Feb 10, 2022

Hi @bmbouter , as you discovered, it will run in --threadless (local) mode by default. Under this mode, it will spawn processes for acceptors. I see you are using --num-workers which has no effect in local executor mode. That flag is application when running in remote executor mode. I know it's confusing, but you must use --num-acceptors to start 4 acceptor processes. Further, each acceptor will spawn a single thread for asyncio request handling.

Per this thread https://bugs.python.org/issue38904, ValueError: signal only works in main thread is a problem starting from Python 3.8. Likely, pytest starts the fixture in a separate thread or something, I am not sure. Irrespective, per that thread, having this guard will help:

if threading.current_thread() is threading.main_thread() then register signals

@abhinavsingh abhinavsingh changed the title Intermittent failure when running in a test Register for signals only when running in the main thread Feb 10, 2022
@abhinavsingh
Copy link
Owner

You can try it from #1087 branch. I hope this fix works out for you :)

@abhinavsingh
Copy link
Owner

If you are interested, read about local vs remote modes here.

@abhinavsingh
Copy link
Owner

Merged. Lemme know if it works. You can try out the pre-releases for quick verification. Happy to release 2.4.1 if this helps you.

@bmbouter
Copy link
Author

I'm going to test it now and it'll run in our CI for a while. I'll watch for the issue for a week and let you know. Feel free to cut a 2.4.1 sooner if you want or you can wait to hear back.

You are incredibly responsive, and I really really appreciate it and this proxy. proxy.py is great!

@bmbouter
Copy link
Author

@abhinavsingh after a week of observation while our CI uses the commit with the fix, I can confirm we've seen 0 instances of the issue anymore. If you're able to release 2.4.1 that would be much appreciated. Thank you! ❤️

@abhinavsingh
Copy link
Owner

@bmbouter Thank you, I'll make a release over the weekend. I ran it last night and it failed due to spell check errors :(. Will try over the weekend :)

@bmbouter
Copy link
Author

bmbouter commented Mar 7, 2022

@abhinavsingh is releasing the 2.4.1 possible? Thanks!

@abhinavsingh
Copy link
Owner

@bmbouter I'll give it a try this week. Apologies for the delay. Something has gone wrong with rst markdown lib due to which our GHA is failing, unable to make new release. I'll have to either fix it or do a manual release.

Give me time till Wednesday. Thank you!!!

@bmbouter
Copy link
Author

bmbouter commented Mar 7, 2022

It's no problem at all. If you have time that would be great. Thank you!

@abhinavsingh
Copy link
Owner

#1104 and #1107 will help us make automated release again. Looks like it is very important for us to make constant releases. Long delays can cause CI to randomly fail due to upstream build only dependency changes.

We'll release 2.4.1 shortly. Best

@bmbouter
Copy link
Author

This is wonderful, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Bug report in proxy server
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants