Skip to content

Commit af78844

Browse files
committed
gateway: Test concurrent rinfo for main_thread_only
Add test coverage for a bug triggered by pytest-cov when it tried to call _rinfo after the gateway was already busy with a remote_exec call: pytest-dev/pytest-xdist#1071
1 parent 4999a10 commit af78844

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

testing/test_gateway.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,3 +645,47 @@ def test_main_thread_only_concurrent_remote_exec_deadlock(
645645
ch.close()
646646
gw.exit()
647647
gw.join()
648+
649+
650+
def test_concurrent_rinfo(execmodel: gateway_base.ExecModel) -> None:
651+
# Use a temporary Group to force the desired execmodel, since
652+
# groups used by fixtures tend to use the thread execmodel.
653+
group = execnet.Group(execmodel=execmodel.backend)
654+
gw = group.makegateway(f"execmodel={execmodel.backend}//popen")
655+
656+
# For main_thread_only _rinfo will deadlock unless the gateway
657+
# has cached the result earlier, but only if sleep_time
658+
# exceeds the 1 second grace period in the WorkerGateway
659+
# _local_schedulexec method. For other execmodels, a shorter
660+
# sleep_time is sufficient to test concurrent _rinfo.
661+
sleep_time = 1.5 if execmodel.backend == "main_thread_only" else 0.5
662+
try:
663+
if execmodel.backend == "main_thread_only":
664+
# Cache rinfo like pytest-dist does for backward compatibility,
665+
# in order to prevent a deadlock error when pytest-cov requests
666+
# rinfo while the main thread is busy with the pytest-xdist
667+
# WorkerController's remote_exec call.
668+
gw._rinfo()
669+
assert hasattr(gw, "_cache_rinfo")
670+
671+
ch = gw.remote_exec(
672+
"""
673+
import os, time
674+
time.sleep({sleep_time})
675+
channel.send(os.getpid())
676+
""".format(
677+
sleep_time=sleep_time
678+
)
679+
)
680+
rinfo = gw._rinfo()
681+
try:
682+
res = ch.receive()
683+
finally:
684+
ch.close()
685+
ch.waitclose()
686+
687+
assert res == rinfo.pid
688+
finally:
689+
gw.exit()
690+
gw.join()
691+
group.terminate(0.5)

0 commit comments

Comments
 (0)