Skip to content

Commit

Permalink
Revises language host startup state capture
Browse files Browse the repository at this point in the history
Once an error or address is captured, ServerState.Write() should just do nothing on subsequent writes. In conjunction with if-else-if, it ensures that we only close the Done channel once.
  • Loading branch information
DavidSeptimus committed Aug 19, 2024
1 parent 81149d2 commit f507f2f
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions pkg/k2/language_host/language_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,21 @@ var listenOnPattern = regexp.MustCompile(`(?m)^\s*Listening on (\S+)$`)
var exceptionPattern = regexp.MustCompile(`(?s)(?:^|\n)\s*Exception occurred: (.+)$`)

func (f *ServerState) Write(b []byte) (int, error) {
if f.Address != "" {
if f.Address != "" || f.Error != nil {
return len(b), nil
}

s := string(b)

// captures the gRPC server address
matches := exceptionPattern.FindStringSubmatch(s)
if len(matches) >= 2 {
// captures a fatal error in the language host that occurs before the address is printed to stdout
if matches := exceptionPattern.FindStringSubmatch(s); len(matches) >= 2 {
f.Error = errors.New(strings.TrimSpace(matches[1]))
f.Log.Debug(s)
}

// captures a fatal error in the language host that occurs before the address is printed to stdout
matches = listenOnPattern.FindStringSubmatch(s)
if len(matches) >= 2 {
// address is the first match
close(f.Done)
// captures the gRPC server address
} else if matches := listenOnPattern.FindStringSubmatch(s); len(matches) >= 2 {
f.Address = matches[1]
f.Log.Debugf("Found language host listening on %s", f.Address)
}

if f.Address != "" || f.Error != nil {
close(f.Done)
}
return len(b), nil
Expand Down

0 comments on commit f507f2f

Please sign in to comment.