Skip to content

Commit

Permalink
fix(lifecycle): adapt close channel handling
Browse files Browse the repository at this point in the history
  • Loading branch information
steebchen committed Feb 21, 2024
1 parent 775eee3 commit 4ef3e14
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
9 changes: 5 additions & 4 deletions engine/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import (
)

func (e *QueryEngine) Connect() error {
e.closed = make(chan interface{})

success := false
go func() {
defer func() {
if !success {
e.closed <- struct{}{}
close(e.closed)
}
}()

Expand Down Expand Up @@ -80,7 +82,7 @@ func (e *QueryEngine) Disconnect() error {
}
}

e.closed <- struct{}{}
close(e.closed)

logger.Debug.Printf("disconnected.")
return nil
Expand Down Expand Up @@ -238,7 +240,6 @@ func (e *QueryEngine) spawn(file string) error {
e.cmd.Stdout = os.Stdout

e.onEngineError = make(chan string)
e.closed = make(chan interface{})

if err := e.streamStderr(e.cmd, e.onEngineError); err != nil {
return fmt.Errorf("setup stream: %w", err)
Expand Down
3 changes: 3 additions & 0 deletions engine/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"log"
"os/exec"

"github.com/steebchen/prisma-client-go/logger"
)

type Messsage struct {
Expand All @@ -26,6 +28,7 @@ func (e *QueryEngine) streamStderr(cmd *exec.Cmd, onError chan<- string) error {
case v := <-e.onEngineError:
e.lastEngineError = v
case <-e.closed:
logger.Debug.Printf("query engine closed")
break outer
}
}
Expand Down

0 comments on commit 4ef3e14

Please sign in to comment.