/
subprocess.go
323 lines (280 loc) · 7.9 KB
/
subprocess.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package subprocess
import (
"bufio"
"context"
"fmt"
"io"
"os"
"os/exec"
"sync"
"syscall"
"time"
"go.uber.org/atomic"
"go.uber.org/zap"
)
const (
defaultRestartDelay = 5 * time.Second
defaultShutdownTimeout = 5 * time.Second
noPid = -1
)
// exported to be used by jmx metrics extension
type Config struct {
ExecutablePath string `mapstructure:"executable_path"`
Args []string `mapstructure:"args"`
EnvironmentVariables map[string]string `mapstructure:"environment_variables"`
StdInContents string `mapstructure:"stdin_contents"`
RestartOnError bool `mapstructure:"restart_on_error"`
RestartDelay *time.Duration `mapstructure:"restart_delay"`
ShutdownTimeout *time.Duration `mapstructure:"shutdown_timeout"`
}
// exported to be used by jmx metrics extension
type Subprocess struct {
Stdout chan string
cancel context.CancelFunc
config *Config
envVars []string
logger *zap.Logger
pid pid
shutdownSignal chan struct{}
// configurable for testing purposes
sendToStdIn func(string, io.Writer) error
}
type pid struct {
pid int
pidLock sync.Mutex
}
func (p *pid) setPid(pid int) {
p.pidLock.Lock()
defer p.pidLock.Unlock()
p.pid = pid
}
func (p *pid) getPid() int {
p.pidLock.Lock()
defer p.pidLock.Unlock()
return p.pid
}
func (subprocess *Subprocess) Pid() int {
pid := subprocess.pid.getPid()
if pid == 0 {
return noPid
}
return pid
}
// exported to be used by jmx metrics extension
func NewSubprocess(conf *Config, logger *zap.Logger) *Subprocess {
if conf.RestartDelay == nil {
restartDelay := defaultRestartDelay
conf.RestartDelay = &restartDelay
}
if conf.ShutdownTimeout == nil {
shutdownTimeout := defaultShutdownTimeout
conf.ShutdownTimeout = &shutdownTimeout
}
return &Subprocess{
Stdout: make(chan string),
pid: pid{pid: noPid, pidLock: sync.Mutex{}},
config: conf,
logger: logger,
shutdownSignal: make(chan struct{}),
sendToStdIn: sendToStdIn,
}
}
const (
Starting = "Starting"
Running = "Running"
ShuttingDown = "ShuttingDown"
Stopped = "Stopped"
Restarting = "Restarting"
Errored = "Errored"
)
func (subprocess *Subprocess) Start(ctx context.Context) error {
var cancelCtx context.Context
cancelCtx, subprocess.cancel = context.WithCancel(ctx)
for k, v := range subprocess.config.EnvironmentVariables {
joined := fmt.Sprintf("%v=%v", k, v)
subprocess.envVars = append(subprocess.envVars, joined)
}
go func() {
subprocess.run(cancelCtx) // will block for lifetime of process
close(subprocess.shutdownSignal)
}()
return nil
}
// Shutdown is invoked during service shutdown.
func (subprocess *Subprocess) Shutdown(ctx context.Context) error {
subprocess.cancel()
timeout := defaultShutdownTimeout
if subprocess.config.ShutdownTimeout != nil {
timeout = *subprocess.config.ShutdownTimeout
}
t := time.NewTimer(timeout)
// Wait for the subprocess to exit or the timeout period to elapse
select {
case <-ctx.Done():
case <-subprocess.shutdownSignal:
case <-t.C:
subprocess.logger.Warn("subprocess hasn't returned within shutdown timeout. May be zombied.",
zap.String("timeout", fmt.Sprintf("%v", timeout)))
}
return nil
}
// A synchronization helper to ensure that signalWhenProcessReturned
// doesn't write to a closed channel
type processReturned struct {
ReturnedChan chan error
isOpen *atomic.Bool
lock *sync.Mutex
}
func newProcessReturned() *processReturned {
pr := processReturned{
ReturnedChan: make(chan error),
isOpen: atomic.NewBool(true),
lock: &sync.Mutex{},
}
return &pr
}
func (pr *processReturned) signal(err error) {
pr.lock.Lock()
defer pr.lock.Unlock()
if pr.isOpen.Load() {
pr.ReturnedChan <- err
}
}
func (pr *processReturned) close() {
pr.lock.Lock()
defer pr.lock.Unlock()
if pr.isOpen.Load() {
close(pr.ReturnedChan)
pr.isOpen.Store(false)
}
}
// Core event loop
func (subprocess *Subprocess) run(ctx context.Context) {
var cmd *exec.Cmd
var err error
var stdin io.WriteCloser
var stdout io.ReadCloser
// writer is signalWhenProcessReturned() and closer is this loop, so we need synchronization
processReturned := newProcessReturned()
state := Starting
for {
subprocess.logger.Debug("subprocess changed state", zap.String("state", state))
switch state {
case Starting:
cmd, stdin, stdout = createCommand(
subprocess.config.ExecutablePath,
subprocess.config.Args,
subprocess.envVars,
)
go collectStdout(bufio.NewScanner(stdout), subprocess.Stdout, subprocess.logger)
subprocess.logger.Debug("starting subprocess", zap.String("command", cmd.String()))
err = cmd.Start()
if err != nil {
state = Errored
continue
}
subprocess.pid.setPid(cmd.Process.Pid)
go signalWhenProcessReturned(cmd, processReturned)
state = Running
case Running:
err = subprocess.sendToStdIn(subprocess.config.StdInContents, stdin)
stdin.Close()
if err != nil {
state = Errored
continue
}
select {
case err = <-processReturned.ReturnedChan:
if err != nil && ctx.Err() == nil {
err = fmt.Errorf("unexpected shutdown: %w", err)
// We aren't supposed to shutdown yet so this is an error state.
state = Errored
continue
}
// We must close this channel or can wait indefinitely at ShuttingDown
processReturned.close()
state = ShuttingDown
case <-ctx.Done(): // context-based cancel.
state = ShuttingDown
}
case Errored:
subprocess.logger.Error("subprocess died", zap.Error(err))
if subprocess.config.RestartOnError {
subprocess.pid.setPid(-1)
state = Restarting
} else {
// We must close this channel or can wait indefinitely at ShuttingDown
processReturned.close()
state = ShuttingDown
}
case ShuttingDown:
if cmd.Process != nil {
cmd.Process.Signal(syscall.SIGTERM)
}
<-processReturned.ReturnedChan
stdout.Close()
subprocess.pid.setPid(-1)
state = Stopped
case Restarting:
stdout.Close()
stdin.Close()
time.Sleep(*subprocess.config.RestartDelay)
state = Starting
case Stopped:
return
}
}
}
func signalWhenProcessReturned(cmd *exec.Cmd, pr *processReturned) {
err := cmd.Wait()
pr.signal(err)
}
func collectStdout(stdoutScanner *bufio.Scanner, stdoutChan chan<- string, logger *zap.Logger) {
for stdoutScanner.Scan() {
text := stdoutScanner.Text()
if text != "" {
stdoutChan <- text
logger.Debug(text)
}
}
// Returns when stdout is closed when the process ends
}
func sendToStdIn(contents string, writer io.Writer) error {
if contents == "" {
return nil
}
_, err := writer.Write([]byte(contents))
return err
}
func createCommand(execPath string, args, envVars []string) (*exec.Cmd, io.WriteCloser, io.ReadCloser) {
cmd := exec.Command(execPath, args...)
var env []string
env = append(env, os.Environ()...)
cmd.Env = append(env, envVars...)
inReader, inWriter, err := os.Pipe()
if err != nil {
panic("Input pipe could not be created for subprocess")
}
cmd.Stdin = inReader
outReader, outWriter, err := os.Pipe()
if err != nil {
panic("Output pipe could not be created for subprocess")
}
cmd.Stdout = outWriter
cmd.Stderr = outWriter
applyOSSpecificCmdModifications(cmd)
return cmd, inWriter, outReader
}