Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EventService.nextEvent() doesn't break when EventService.breakloop() is called #30

Closed
tomgoff opened this issue Sep 22, 2015 · 1 comment
Assignees
Labels

Comments

@tomgoff
Copy link

tomgoff commented Sep 22, 2015

In src/emanesh/emanesh/events/eventservice.py, EventService.nextEvent() does not currently break when EventService.breakloop() is called. This is different than how EventService.loop() behaves and makes it difficult to gracefully interrupt a call to nextEvent().

One option could be something like the following patch (based on the develop branch), which also removes the unused 'running' variable:

--- a/src/emanesh/emanesh/events/eventservice.py
+++ b/src/emanesh/emanesh/events/eventservice.py
@@ -238,7 +238,6 @@ class EventService:
                     data,_ = self._socket.recvfrom(65535)

                     if not len(data):
-                        running = False
                         break

                     (length,) = struct.unpack_from("!H",data)
@@ -257,11 +256,13 @@ class EventService:
                                 event.sequenceNumber,
                                 tuple(events))

+                elif fd is self._readFd:
+                    break
+
                 elif fd is self._socketOTA:
                     data,_ = self._socketOTA.recvfrom(65535)

                     if not len(data):
-                        running = False
                         break

                     (headerLength,) = struct.unpack_from("!H",data)
@@ -283,6 +284,7 @@ class EventService:
                             otaHeader.sequenceNumber,
                             tuple(events))

+            return (None, None, tuple(events))

     def subscribe(self,eventId,callback):
         self._lock.acquire()
@sgalgano sgalgano added the bug label Sep 22, 2015
@sgalgano sgalgano self-assigned this Sep 22, 2015
sgalgano added a commit that referenced this issue Sep 23, 2015
breakloop(). Added missing logic to use internal pipe to signal
getNext() to return. On cancel via breakloop() or on a socket read
error, getNext() will return (None,None,()) for the UUID, sequence
number and event data, respectively.

Submitted-by: Tom Goff <https://github.com/tomgoff>
See #30
@sgalgano
Copy link
Member

Thanks for the bug report and suggested patch. The committed fix also adds logic to handle socket read errors in a manner similar to loop().

Tested with:

#!/usr/bin/env python

import signal
from emanesh.events import EventService

service = EventService(("224.1.2.8",45703,'lo'),
                       ("224.1.2.8",45702,'lo'));

def handler(signum, frame):
    global service
    service.breakloop()

signal.signal(signal.SIGQUIT, handler)
signal.signal(signal.SIGINT, handler)

while True:
    UUID,seq,events = service.nextEvent()

    if len(events):
        print UUID, seq
    else:
        break

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants