Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7ed8d51

Browse files
authored
fix(api): safely stop engine (#5403)
* fix(api): safely stop engine * fix: race condition and concurrent map writes
1 parent 71c9e47 commit 7ed8d51

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

engine/cmd_start.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os/signal"
99
"sort"
1010
"strings"
11+
"sync"
1112
"syscall"
1213
"time"
1314

@@ -275,8 +276,10 @@ See $ engine config command for more details.
275276
return serviceConfs[i].arg < serviceConfs[j].arg
276277
})
277278

279+
var wg sync.WaitGroup
278280
//Configure the services
279-
for _, s := range serviceConfs {
281+
for i := range serviceConfs {
282+
s := serviceConfs[i]
280283
if err := s.service.ApplyConfiguration(s.cfg); err != nil {
281284
sdk.Exit("Unable to init service %s: %v", s.arg, err)
282285
}
@@ -294,14 +297,20 @@ See $ engine config command for more details.
294297
sdk.Exit("Unable to start tracing exporter: %v", err)
295298
}
296299

297-
go start(ctx, s.service, s.cfg, s.arg)
300+
wg.Add(1)
301+
go func(srv serviceConf) {
302+
start(ctx, srv.service, srv.cfg, srv.arg)
303+
wg.Done()
304+
}(s)
298305

299306
// Stupid trick: when API is starting wait a bit before start the other
300307
if s.arg == "API" || s.arg == "api" {
301308
time.Sleep(2 * time.Second)
302309
}
303310
}
304311

312+
wg.Wait()
313+
305314
//Wait for the end
306315
<-ctx.Done()
307316
if ctx.Err() != nil {
@@ -312,7 +321,7 @@ See $ engine config command for more details.
312321

313322
func start(c context.Context, s service.Service, cfg interface{}, serviceName string) {
314323
if err := serve(c, s, serviceName, cfg); err != nil {
315-
sdk.Exit("Service has been stopped: %s %+v", serviceName, err)
324+
fmt.Printf("Service has been stopped: %s %+v", serviceName, err)
316325
}
317326
}
318327

0 commit comments

Comments
 (0)