Skip to content

Commit

Permalink
fix: clear discovery manager when spawing a new one
Browse files Browse the repository at this point in the history
Co-authored-by: Cristian Maglie <c.maglie@arduino.cc>
  • Loading branch information
Luca Bianconi and cmaglie committed Feb 3, 2023
1 parent 2b8d6d7 commit 6074df3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
6 changes: 5 additions & 1 deletion arduino/cores/packagemanager/package_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ func (pmb *Builder) BuildIntoExistingPackageManager(target *PackageManager) {
target.tempDir = pmb.tempDir
target.packagesCustomGlobalProperties = pmb.packagesCustomGlobalProperties
target.profile = pmb.profile
target.discoveryManager = pmb.discoveryManager
if target.discoveryManager != nil {
target.discoveryManager.Clear()
} else {
target.discoveryManager = pmb.discoveryManager
}
target.userAgent = pmb.userAgent
}

Expand Down
1 change: 1 addition & 0 deletions arduino/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ func (disc *PluggableDiscovery) Quit() {
if _, err := disc.waitMessage(time.Second * 5); err != nil {
logrus.Errorf("Quitting discovery %s: %s", disc.id, err)
}
disc.stopSync()
disc.killProcess()
}

Expand Down
12 changes: 10 additions & 2 deletions arduino/discovery/discoverymanager/discoverymanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,15 @@ func (dm *DiscoveryManager) Clear() {
logrus.Infof("Closed and removed discovery %s", d.GetID())
}
}
dm.discoveriesRunning = false
dm.discoveries = map[string]*discovery.PluggableDiscovery{}
}

// AreDiscoveriesRunning returns a boolean representing the running status of discoveries
func (dm *DiscoveryManager) AreDiscoveriesRunning() bool {
return dm.discoveriesRunning
}

// IDs returns the list of discoveries' ids in this DiscoveryManager
func (dm *DiscoveryManager) IDs() []string {
ids := []string{}
Expand Down Expand Up @@ -194,12 +200,14 @@ func (dm *DiscoveryManager) startDiscovery(d *discovery.PluggableDiscovery) (dis
return fmt.Errorf("%s: %s", tr("starting discovery %s", d.GetID()), err)
}

go func() {
go func(d *discovery.PluggableDiscovery) {
// Transfer all incoming events from this discovery to the feed channel
for ev := range eventCh {
// here from discovery to discovery manager
dm.feed <- ev
}
}()
logrus.Infof("Discovery event channel closed %s. Exiting goroutine.", d.GetID())
}(d)
return nil
}

Expand Down
22 changes: 21 additions & 1 deletion commands/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,17 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
},
})
}

var shouldRestartDiscovery bool
{
// We need to rebuild the PackageManager currently in use by this instance
// in case this is not the first Init on this instances, that might happen
// after reinitializing an instance after installing or uninstalling a core.
// If this is not done the information of the uninstall core is kept in memory,
// even if it should not.

// register whether the discoveries are running, if so we need to start them in
// order for the previous watchers to keep receiving events
shouldRestartDiscovery = areDiscoveriesRunning(instance.pm)
pmb, commitPackageManager := instance.pm.NewBuilder()

loadBuiltinTools := func() []error {
Expand Down Expand Up @@ -443,6 +447,9 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
responseError(s)
}

if shouldRestartDiscovery {
pme.DiscoveryManager().Start()
}
// Refreshes the locale used, this will change the
// language of the CLI if the locale is different
// after started.
Expand All @@ -451,6 +458,19 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
return nil
}

func areDiscoveriesRunning(pm *packagemanager.PackageManager) bool {
if pm == nil {
return false
}
exp, release := pm.NewExplorer()
defer release()

if exp.DiscoveryManager() != nil && exp.DiscoveryManager().AreDiscoveriesRunning() {
return true
}
return false
}

// Destroy FIXMEDOC
func Destroy(ctx context.Context, req *rpc.DestroyRequest) (*rpc.DestroyResponse, error) {
if ok := instances.RemoveID(req.GetInstance().GetId()); !ok {
Expand Down

0 comments on commit 6074df3

Please sign in to comment.