You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How do I catch errors in tasks started from fixtures and abort tests(s) using cancel scope? #120
I'm not quite sure I'm following the details here, but this looks like a bug -- if a task in a fixture raises an exception then pytest-trio should automatically cancel everything and give you a traceback.
The logic in the statements line 121 - 128 sound really solid to me and I can't disagree with any of it.
In this specific case, the part if the test is running, we cancel it. seems the most relevant?
Lets see if, by cancel it, we can agree that would mean applying a cancel scope. (Let's keep aside for a minute, that right now,
no such cancel scope is explicitly applied - though it might something we work towards if we agree on this)
What seems to be happening here: the cancel scope never affects the trio.Event().wait() in hanging_fixture.
As a result, hanging_fixture never tears down - ergo the test never ends.
Hence, a fix would be to apply a cancel scope, perhaps explicitly on explicit dependencies like the one built in this case - we would have to, think of a way to either cancel the trio.Event().wait() or the fixture that contains it.
If this does not make sense, let's figure out where we differ.
What that comment is saying is: we only cancel the test function itself, the function called test_whatever. The fixtures have to finish before the test function can run. And one of the fixtures is hanging, so the test function never runs, so it never gets cancelled.
I think we can tweak this slightly: if the test hasn't started yet, we don't start it, and we treat it like it's failed and start unwinding fixtures backwards down the dependency tree. I guess I just never thought about this case :-)
And one of the fixtures is hanging, so the test function never runs, so it never gets cancelled.
Ah! This is what I was missing. Logically it makes sense.
I think we can tweak this slightly: if the test hasn't started yet, we don't start it, and we treat it like it's failed and start unwinding fixtures backwards down the dependency tree. I guess I just never thought about this case :-)
Makes sense!
and we treat it like it's failed
Sure. I mean if you wanted to be picky, we can mark it as skipped with a reason "Fixtures failed" or similar (because we don't have information whether the test actually failed). However, this could be something I add on later after I work with you to figure out the fundamental cancel mechanics.
So, would it be fair to say the "blocker" for this issue is that trio.Event().wait() blocks by design but there's no way to cancel it once it's clear to us that will never unblock (because the Fixture that's responsible for the unblock failed)?
So, would it be fair to say the "blocker" for this issue is that trio.Event().wait() blocks by design but there's no way to cancel it once it's clear to us that will never unblock (because the Fixture that's responsible for the unblock failed)?
No, we can cancel it. We just need to wire things up in pytest-trio so that it does cancel it under this circumstance :-).
I spent a few minutes looking at this yesterday and didn't get terribly far. The concurrent fixture setup code is hard to understand. I guess another way to solve this would be to remove the concurrent setup -- so then the first fixture would fail, and the second would never start (#57).
I can continue discussing this on #57 but needed clarity on 2 things:
I guess another way to solve this would be to remove the concurrent setup -- so then the first fixture would fail, and the second would never start
What if we could have both. Those tests that don't need concurrent fixtures (because of interdependencies) pass in a --no-concurrent-fixtures option to pytest-trio and in that case, fixtures run in sequence?
Would running fixtures run in sequence actually help us in this specific case where the setup of one fixture depends on the setup of another but they are not torn down yet - they just enter their "yield" phase at which point the actual test runs (so the fixtures in theory haven't entered their teardown yet)?
There's however something, that I think is being discussed at #110
This is my own take on it.
Given a test file test_x.py:
# -*- coding: utf-8 -*-importtrioasyncdeftest_passes_shorter(boom):
awaittrio.sleep(boom-2) # will pass as the nursery is cancelled before the exception is thrown
and a conftest.py:
# -*- coding: utf-8 -*-frompytest_trio.enable_trio_modeimport*fromfunctoolsimportpartialimporttrioimportpytestfromloguruimportloggerBLOCKING_PERIOD=5asyncdefrun_boom(task_status=trio.TASK_STATUS_IGNORED):
logger.error("run_boom:start")
task_status.started(BLOCKING_PERIOD)
awaittrio.sleep(BLOCKING_PERIOD)
logger.error("run_boom:hitting-Exception")
raiseException() # This gets thrown as expected IFF this is still running (which is when the tests aren't done yet/still running themselves)@pytest.fixtureasyncdefboom(nursery):
logger.debug("boom:start")
time_start=trio.current_time()
blocking_period=awaitnursery.start(partial(run_boom))
yieldblocking_periodtime_end=trio.current_time()
logger.debug(f"boom:blocking_period: {blocking_period} -> {time_end-time_start}")
logger.debug("boom:end")
Activity
njsmith commentedon May 3, 2021
I'm not quite sure I'm following the details here, but this looks like a bug -- if a task in a fixture raises an exception then pytest-trio should automatically cancel everything and give you a traceback.
njsmith commentedon May 3, 2021
For reference, running with
pytest -vs, I get:...and then it hangs.
njsmith commentedon May 3, 2021
Here's a self-contained minimal version that still demonstrates the hang:
Output:
njsmith commentedon May 3, 2021
Oh hmm, so apparently I set things up this way on purpose, at some point:
pytest-trio/pytest_trio/plugin.py
Lines 121 to 128 in 59da535
Maybe we should revisit that?
gc-ss commentedon May 4, 2021
The logic in the statements line 121 - 128 sound really solid to me and I can't disagree with any of it.
In this specific case, the part
if the test is running, we cancel it.seems the most relevant?Lets see if, by
cancel it, we can agree that would mean applying a cancel scope. (Let's keep aside for a minute, that right now,no such cancel scope is explicitly applied - though it might something we work towards if we agree on this)
What seems to be happening here: the cancel scope never affects the
trio.Event().wait()inhanging_fixture.As a result,
hanging_fixturenever tears down - ergo the test never ends.Hence, a fix would be to apply a cancel scope, perhaps explicitly on explicit dependencies like the one built in this case - we would have to, think of a way to either cancel the
trio.Event().wait()or the fixture that contains it.If this does not make sense, let's figure out where we differ.
njsmith commentedon May 4, 2021
What that comment is saying is: we only cancel the test function itself, the function called
test_whatever. The fixtures have to finish before the test function can run. And one of the fixtures is hanging, so the test function never runs, so it never gets cancelled.I think we can tweak this slightly: if the test hasn't started yet, we don't start it, and we treat it like it's failed and start unwinding fixtures backwards down the dependency tree. I guess I just never thought about this case :-)
gc-ss commentedon May 4, 2021
Ah! This is what I was missing. Logically it makes sense.
Makes sense!
Sure. I mean if you wanted to be picky, we can mark it as skipped with a reason "Fixtures failed" or similar (because we don't have information whether the test actually failed). However, this could be something I add on later after I work with you to figure out the fundamental cancel mechanics.
So, would it be fair to say the "blocker" for this issue is that
trio.Event().wait()blocks by design but there's no way to cancel it once it's clear to us that will never unblock (because the Fixture that's responsible for the unblock failed)?njsmith commentedon May 4, 2021
No, we can cancel it. We just need to wire things up in pytest-trio so that it does cancel it under this circumstance :-).
I spent a few minutes looking at this yesterday and didn't get terribly far. The concurrent fixture setup code is hard to understand. I guess another way to solve this would be to remove the concurrent setup -- so then the first fixture would fail, and the second would never start (#57).
gc-ss commentedon May 4, 2021
I can continue discussing this on #57 but needed clarity on 2 things:
What if we could have both. Those tests that don't need concurrent fixtures (because of interdependencies) pass in a
--no-concurrent-fixturesoption topytest-trioand in that case, fixtures run in sequence?When fixture setup crashes, immediately cancel all other fixture setups
njsmith commentedon May 6, 2021
Looked at this again and it wasn't as tricky as I thought. (At least, I think it's not! We'll see if anyone pokes any holes in it :-).) Anyway: #121
gc-ss commentedon May 7, 2021
Neither was I. I will try harder over the next few weeks though!
gc-ss commentedon May 7, 2021
There's however something, that I think is being discussed at #110
This is my own take on it.
Given a test file test_x.py:
and a conftest.py:
The test will always pass.