diff options
author | Valery Piashchynski <[email protected]> | 2021-12-15 01:02:39 +0300 |
---|---|---|
committer | GitHub <[email protected]> | 2021-12-15 01:02:39 +0300 |
commit | ff1401e97a9d1f3c059a60acbcae3f4507dcfe03 (patch) | |
tree | 7076f4712f9b4dbfb6d3a1bae3f4ba812d92a89d /worker/worker.go | |
parent | f2c79017ae5759256b03ec58b608f298a29e4b96 (diff) | |
parent | 1bcb131c1ace6bdb47123cf05e4943ac3c4744c4 (diff) |
[#872]: bug(static_pool, debug mode): worker exited immediately after obtaining the response
Diffstat (limited to 'worker/worker.go')
-rwxr-xr-x | worker/worker.go | 34 |
1 files changed, 22 insertions, 12 deletions
diff --git a/worker/worker.go b/worker/worker.go index e5c3a192..564d83c4 100755 --- a/worker/worker.go +++ b/worker/worker.go @@ -44,7 +44,8 @@ type Process struct { // pid of the process, points to pid of underlying process and // can be nil while process is not started. - pid int + pid int + doneCh chan struct{} // communication bus with underlying process. relay relay.Relay @@ -63,6 +64,7 @@ func InitBaseWorker(cmd *exec.Cmd, options ...Options) (*Process, error) { eventsID: id, cmd: cmd, state: NewWorkerState(StateInactive), + doneCh: make(chan struct{}, 1), } // set self as stderr implementation (Writer interface) @@ -136,6 +138,7 @@ func (w *Process) Wait() error { var err error err = w.cmd.Wait() defer w.events.Unsubscribe(w.eventsID) + w.doneCh <- struct{}{} // If worker was destroyed, just exit if w.State().Value() == StateDestroyed { @@ -179,19 +182,26 @@ func (w *Process) closeRelay() error { // Stop sends soft termination command to the Process and waits for process completion. func (w *Process) Stop() error { const op = errors.Op("process_stop") - w.state.Set(StateStopping) - err := internal.SendControl(w.relay, &internal.StopCommand{Stop: true}) - if err != nil { - w.state.Set(StateKilling) - _ = w.cmd.Process.Signal(os.Kill) + defer w.events.Unsubscribe(w.eventsID) - w.events.Unsubscribe(w.eventsID) - return errors.E(op, errors.Network, err) - } + select { + // finished + case <-w.doneCh: + return nil + default: + w.state.Set(StateStopping) + err := internal.SendControl(w.relay, &internal.StopCommand{Stop: true}) + if err != nil { + w.state.Set(StateKilling) + _ = w.cmd.Process.Signal(os.Kill) - w.state.Set(StateStopped) - w.events.Unsubscribe(w.eventsID) - return nil + return errors.E(op, errors.Network, err) + } + + <-w.doneCh + w.state.Set(StateStopped) + return nil + } } // Kill kills underlying process, make sure to call Wait() func to gather |