More immediate resource closures

This commit is contained in:
Kovid Goyal 2023-10-17 05:45:39 +05:30
parent 5fc243a93d
commit a48a0fc52f
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 13 additions and 5 deletions

View File

@ -86,7 +86,8 @@ def get_split_book(fmt='epub'):
src = os.path.join(os.path.dirname(__file__), 'split.html') src = os.path.join(os.path.dirname(__file__), 'split.html')
if needs_recompile(ans, src): if needs_recompile(ans, src):
x = src.replace('split.html', 'index.html') x = src.replace('split.html', 'index.html')
raw = open(src, 'rb').read().decode('utf-8') with open(src, 'rb') as sf:
raw = sf.read().decode('utf-8')
try: try:
with open(x, 'wb') as f: with open(x, 'wb') as f:
f.write(raw.encode('utf-8')) f.write(raw.encode('utf-8'))

View File

@ -97,6 +97,8 @@ class Overseer:
for w in self.workers.values(): for w in self.workers.values():
w.stdin.write(b'EXIT:0\n') w.stdin.write(b'EXIT:0\n')
w.stdin.flush() w.stdin.flush()
w.stdin.close()
w.stdout.close()
for w in self.workers.values(): for w in self.workers.values():
if self.safe_wait(w, 0.2) is None: if self.safe_wait(w, 0.2) is None:
w.terminate() w.terminate()

View File

@ -87,10 +87,14 @@ def lock_file(path, timeout=15, sleep_time=0.2):
timeout, sleep_time, windows_open, windows_retry, path timeout, sleep_time, windows_open, windows_retry, path
) )
f = unix_open(path) f = unix_open(path)
retry_for_a_time( try:
timeout, sleep_time, fcntl.flock, unix_retry, retry_for_a_time(
f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB timeout, sleep_time, fcntl.flock, unix_retry,
) f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB
)
except Exception:
f.close()
raise
return f return f

View File

@ -109,6 +109,7 @@ class IPCLockTest(unittest.TestCase):
finally: finally:
if child.poll() is None: if child.poll() is None:
child.kill() child.kill()
child.wait()
def test_exclusive_file_other_process_clean(self): def test_exclusive_file_other_process_clean(self):
self.run_other_ef_op(True) self.run_other_ef_op(True)