Skip to content

Commit

Permalink
Close hanging file handles with a CM
Browse files Browse the repository at this point in the history
  • Loading branch information
webknjaz committed Feb 12, 2021
1 parent 8880381 commit 438d329
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
12 changes: 8 additions & 4 deletions magicbus/plugins/opsys.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ def ENTER(self):
if self.finalized:
self.bus.log('PID %r already written to %r.' % (pid, self.pidfile))
else:
open(self.pidfile, 'wb').write(ntob('%s' % pid, 'utf8'))
with open(self.pidfile, 'wb') as pid_file:
pid_file.write(ntob('%s' % pid, 'utf8'))
self.bus.log('PID %r written to %r.' % (pid, self.pidfile))
self.finalized = True
ENTER.priority = 70
Expand All @@ -255,9 +256,12 @@ def wait(self, timeout=None, poll_interval=0.1):
"""
starttime = time.time()
while timeout is None or time.time() - starttime <= timeout:
if os.path.exists(self.pidfile):
return int(open(self.pidfile, 'rb').read())
time.sleep(poll_interval)
if not os.path.exists(self.pidfile):
time.sleep(poll_interval)
continue

with open(self.pidfile, 'rb') as pid_file:
return int(pid_file.read())

def join(self, timeout=None, poll_interval=0.1):
"""Return when the PID file does not exist, or the timeout expires."""
Expand Down
4 changes: 3 additions & 1 deletion magicbus/test/test_opsys.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ def test_daemonize(self):
resp = service.do_GET('/pid')
assert resp.status == 200
page_pid = int(resp.read())
assert ntob(str(page_pid)) == open(pidfile.pidfile, 'rb').read()
with open(pidfile.pidfile, 'rb') as pid_file:
actual_pid = pid_file.read()
assert ntob(str(page_pid)) == actual_pid
finally:
# Shut down the spawned process
service.do_GET('/exit')
Expand Down

0 comments on commit 438d329

Please sign in to comment.