From aaeb4f69f9fb9c531a7308af5adbcbe4d4cdfd39 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Tue, 3 Aug 2010 11:03:45 -0400 Subject: [PATCH] Fix run cleanup if the UI ends without clearing non-Hooke msgs from the Queue. If the UI closes before the last command finishes (e.g. if bin/hooke is run in a pipe), there may be cruft (e.g. CommandExit instances) left in the engine-to-UI queue before the Hooke instance that the CommandEngine appends on closing. The new implementation keeps digging through the queue until it finds a Hooke instance, where the old implementation only looked at the first message that came off the queue. Also added some useful debugging logs for UI<->Engine communication. --- hooke/engine.py | 8 ++++++++ hooke/hooke.py | 10 ++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/hooke/engine.py b/hooke/engine.py index b91551c..ca4461d 100644 --- a/hooke/engine.py +++ b/hooke/engine.py @@ -20,8 +20,11 @@ :class:`hooke.command.Command`\s. """ +import logging + from .ui import CloseEngine, CommandMessage + class CommandEngine (object): def run(self, hooke, ui_to_command_queue, command_to_ui_queue): """Get a :class:`hooke.ui.QueueMessage` from the incoming @@ -37,11 +40,16 @@ class CommandEngine (object): `command_to_ui_queue`, at which point the `CommandEngine` will be ready to receive the next :class:`hooke.ui.QueueMessage`. """ + log = logging.getLogger('hooke') while True: + log.debug('engine waiting for command') msg = ui_to_command_queue.get() if isinstance(msg, CloseEngine): command_to_ui_queue.put(hooke) + log.debug( + 'engine closing, placed hooke instance in return queue') break assert isinstance(msg, CommandMessage), type(msg) + log.debug('engine running %s' % msg.command.name) msg.command.run(hooke, ui_to_command_queue, command_to_ui_queue, **msg.arguments) diff --git a/hooke/hooke.py b/hooke/hooke.py index 1923d22..c2ef159 100644 --- a/hooke/hooke.py +++ b/hooke/hooke.py @@ -59,6 +59,7 @@ import logging.config import multiprocessing import optparse import os.path +import Queue import unittest import StringIO import sys @@ -156,9 +157,14 @@ class HookeRunner (object): return (ui_to_command, command_to_ui, command) def _cleanup_run(self, ui_to_command, command_to_ui, command): + log = logging.getLogger('hooke') + log.debug('cleanup sending CloseEngine') ui_to_command.put(ui.CloseEngine()) - hooke = command_to_ui.get() - assert isinstance(hooke, Hooke) + hooke = None + while not isinstance(hooke, Hooke): + log.debug('cleanup waiting for Hooke instance from the engine.') + hooke = command_to_ui.get(block=True) + log.debug('cleanup got %s instance' % type(hooke)) command.join() return hooke -- 2.26.2