Skip to content

Commit 5b143df

Browse files
Luca Bianconicmaglie
authored andcommitted
fix: clear discovery manager when spawing a new one
Co-authored-by: Cristian Maglie <[email protected]>
1 parent fed439a commit 5b143df

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

arduino/cores/packagemanager/package_manager.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,11 @@ func (pmb *Builder) BuildIntoExistingPackageManager(target *PackageManager) {
9898
target.tempDir = pmb.tempDir
9999
target.packagesCustomGlobalProperties = pmb.packagesCustomGlobalProperties
100100
target.profile = pmb.profile
101-
target.discoveryManager = pmb.discoveryManager
101+
if target.discoveryManager != nil {
102+
target.discoveryManager.Clear()
103+
} else {
104+
target.discoveryManager = pmb.discoveryManager
105+
}
102106
target.userAgent = pmb.userAgent
103107
}
104108

arduino/discovery/discovery.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ func (disc *PluggableDiscovery) Quit() {
381381
if _, err := disc.waitMessage(time.Second * 5); err != nil {
382382
logrus.Errorf("Quitting discovery %s: %s", disc.id, err)
383383
}
384+
disc.stopSync()
384385
disc.killProcess()
385386
}
386387

arduino/discovery/discoverymanager/discoverymanager.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,15 @@ func (dm *DiscoveryManager) Clear() {
6565
logrus.Infof("Closed and removed discovery %s", d.GetID())
6666
}
6767
}
68+
dm.discoveriesRunning = false
6869
dm.discoveries = map[string]*discovery.PluggableDiscovery{}
6970
}
7071

72+
// AreDiscoveriesRunning returns a boolean representing the running status of discoveries
73+
func (dm *DiscoveryManager) AreDiscoveriesRunning() bool {
74+
return dm.discoveriesRunning
75+
}
76+
7177
// IDs returns the list of discoveries' ids in this DiscoveryManager
7278
func (dm *DiscoveryManager) IDs() []string {
7379
ids := []string{}
@@ -194,12 +200,14 @@ func (dm *DiscoveryManager) startDiscovery(d *discovery.PluggableDiscovery) (dis
194200
return fmt.Errorf("%s: %s", tr("starting discovery %s", d.GetID()), err)
195201
}
196202

197-
go func() {
203+
go func(d *discovery.PluggableDiscovery) {
198204
// Transfer all incoming events from this discovery to the feed channel
199205
for ev := range eventCh {
206+
// here from discovery to discovery manager
200207
dm.feed <- ev
201208
}
202-
}()
209+
logrus.Infof("Discovery event channel closed %s. Exiting goroutine.", d.GetID())
210+
}(d)
203211
return nil
204212
}
205213

commands/instances.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,17 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
261261
},
262262
})
263263
}
264-
264+
var shouldRestartDiscovery bool
265265
{
266266
// We need to rebuild the PackageManager currently in use by this instance
267267
// in case this is not the first Init on this instances, that might happen
268268
// after reinitializing an instance after installing or uninstalling a core.
269269
// If this is not done the information of the uninstall core is kept in memory,
270270
// even if it should not.
271+
272+
// register whether the discoveries are running, if so we need to start them in
273+
// order for the previous watchers to keep receiving events
274+
shouldRestartDiscovery = areDiscoveriesRunning(instance.pm)
271275
pmb, commitPackageManager := instance.pm.NewBuilder()
272276

273277
loadBuiltinTools := func() []error {
@@ -463,6 +467,9 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
463467
responseError(s)
464468
}
465469

470+
if shouldRestartDiscovery {
471+
pme.DiscoveryManager().Start()
472+
}
466473
// Refreshes the locale used, this will change the
467474
// language of the CLI if the locale is different
468475
// after started.
@@ -471,6 +478,19 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
471478
return nil
472479
}
473480

481+
func areDiscoveriesRunning(pm *packagemanager.PackageManager) bool {
482+
if pm == nil {
483+
return false
484+
}
485+
exp, release := pm.NewExplorer()
486+
defer release()
487+
488+
if exp.DiscoveryManager() != nil && exp.DiscoveryManager().AreDiscoveriesRunning() {
489+
return true
490+
}
491+
return false
492+
}
493+
474494
// Destroy FIXMEDOC
475495
func Destroy(ctx context.Context, req *rpc.DestroyRequest) (*rpc.DestroyResponse, error) {
476496
if ok := instances.RemoveID(req.GetInstance().GetId()); !ok {

0 commit comments

Comments
 (0)