Skip to content

Commit

Permalink
Check errfile on setting
Browse files Browse the repository at this point in the history
  • Loading branch information
danijar committed Sep 6, 2024
1 parent a833bcb commit 11b288a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion portal/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '3.1.8'
__version__ = '3.1.9'

import multiprocessing as mp
try:
Expand Down
17 changes: 9 additions & 8 deletions portal/contextlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def setup(
errfile = pathlib.Path(errfile)
assert hasattr(errfile, 'exists') and hasattr(errfile, 'write_text')
self.errfile = errfile
self._check_errfile()

if interval:
assert isinstance(interval, (int, float))
Expand Down Expand Up @@ -144,14 +145,14 @@ def get_children(self, ident=None):
return self.children[ident]

def _watcher(self):
while True:
if self.errfile and self.errfile.exists():
message = f'Shutting down due to error file: {self.errfile}',
print(message, file=sys.stderr)
self.shutdown(2)
break
if self.done.wait(self.interval):
break
while not self.done.wait(self.interval):
self._check_errfile()

def _check_errfile(self):
if self.errfile and self.errfile.exists():
message = f'Shutting down due to error file: {self.errfile}'
print(message, file=sys.stderr)
self.shutdown(exitcode=2)


context = Context()
Expand Down
21 changes: 10 additions & 11 deletions tests/test_errfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,30 @@ def fn():
assert 'line' in content
assert 'in fn' in content

@pytest.mark.parametrize('repeat', range(3))
@pytest.mark.parametrize('repeat', range(5))
def test_sibling_procs(self, tmpdir, repeat):
errfile = pathlib.Path(tmpdir) / 'error'
ready = portal.context.mp.Semaphore(0)
barrier = portal.context.mp.Barrier(3)

def fn1(ready, errfile):
def fn1(barrier, errfile):
portal.setup(errfile=errfile, interval=0.1)
ready.release()
barrier.wait()
raise ValueError('reason')

def fn2(ready, errfile):
def fn2(barrier, errfile):
portal.setup(errfile=errfile, interval=0.1)
ready.release()
barrier.wait()
while True:
time.sleep(0.1)

worker1 = portal.Process(fn1, ready, errfile, name='worker1', start=True)
worker2 = portal.Process(fn2, ready, errfile, name='worker2', start=True)
ready.acquire()
ready.acquire()
worker1 = portal.Process(fn1, barrier, errfile, start=True)
worker2 = portal.Process(fn2, barrier, errfile, start=True)
barrier.wait()
worker1.join()
worker2.join()
content = errfile.read_text()
first_line = content.split('\n')[0]
assert "Error in 'worker1' (ValueError: reason):" == first_line
assert "Error in 'fn1' (ValueError: reason):" == first_line
assert not worker1.running
assert not worker2.running
assert worker1.exitcode == 1
Expand Down

0 comments on commit 11b288a

Please sign in to comment.