Skip to content

Commit 2c75823

Browse files
Handle reconnect failure in websocket gracefully
1 parent 1636dd1 commit 2c75823

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

protocol/logger/job_logger.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,25 @@ func (logger *WebsocketLivelogger) Connect() error {
7979
ctx, cancel := context.WithTimeout(context.Background(), websocketDialTimeout)
8080
defer cancel()
8181
//nolint:bodyclose // websocket.Dial doesn't return an HTTP response body to close
82-
logger.ws, _, err = websocket.Dial(ctx, feedStreamURL.String(), &websocket.DialOptions{
82+
ws, _, err := websocket.Dial(ctx, feedStreamURL.String(), &websocket.DialOptions{
8383
HTTPClient: logger.Connection.HTTPClient(),
8484
HTTPHeader: http.Header{
8585
"Authorization": []string{"Bearer " + logger.Connection.Token},
8686
"User-Agent": []string{"github-act-runner/1.0.0"},
8787
},
8888
})
89+
// While reconnecting never assign this to null
90+
if ws != nil {
91+
logger.ws = ws
92+
}
8993
return err
9094
}
9195

9296
func (logger *WebsocketLivelogger) SendLog(lines *protocol.TimelineRecordFeedLinesWrapper) error {
97+
// Do not try to send if something is wrong
98+
if logger.ws == nil {
99+
return fmt.Errorf("missing websocket connection")
100+
}
93101
ctx, cancel := context.WithTimeout(context.Background(), websocketMessageTimeout)
94102
defer cancel()
95103
return wsjson.Write(ctx, logger.ws, lines)
@@ -181,7 +189,7 @@ func (logger *WebsocketLiveloggerWithFallback) SendLog(wrapper *protocol.Timelin
181189
type BufferedLiveLogger struct {
182190
LiveLogger
183191
logchan chan *protocol.TimelineRecordFeedLinesWrapper
184-
logfinished chan struct{}
192+
logfinished chan struct{}
185193
}
186194

187195
func (logger *BufferedLiveLogger) sendLogs(logchan chan *protocol.TimelineRecordFeedLinesWrapper, logfinished chan struct{}) {

0 commit comments

Comments
 (0)