Skip to content

Commit

Permalink
solverrpc: Detect exited csppsolver process
Browse files Browse the repository at this point in the history
If the csppsolver process was successfully started, but has since exited (due
to crash, or killed by a signal), a new error is returned.  This error
condition will need to be checked by mixclient so it can disable root solving
attempts for future mix runs.
  • Loading branch information
jrick committed Nov 8, 2024
1 parent 3c81672 commit f711f7d
Showing 1 changed file with 33 additions and 0 deletions.
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

0 comments on commit f711f7d

Please sign in to comment.