diff --git a/src/calibre/gui2/viewer/convert_book.py b/src/calibre/gui2/viewer/convert_book.py index cb443c0a75..5022e5036b 100644 --- a/src/calibre/gui2/viewer/convert_book.py +++ b/src/calibre/gui2/viewer/convert_book.py @@ -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) diff --git a/src/calibre/gui2/viewer/ui.py b/src/calibre/gui2/viewer/ui.py index 4efb237f14..4650049bd8 100644 --- a/src/calibre/gui2/viewer/ui.py +++ b/src/calibre/gui2/viewer/ui.py @@ -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) # }}}