Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solverrpc: Detect exited csppsolver process #100

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions solverrpc/rpc.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package solverrpc

import (
"errors"
"io"
"math/big"
"net/rpc"
Expand All @@ -13,6 +14,10 @@ import (
// Roots if the process is named differently or if an absolute path is needed.
var SolverProcess = "csppsolver"

// ErrSolverProcessExited indicates that the solver process was started, but
// has since exited (possibly due to a crash, or a signal which killed it).
var ErrSolverProcessExited = errors.New("solver process exited")

type solverProcess struct {
cmd *exec.Cmd
stdin io.WriteCloser
Expand Down Expand Up @@ -44,6 +49,7 @@ var (
once sync.Once
onceErr error
client *rpc.Client
cmdDone = make(chan struct{})
)

func startSolver() {
Expand All @@ -68,6 +74,19 @@ func startSolver() {
stdin: stdin,
stdout: stdout,
})
go func() {
cmd.Wait()
close(cmdDone)
}()
}

func checkExit() error {
select {
case <-cmdDone:
return ErrSolverProcessExited
default:
return nil
}
}

// StartSolver starts the solver's background process. This can be used to
Expand All @@ -85,6 +104,10 @@ func RootFactors(a []*big.Int, F *big.Int) ([]*big.Int, []int, error) {
return nil, nil, err
}

if err := checkExit(); err != nil {
return nil, nil, err
}

var args struct {
A []*big.Int
F *big.Int
Expand All @@ -97,6 +120,9 @@ func RootFactors(a []*big.Int, F *big.Int) ([]*big.Int, []int, error) {
}
err := client.Call("Solver.RootFactors", args, &result)
if err != nil {
if exitErr := checkExit(); exitErr != nil {
err = exitErr
}
return nil, nil, err
}
return result.Roots, result.Exponents, nil
Expand All @@ -114,6 +140,10 @@ func Roots(a []*big.Int, F *big.Int) ([]*big.Int, error) {
return nil, err
}

if err := checkExit(); err != nil {
return nil, err
}

var args struct {
A []*big.Int
F *big.Int
Expand All @@ -126,6 +156,9 @@ func Roots(a []*big.Int, F *big.Int) ([]*big.Int, error) {
}
err := client.Call("Solver.Roots", args, &result)
if err != nil {
if exitErr := checkExit(); exitErr != nil {
err = exitErr
}
return nil, err
}
if result.RepeatedRoot != nil {
Expand Down
Loading