Skip to content

Fixed start/stop handling of Discovery #1346

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

Merged
merged 1 commit into from
Jul 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions arduino/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ func New(id string, args ...string) (*PluggableDiscovery, error) {
process: proc,
incomingMessagesChan: messageChan,
outgoingCommandsPipe: stdin,
alive: true,
}
go disc.jsonDecodeLoop(stdout, messageChan)
return disc, nil
Expand All @@ -118,6 +117,7 @@ func (disc *PluggableDiscovery) jsonDecodeLoop(in io.Reader, outChan chan<- *dis
disc.incomingMessagesError = err
disc.statusMutex.Unlock()
close(outChan)
// TODO: Try restarting process some times before closing it completely
}

for {
Expand Down Expand Up @@ -202,6 +202,9 @@ func (disc *PluggableDiscovery) runProcess() error {
if err := disc.process.Start(); err != nil {
return err
}
disc.statusMutex.Lock()
defer disc.statusMutex.Unlock()
disc.alive = true
return nil
}

Expand Down Expand Up @@ -257,6 +260,13 @@ func (disc *PluggableDiscovery) Stop() error {
} else if msg.Message != "OK" || msg.Error {
return errors.Errorf("command failed: %s", msg.Message)
}
disc.statusMutex.Lock()
defer disc.statusMutex.Unlock()
if disc.eventChan != nil {
close(disc.eventChan)
disc.eventChan = nil
}
disc.eventsMode = false
return nil
}

Expand All @@ -272,6 +282,13 @@ func (disc *PluggableDiscovery) Quit() error {
} else if msg.Message != "OK" || msg.Error {
return errors.Errorf("command failed: %s", msg.Message)
}
disc.statusMutex.Lock()
defer disc.statusMutex.Unlock()
if disc.eventChan != nil {
close(disc.eventChan)
disc.eventChan = nil
}
disc.alive = false
return nil
}

Expand All @@ -296,9 +313,14 @@ func (disc *PluggableDiscovery) List() ([]*Port, error) {
// The event channel must be consumed as quickly as possible since it may block the
// discovery if it becomes full. The channel size is configurable.
func (disc *PluggableDiscovery) EventChannel(size int) <-chan *Event {
c := make(chan *Event, size)
disc.statusMutex.Lock()
defer disc.statusMutex.Unlock()
if disc.eventChan != nil {
// In case there is already an existing event channel in use we close it
// before creating a new one.
close(disc.eventChan)
}
c := make(chan *Event, size)
disc.eventChan = c
return c
}
Expand Down