Skip to content

Remove unnecessary thread_ident assignment#8194

Merged
emlove merged 2 commits into
home-assistant:devfrom
emlove:fix-test-instability
Jun 25, 2017
Merged

Remove unnecessary thread_ident assignment#8194
emlove merged 2 commits into
home-assistant:devfrom
emlove:fix-test-instability

Conversation

@emlove
Copy link
Copy Markdown
Contributor

@emlove emlove commented Jun 24, 2017

Description:

I'm hoping this will help fix some of the test instability. get_test_home_assistant calls async_test_home_assistant, which is running the event loop normally, so the patching isn't necessary.

@mention-bot
Copy link
Copy Markdown

@armills, thanks for your PR! By analyzing the history of the files in this pull request, we identified @balloob, @fabaff and @kellerza to be potential reviewers.

@balloob
Copy link
Copy Markdown
Member

balloob commented Jun 25, 2017

I don't think this fixes it. The idea is that you call the method inside the loop, so we get the identifier of the thread. Somehow pytest ends up re-using that thread for doing other things.

I've been keeping a list of ones that fail and wonder if we should just rewrite them as async tests…

@balloob
Copy link
Copy Markdown
Member

balloob commented Jun 25, 2017

So I recall that it tends to happen more frequent when we have tests that are written as standalone methods but are not async. Going to test if I can convert all those to async.

@balloob
Copy link
Copy Markdown
Member

balloob commented Jun 25, 2017

nevermind, we have too many for my last comment to be true.

@balloob
Copy link
Copy Markdown
Member

balloob commented Jun 25, 2017

If we look at the history, it seems to start 20 days ago with build 24930 which is PR #7841. The PR doesn't seem to show any bad coding practice.

Comment thread tests/conftest.py Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not in the thread that is the loop here. We are in a fixture which is executed synchronously. That's why the previous line actually calls loop.run-until_complete

Comment thread tests/common.py Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is inside the async thread because it's run inside a coroutine. However, I do realize now that this might actually not be the case because during the fixture we're calling loop.run_until_complete, which means that we're actually in a thread that executes the fixture!

We need to somehow schedule this so that it gets executed on the loop.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we should remove it regardless, as it's wrong.

@balloob balloob mentioned this pull request Jun 25, 2017
@balloob
Copy link
Copy Markdown
Member

balloob commented Jun 25, 2017

We should consider running our threads with PYTHONASYNCIODEBUG=1. It will be slower but it will also raise when async methods called from a sync context.

The reason I added the thread check originally was that Home Assistant gets into a deadlock when you call run_callback_threadsafe(…).result() from inside the event loop.

@balloob
Copy link
Copy Markdown
Member

balloob commented Jun 25, 2017

Found 3 tests that failed under PYTHONASYNCIODEBUG=1. Fixed two: #8200

@emlove
Copy link
Copy Markdown
Contributor Author

emlove commented Jun 25, 2017

I think the real question which I wasn't able to figure out myself is this: When we execute a pytest style test that isn't in a test case class and is decorated as a coroutine, it seems that it is executing in a different event loop than generated by the fixture for hass.loop. I think we to figure out how we can get a reference to the event loop object calling the test, and use that for hass.

@asyncio.coroutine
def test_example(hass):
    yield from test_method()
    assert True

@emlove
Copy link
Copy Markdown
Contributor Author

emlove commented Jun 25, 2017

If nothing else we can run PYTHONASYNCIODEBUG every now and then to check the tests.

@balloob
Copy link
Copy Markdown
Member

balloob commented Jun 25, 2017

Yeah, I think under the hood something like this happens inside 1 thread:

if async_test:
    loop.run_until_complete(test)
else:
    test

if tear_down:
    tear_down()  # Problem is here, now loop thread and sync tear down thread are the same!

@emlove
Copy link
Copy Markdown
Contributor Author

emlove commented Jun 25, 2017

So now that #8198 is merged, I don't think we're actually depending on this functionality directly in the tests any more.

I'm also of the opinion that if we did need it, we should be running hass.async_start from the tests, since we might be depending on other behavior as well.

Comment thread tests/common.py
def run_loop():
"""Run event loop."""
# pylint: disable=protected-access
loop._thread_ident = threading.get_ident()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is still great to have because this is actually a standalone thread running the loop. And since the check does catch errors, it would be nice to still have.

@emlove emlove changed the title Move thread_ident patch to correct location Remove unnecessary thread_ident assignment Jun 25, 2017
Copy link
Copy Markdown
Member

@balloob balloob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope our CI hurdles will be over now! Thanks

@emlove emlove merged commit 8358542 into home-assistant:dev Jun 25, 2017
@emlove emlove deleted the fix-test-instability branch June 25, 2017 20:39
@balloob balloob mentioned this pull request Jul 1, 2017
dethpickle pushed a commit to dethpickle/home-assistant that referenced this pull request Aug 18, 2017
* Remove mocking of _thread_ident

* Re-add run_loop thread_ident assignment
@home-assistant home-assistant locked and limited conversation to collaborators Oct 20, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants