pact-python icon indicating copy to clipboard operation
pact-python copied to clipboard

_wait_for_server_stop cannot stop process and throw AssertionError

Open sentyaev opened this issue 4 years ago • 7 comments

There is a docker-compose configuration with django application. When inside docker I run pytest command to execute pact tests is fails on _wait_for_server_stop call. Looks like that process cannot be stopped for some reason.

The problem is that it's happening only for the first time when I build containers. All the following execution of pytest completes successfully.

I'm trying to figre out the reason, but without success.

This is a stacktrace:

__________ TestProvider.test_something __________

self = <pact.message_provider.MessageProvider object at 0x7fc61e720430>

    def _wait_for_server_stop(self):
        """Wait for server to finish, or raise exception after timeout."""
        retry = 20
        while True:
            self._process.terminate()
            time.sleep(0.1)
            try:
>               assert not self._process.is_alive()
E               AssertionError

/usr/local/lib/python3.10/site-packages/pact/message_provider.py:92: AssertionError

During handling of the above exception, another exception occurred:

    def test_something():
       provider = MessageProvider(
            message_providers={
                'something triggered': message_provider
            },
            provider='api',
            consumer='consumer',
            pact_dir='./_pacts',
        )

>       with provider:

application/tests/services/test_pact.py:100:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/usr/local/lib/python3.10/site-packages/pact/message_provider.py:156: in __exit__
    self._stop_proxy()
/usr/local/lib/python3.10/site-packages/pact/message_provider.py:108: in _stop_proxy
    self._wait_for_server_stop()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <pact.message_provider.MessageProvider object at 0x7fc61e720430>

    def _wait_for_server_stop(self):
        """Wait for server to finish, or raise exception after timeout."""
        retry = 20
        while True:
            self._process.terminate()
            time.sleep(0.1)
            try:
                assert not self._process.is_alive()
                return 0
            except AssertionError:
                if retry == 0:
>                   raise RuntimeError("Process timed out")
E                   RuntimeError: Process timed out

/usr/local/lib/python3.10/site-packages/pact/message_provider.py:96: RuntimeError
----------------------------- Captured stdout call -----------------------------
INFO: Reading pact at ./_pacts/api-consumer.json


Verifying a pact between api and consumer
  Given something triggered
    message-name
WARN: Skipping set up for provider state 'something triggered' for consumer 'consumer' as there is no --provider-states-setup-url specified.
      has matching content

1 interaction, 0 failures
=========================== short test summary info ============================
FAILED application/tests/services/test_pact.py::TestProvider::test_something

sentyaev avatar Mar 23 '22 13:03 sentyaev

One thing I found. If I change this line https://github.com/pact-foundation/pact-python/blob/master/pact/message_provider.py#L89 from self._process.terminate() to self._process.kill() everything works.

I guess it's something related to how SIGTERM and SIGKILL works inside docker container.

sentyaev avatar Mar 23 '22 15:03 sentyaev

kill() uses SIGKILL which terminates the process immediately. terminate() uses SIGTERM which gracefully tries to kill the process, which means something can block it from it trying to kill the process and it can be captured. (like in the method you referred to, it's captured).

My guess is there's child processes laying around which terminate can't kill but kill can't.

They should probably add a python 3.7 check there. use kill if python 3.7 or above and terminate if below

duzumaki avatar Mar 23 '22 17:03 duzumaki

Thanks for the pr @sentyaev

Should we consider @duzumaki's comment regarding python 3.7.

Are we able to try on Python 3.6 and see if it works without the change request in the PR?

Many thanks!

YOU54F avatar Mar 24 '22 18:03 YOU54F

@YOU54F sure, I'll try to find some time to test it.

sentyaev avatar Mar 24 '22 18:03 sentyaev

FYI. I did more experiments with this issue and looks like it's an issue with particular test configuration. Now I'm investigating what is preventing process.terminate() to stop process.

sentyaev avatar Mar 25 '22 13:03 sentyaev

Thank you so much @sentyaev, I really appreciate you taking the time and sharing your learnings

YOU54F avatar Mar 25 '22 13:03 YOU54F

Well... after some investigation I found that this issue happened when test has @freeze_time decorator. So I've remove freeze_time and everything just works. I did not found out yet why this happens, but at least it's unblocks me from start using pact. So, I'll close this issue and PR as well, in case I'll found the reason I'll open another issue.

sentyaev avatar Mar 25 '22 16:03 sentyaev