Skip to content

Commit 46c3722

Browse files
Merge pull request #4331 from vanhalenar/logfix2
USHIFT-5221: Fix StartupRecorder race condition
2 parents 0b62a2c + d12e534 commit 46c3722

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

pkg/cmd/run.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,6 @@ func RunMicroshift(cfg *config.Config) error {
262262
select {
263263
case <-ready:
264264
startRec.MicroshiftReady()
265-
startRec.OutputData()
266265

267266
os.Setenv("NOTIFY_SOCKET", notifySocket)
268267
if supported, err := daemon.SdNotify(false, daemon.SdNotifyReady); err != nil {

pkg/servicemanager/manager.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ func (m *ServiceManager) Run(ctx context.Context, ready chan<- struct{}, stopped
5959
defer close(stopped)
6060

6161
services := m.services
62+
63+
m.startRec.ServiceCount = len(services)
6264
// No need for topological sorting here as long as we enforce order while adding services.
6365
// services, err := m.topoSort(services)
6466
// if err != nil {

pkg/servicemanager/startuprecorder/startuprecorder.go

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,15 @@ type StartupData struct {
3232
type StartupRecorder struct {
3333
Data StartupData
3434

35-
m sync.Mutex
35+
ServiceCount int
36+
allLogged chan struct{}
37+
m sync.Mutex
3638
}
3739

3840
func New() *StartupRecorder {
39-
return &StartupRecorder{}
41+
return &StartupRecorder{
42+
allLogged: make(chan struct{}),
43+
}
4044
}
4145

4246
func (l *StartupRecorder) ServiceReady(serviceName string, dependencies []string, start time.Time) {
@@ -56,6 +60,10 @@ func (l *StartupRecorder) ServiceReady(serviceName string, dependencies []string
5660
l.m.Lock()
5761
defer l.m.Unlock()
5862
l.Data.Services = append(l.Data.Services, serviceData)
63+
l.ServiceCount--
64+
if l.ServiceCount == 0 {
65+
close(l.allLogged)
66+
}
5967
}
6068

6169
func (l *StartupRecorder) MicroshiftStarts(start time.Time) {
@@ -69,6 +77,8 @@ func (l *StartupRecorder) MicroshiftReady() {
6977
klog.InfoS("MICROSHIFT READY", "since-start", time.Since(l.Data.Microshift.Start))
7078
l.Data.Microshift.Ready = ready
7179
l.Data.Microshift.TimeToReady = ready.Sub(l.Data.Microshift.Start)
80+
81+
l.OutputData()
7282
}
7383

7484
func (l *StartupRecorder) ServicesStart(start time.Time) {
@@ -77,18 +87,25 @@ func (l *StartupRecorder) ServicesStart(start time.Time) {
7787
}
7888

7989
func (l *StartupRecorder) OutputData() {
80-
jsonOutput, err := json.Marshal(l.Data)
81-
if err != nil {
82-
klog.Error("Failed to marshal startup data")
83-
}
84-
85-
klog.Infof("Startup data: %s", string(jsonOutput))
86-
87-
path, ok := os.LookupEnv("STARTUP_LOGS_PATH")
88-
if ok {
89-
err = os.WriteFile(path, jsonOutput, 0600)
90-
if err != nil {
91-
klog.Error("Failed to write startup data to file")
90+
go func() {
91+
select {
92+
case <-l.allLogged:
93+
jsonOutput, err := json.Marshal(l.Data)
94+
if err != nil {
95+
klog.Error("Failed to marshal startup data")
96+
}
97+
98+
klog.Infof("Startup data: %s", string(jsonOutput))
99+
100+
path, ok := os.LookupEnv("STARTUP_LOGS_PATH")
101+
if ok {
102+
err = os.WriteFile(path, jsonOutput, 0600)
103+
if err != nil {
104+
klog.Error("Failed to write startup data to file")
105+
}
106+
}
107+
case <-time.After(30 * time.Second):
108+
klog.Error("StartupRecorder timed out")
92109
}
93-
}
110+
}()
94111
}

0 commit comments

Comments
 (0)