|
| 1 | +package runner |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "bytes" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "log/slog" |
| 10 | + "os/exec" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + // ErrHandlerTimeout indicates the handler did not respond within the timeout |
| 16 | + ErrHandlerTimeout = errors.New("handler timeout") |
| 17 | + // ErrHandlerClosed indicates the handler closed stdout unexpectedly |
| 18 | + ErrHandlerClosed = errors.New("handler closed unexpectedly") |
| 19 | +) |
| 20 | + |
| 21 | +// HandlerConfig configures a handler process |
| 22 | +type HandlerConfig struct { |
| 23 | + Path string |
| 24 | + Args []string |
| 25 | + Env []string |
| 26 | + // Timeout specifies the maximum duration to wait when reading from the handler's |
| 27 | + // stdout. If zero, defaults to 10 seconds. The handler is killed if it fails to |
| 28 | + // write output within this timeout. |
| 29 | + Timeout time.Duration |
| 30 | +} |
| 31 | + |
| 32 | +// Handler manages a conformance handler process communicating via stdin/stdout |
| 33 | +type Handler struct { |
| 34 | + cmd *exec.Cmd |
| 35 | + stdin io.WriteCloser |
| 36 | + stdout *bufio.Scanner |
| 37 | + stderr io.ReadCloser |
| 38 | + timeout time.Duration |
| 39 | +} |
| 40 | + |
| 41 | +// NewHandler spawns a new handler process with the given configuration |
| 42 | +func NewHandler(cfg *HandlerConfig) (*Handler, error) { |
| 43 | + cmd := exec.Command(cfg.Path, cfg.Args...) |
| 44 | + if cfg.Env != nil { |
| 45 | + cmd.Env = append(cmd.Environ(), cfg.Env...) |
| 46 | + } |
| 47 | + |
| 48 | + stdin, err := cmd.StdinPipe() |
| 49 | + if err != nil { |
| 50 | + return nil, fmt.Errorf("failed to create stdin pipe: %w", err) |
| 51 | + } |
| 52 | + |
| 53 | + stdout, err := cmd.StdoutPipe() |
| 54 | + if err != nil { |
| 55 | + return nil, fmt.Errorf("failed to create stdout pipe: %w", err) |
| 56 | + } |
| 57 | + |
| 58 | + stderr, err := cmd.StderrPipe() |
| 59 | + if err != nil { |
| 60 | + return nil, fmt.Errorf("failed to create stderr pipe: %w", err) |
| 61 | + } |
| 62 | + |
| 63 | + // Start() automatically closes all pipes on failure, no manual cleanup needed |
| 64 | + if err := cmd.Start(); err != nil { |
| 65 | + return nil, fmt.Errorf("failed to start handler: %w", err) |
| 66 | + } |
| 67 | + |
| 68 | + timeout := cfg.Timeout |
| 69 | + if timeout == 0 { |
| 70 | + timeout = 10 * time.Second |
| 71 | + } |
| 72 | + |
| 73 | + return &Handler{ |
| 74 | + cmd: cmd, |
| 75 | + stdin: stdin, |
| 76 | + stdout: bufio.NewScanner(stdout), |
| 77 | + stderr: stderr, |
| 78 | + timeout: timeout, |
| 79 | + }, nil |
| 80 | +} |
| 81 | + |
| 82 | +// SendLine writes a line to the handler's stdin |
| 83 | +func (h *Handler) SendLine(line []byte) error { |
| 84 | + _, err := h.stdin.Write(append(line, '\n')) |
| 85 | + return err |
| 86 | +} |
| 87 | + |
| 88 | +// ReadLine reads a line from the handler's stdout with a configurable timeout |
| 89 | +func (h *Handler) ReadLine() ([]byte, error) { |
| 90 | + // Use a timeout for Scan() in case the handler hangs |
| 91 | + scanDone := make(chan bool, 1) |
| 92 | + go func() { |
| 93 | + scanDone <- h.stdout.Scan() |
| 94 | + }() |
| 95 | + |
| 96 | + var baseErr error |
| 97 | + select { |
| 98 | + case ok := <-scanDone: |
| 99 | + if ok { |
| 100 | + return h.stdout.Bytes(), nil |
| 101 | + } |
| 102 | + if err := h.stdout.Err(); err != nil { |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + // EOF - handler closed stdout prematurely, fall through to kill and capture stderr |
| 106 | + baseErr = ErrHandlerClosed |
| 107 | + case <-time.After(h.timeout): |
| 108 | + // Timeout - handler didn't respond, fall through to kill and capture stderr |
| 109 | + baseErr = ErrHandlerTimeout |
| 110 | + } |
| 111 | + |
| 112 | + // Kill the process immediately to force stderr to close. |
| 113 | + // Without this, there's a rare scenario where stdout closes but stderr remains open, |
| 114 | + // causing io.ReadAll(h.stderr) below to block indefinitely waiting for stderr EOF. |
| 115 | + if h.cmd.Process != nil { |
| 116 | + h.cmd.Process.Kill() |
| 117 | + } |
| 118 | + |
| 119 | + // Capture stderr to provide diagnostic information when the handler fails. |
| 120 | + if stderrOut, err := io.ReadAll(h.stderr); err == nil && len(stderrOut) > 0 { |
| 121 | + return nil, fmt.Errorf("%w: %s", baseErr, bytes.TrimSpace(stderrOut)) |
| 122 | + } |
| 123 | + return nil, baseErr |
| 124 | +} |
| 125 | + |
| 126 | +// Close closes stdin and waits for the handler to exit with a 5-second timeout. |
| 127 | +// If the handler doesn't exit within the timeout, it is killed. |
| 128 | +func (h *Handler) Close() { |
| 129 | + if h.stdin != nil { |
| 130 | + // Close stdin to signal the handler that we're done sending requests. |
| 131 | + // Per the handler specification, the handler should exit cleanly when stdin closes. |
| 132 | + h.stdin.Close() |
| 133 | + } |
| 134 | + if h.cmd != nil { |
| 135 | + // Wait for the handler to exit cleanly in response to stdin closing. |
| 136 | + // Wait() automatically closes all remaining pipes after the process exits. |
| 137 | + // Use a timeout in case the handler doesn't respect the protocol. |
| 138 | + done := make(chan error, 1) |
| 139 | + go func() { |
| 140 | + done <- h.cmd.Wait() |
| 141 | + }() |
| 142 | + |
| 143 | + select { |
| 144 | + case err := <-done: |
| 145 | + if err != nil { |
| 146 | + slog.Warn("Handler exit with error", "error", err) |
| 147 | + } |
| 148 | + case <-time.After(5 * time.Second): |
| 149 | + slog.Warn("Handler did not exit within a 5-second timeout, killing process") |
| 150 | + if h.cmd.Process != nil { |
| 151 | + h.cmd.Process.Kill() |
| 152 | + // Call Wait() again to let the process finish cleanup (closing pipes, etc.) |
| 153 | + // No timeout needed since Kill() should guarantee the process will exit |
| 154 | + h.cmd.Wait() |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments