Skip to content

Commit 933086e

Browse files
committed
Use execnet main_thread_only execmodel
Use the execnet main_thread_only execmodel so that code which expects to run in the main thread will just work. This execmodel has been merged to the execnet master branch via pytest-dev/execnet#243, so this patch should not be merged until there is a released version of execnet supporting the main_thread_only execmodel. Closes: #620
1 parent 4720808 commit 933086e

File tree

5 files changed

+12
-5
lines changed

5 files changed

+12
-5
lines changed

changelog/620.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use the ``execnet`` new ``main_thread_only`` "execmodel" so that code which expects to only run in the main thread will now work as expected.

src/xdist/looponfail.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def trace(self, *args):
7878
print("RemoteControl:", msg)
7979

8080
def initgateway(self):
81-
return execnet.makegateway("popen")
81+
return execnet.makegateway("execmodel=main_thread_only//popen")
8282

8383
def setup(self):
8484
if hasattr(self, "gateway"):

src/xdist/workermanage.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ def __init__(self, config, specs=None, defaultchdir="pyexecnetcache") -> None:
4646
self.testrunuid = self.config.getoption("testrunuid")
4747
if self.testrunuid is None:
4848
self.testrunuid = uuid.uuid4().hex
49-
self.group = execnet.Group()
49+
self.group = execnet.Group(execmodel="main_thread_only")
5050
if specs is None:
5151
specs = self._getxspecs()
5252
self.specs = []
5353
for spec in specs:
5454
if not isinstance(spec, execnet.XSpec):
5555
spec = execnet.XSpec(spec)
56+
if getattr(spec, "execmodel", None) != "main_thread_only":
57+
spec = execnet.XSpec(f"execmodel=main_thread_only//{spec}")
5658
if not spec.chdir and not spec.popen:
5759
spec.chdir = defaultchdir
5860
self.group.allocate_id(spec)
@@ -73,6 +75,10 @@ def setup_nodes(self, putevent):
7375
return [self.setup_node(spec, putevent) for spec in self.specs]
7476

7577
def setup_node(self, spec, putevent):
78+
if not isinstance(spec, execnet.XSpec):
79+
spec = execnet.XSpec(spec)
80+
if getattr(spec, "execmodel", None) != "main_thread_only":
81+
spec = execnet.XSpec(f"execmodel=main_thread_only//{spec}")
7682
gw = self.group.makegateway(spec)
7783
self.config.hook.pytest_xdist_newgateway(gateway=gw)
7884
self.rsync_roots(gw)

testing/test_remote.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def __init__(self, request, pytester: pytest.Pytester) -> None:
3939
def setup(self) -> None:
4040
self.pytester.chdir()
4141
# import os ; os.environ['EXECNET_DEBUG'] = "2"
42-
self.gateway = execnet.makegateway()
42+
self.gateway = execnet.makegateway("execmodel=main_thread_only//popen")
4343
self.config = config = self.pytester.parseconfigure()
4444
putevent = self.events.put if self.use_callback else None
4545

testing/test_workermanage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def test_popen_makegateway_events(
7777
assert len(call.specs) == 2
7878

7979
call = hookrecorder.popcall("pytest_xdist_newgateway")
80-
assert call.gateway.spec == execnet.XSpec("popen")
80+
assert call.gateway.spec == execnet.XSpec("execmodel=main_thread_only//popen")
8181
assert call.gateway.id == "gw0"
8282
call = hookrecorder.popcall("pytest_xdist_newgateway")
8383
assert call.gateway.id == "gw1"
@@ -167,7 +167,7 @@ def test_hrsync_filter(self, source: Path, dest: Path) -> None:
167167
assert names == {"dir", "file.txt", "somedir"}
168168

169169
def test_hrsync_one_host(self, source: Path, dest: Path) -> None:
170-
gw = execnet.makegateway("popen//chdir=%s" % dest)
170+
gw = execnet.makegateway("execmodel=main_thread_only//popen//chdir=%s" % dest)
171171
finished = []
172172
rsync = HostRSync(source)
173173
rsync.add_target_host(gw, finished=lambda: finished.append(1))

0 commit comments

Comments
 (0)