Skip to content

Commit 82a851d

Browse files
committed
added wating for the parent after sending 'SIGKILL' signal
1 parent f242cc8 commit 82a851d

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

app.go

+17-5
Original file line numberDiff line numberDiff line change
@@ -376,12 +376,24 @@ func protocolActAsChild(m *StreamMessenger, waitChildTimeout time.Duration, wait
376376
return nil
377377
}
378378

379-
func killParent() (int, error) {
379+
func killParent() (parentPID int, err error) {
380+
return killProcess(os.Getppid())
381+
}
382+
383+
func killProcess(pid int) (parentPID int, err error) {
380384
// If it's systemd - keep it alive. Possible e.g. when systemd
381385
// performs 'socket activation'.
382-
parentPID := os.Getppid()
383-
if parentPID == 1 {
384-
return parentPID, fmt.Errorf("failed to kill parent. It's systemd")
386+
if pid == 1 {
387+
return pid, fmt.Errorf("failed to kill process. It's systemd")
388+
}
389+
p, err := os.FindProcess(pid)
390+
if err != nil {
391+
return
392+
}
393+
err = p.Signal(syscall.SIGKILL)
394+
if err != nil {
395+
return
385396
}
386-
return parentPID, syscall.Kill(parentPID, syscall.SIGKILL)
397+
_, err = p.Wait()
398+
return
387399
}

app_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,29 @@ func TestTrippleRestartStateless(t *testing.T) {
291291
require.Equal(t, 1, <-ch)
292292
}
293293

294+
func TestKillParent(t *testing.T) {
295+
d := newRun(t, 2608)
296+
d.start(true)
297+
298+
_, err := killProcess(d.lastProcess().Pid)
299+
require.NoError(t, err)
300+
st, err := d.lastProcess().Wait()
301+
assert.Nil(t, st)
302+
assert.EqualValues(t, syscall.ECHILD, underlyingError(err).(syscall.Errno))
303+
}
304+
305+
func underlyingError(err error) error {
306+
switch err := err.(type) {
307+
case *os.PathError:
308+
return err.Err
309+
case *os.LinkError:
310+
return err.Err
311+
case *os.SyscallError:
312+
return err.Err
313+
}
314+
return err
315+
}
316+
294317
//
295318
// A server live here.
296319
//

0 commit comments

Comments
 (0)