vici: Catch Python GeneratorExit to properly cancel streamed event iteration
authorMartin Willi <martin@revosec.ch>
Mon, 9 Mar 2015 11:06:38 +0000 (12:06 +0100)
committerMartin Willi <martin@revosec.ch>
Wed, 18 Mar 2015 12:59:14 +0000 (13:59 +0100)
src/libcharon/plugins/vici/README.md
src/libcharon/plugins/vici/python/vici/session.py

index 655f749..0ce4271 100644 (file)
@@ -909,6 +909,11 @@ event:
                for key in conn:
                        print key
 
+Please note that if the returned generator is not iterated completely, it must
+be closed using _close()_. This is implicitly done when breaking from a loop,
+but an explicit call may be required when directly iterating the generator with
+_next()_.
+
 ## Sorting in dictionaries ##
 
 In VICI, in some message trees the order of objects in dictionary matters. In
index 65b89b5..da79ecd 100644 (file)
@@ -281,10 +281,16 @@ class SessionHandler(object):
         # issue command, and read any event messages
         packet = Packet.request(command, message)
         self.transport.send(packet)
+        exited = False
         while True:
             response = Packet.parse(self.transport.receive())
             if response.response_type == Packet.EVENT:
-                yield Message.deserialize(response.payload)
+                if not exited:
+                    try:
+                        yield Message.deserialize(response.payload)
+                    except GeneratorExit:
+                        exited = True
+                        pass
             else:
                 break