Skip to content

Commit b7a93b9

Browse files
Merge pull request #28426 from deads2k/fix-stream-race
eliminate the closed channel race in pod_log_streamer
2 parents 639414f + 31cd1c0 commit b7a93b9

File tree

1 file changed

+7
-16
lines changed

1 file changed

+7
-16
lines changed

pkg/monitortestlibrary/podaccess/pod_log_streamer.go

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -95,23 +95,13 @@ func (s *PodStreamer) streamLogs(ctx context.Context) (bool, error) {
9595
return false, nil
9696
}
9797

98-
exit := make(chan struct{})
99-
go func(ctx context.Context, currPod *corev1.Pod) {
100-
defer utilruntime.HandleCrash()
101-
defer close(exit)
102-
s.streamLogsReader(ctx, currPod)
103-
}(ctx, currPod)
104-
105-
select {
106-
case <-exit:
107-
return false, nil
108-
case <-ctx.Done():
109-
// wait just a moment so that the streamLogsReader doesn't write to a closed channel
110-
time.Sleep(100 * time.Millisecond)
111-
return false, nil
112-
}
98+
s.streamLogsReader(ctx, currPod)
99+
return false, nil
113100
}
114101

102+
// streamLogsReader will run and block until
103+
// 1. server closes the http connection
104+
// 2. context is closed.
115105
func (s *PodStreamer) streamLogsReader(ctx context.Context, currPod *corev1.Pod) {
116106
caughtUpToLastLine := false
117107
if currPod.UID != s.lastUID {
@@ -131,7 +121,7 @@ func (s *PodStreamer) streamLogsReader(ctx context.Context, currPod *corev1.Pod)
131121
Container: s.containerName,
132122
Follow: true,
133123
Timestamps: true,
134-
}).Stream(context.Background())
124+
}).Stream(ctx)
135125
if err != nil {
136126
s.errs <- LogError{
137127
Pod: currPod,
@@ -140,6 +130,7 @@ func (s *PodStreamer) streamLogsReader(ctx context.Context, currPod *corev1.Pod)
140130
}
141131
return
142132
}
133+
143134
scan := bufio.NewScanner(reader)
144135
for scan.Scan() {
145136
// exit if we have been stopped.

0 commit comments

Comments
 (0)