Ensure worker process is killed on viewer exit

This commit is contained in:
Kovid Goyal 2019-11-06 07:19:58 +05:30
parent bcc9f16df2
commit 2da1f63821
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 33 additions and 12 deletions

View File

@ -129,19 +129,37 @@ class ConversionFailure(ValueError):
self, 'Failed to convert book: {} with error:\n{}'.format(book_path, worker_output))
running_workers = []
def clean_running_workers():
for p in running_workers:
if p.poll() is None:
p.kill()
del running_workers[:]
def do_convert(path, temp_path, key, instance):
tdir = os.path.join(temp_path, instance['path'])
with TemporaryFile('log.txt') as logpath:
with open(logpath, 'w+b') as logf:
p = start_pipe_worker('from calibre.srv.render_book import viewer_main; viewer_main()', stdout=logf, stderr=logf)
p.stdin.write(msgpack_dumps((
path, tdir, {'size': instance['file_size'], 'mtime': instance['file_mtime'], 'hash': key},
)))
p.stdin.close()
if p.wait() != 0:
with lopen(logpath, 'rb') as logf:
worker_output = logf.read().decode('utf-8', 'replace')
raise ConversionFailure(path, worker_output)
p = None
try:
with TemporaryFile('log.txt') as logpath:
with open(logpath, 'w+b') as logf:
p = start_pipe_worker('from calibre.srv.render_book import viewer_main; viewer_main()', stdout=logf, stderr=logf)
running_workers.append(p)
p.stdin.write(msgpack_dumps((
path, tdir, {'size': instance['file_size'], 'mtime': instance['file_mtime'], 'hash': key},
)))
p.stdin.close()
if p.wait() != 0:
with lopen(logpath, 'rb') as logf:
worker_output = logf.read().decode('utf-8', 'replace')
raise ConversionFailure(path, worker_output)
finally:
try:
running_workers.remove(p)
except Exception:
pass
size = 0
for f in walk(tdir):
size += os.path.getsize(f)

View File

@ -28,7 +28,9 @@ from calibre.gui2.viewer.annotations import (
merge_annotations, parse_annotations, save_annots_to_epub, serialize_annotations
)
from calibre.gui2.viewer.bookmarks import BookmarkManager
from calibre.gui2.viewer.convert_book import prepare_book, update_book
from calibre.gui2.viewer.convert_book import (
clean_running_workers, prepare_book, update_book
)
from calibre.gui2.viewer.lookup import Lookup
from calibre.gui2.viewer.overlay import LoadingOverlay
from calibre.gui2.viewer.toc import TOC, TOCSearch, TOCView
@ -495,5 +497,6 @@ class EbookViewer(MainWindow):
except Exception:
import traceback
traceback.print_exc()
clean_running_workers()
return MainWindow.closeEvent(self, ev)
# }}}