@@ -95,7 +95,6 @@ func New(id string, args ...string) (*PluggableDiscovery, error) {
9595 process : proc ,
9696 incomingMessagesChan : messageChan ,
9797 outgoingCommandsPipe : stdin ,
98- alive : true ,
9998 }
10099 go disc .jsonDecodeLoop (stdout , messageChan )
101100 return disc , nil
@@ -118,6 +117,7 @@ func (disc *PluggableDiscovery) jsonDecodeLoop(in io.Reader, outChan chan<- *dis
118117 disc .incomingMessagesError = err
119118 disc .statusMutex .Unlock ()
120119 close (outChan )
120+ // TODO: Try restarting process some times before closing it completely
121121 }
122122
123123 for {
@@ -202,6 +202,9 @@ func (disc *PluggableDiscovery) runProcess() error {
202202 if err := disc .process .Start (); err != nil {
203203 return err
204204 }
205+ disc .statusMutex .Lock ()
206+ defer disc .statusMutex .Unlock ()
207+ disc .alive = true
205208 return nil
206209}
207210
@@ -257,6 +260,13 @@ func (disc *PluggableDiscovery) Stop() error {
257260 } else if msg .Message != "OK" || msg .Error {
258261 return errors .Errorf ("command failed: %s" , msg .Message )
259262 }
263+ disc .statusMutex .Lock ()
264+ defer disc .statusMutex .Unlock ()
265+ if disc .eventChan != nil {
266+ close (disc .eventChan )
267+ disc .eventChan = nil
268+ }
269+ disc .eventsMode = false
260270 return nil
261271}
262272
@@ -272,6 +282,13 @@ func (disc *PluggableDiscovery) Quit() error {
272282 } else if msg .Message != "OK" || msg .Error {
273283 return errors .Errorf ("command failed: %s" , msg .Message )
274284 }
285+ disc .statusMutex .Lock ()
286+ defer disc .statusMutex .Unlock ()
287+ if disc .eventChan != nil {
288+ close (disc .eventChan )
289+ disc .eventChan = nil
290+ }
291+ disc .alive = false
275292 return nil
276293}
277294
@@ -296,9 +313,14 @@ func (disc *PluggableDiscovery) List() ([]*Port, error) {
296313// The event channel must be consumed as quickly as possible since it may block the
297314// discovery if it becomes full. The channel size is configurable.
298315func (disc * PluggableDiscovery ) EventChannel (size int ) <- chan * Event {
299- c := make (chan * Event , size )
300316 disc .statusMutex .Lock ()
301317 defer disc .statusMutex .Unlock ()
318+ if disc .eventChan != nil {
319+ // In case there is already an existing event channel in use we close it
320+ // before creating a new one.
321+ close (disc .eventChan )
322+ }
323+ c := make (chan * Event , size )
302324 disc .eventChan = c
303325 return c
304326}
0 commit comments