Skip to content

os/signal: signal.Ignore is inherited by children after a forkExec #20479

@muhamadazmy

Description

@muhamadazmy

What version of Go are you using (go version)?

go version go1.8.1 linux/amd64

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/azmy/work/go"
GORACE=""
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build637832860=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

What did you do?

package main

import (
	"fmt"
	"os/exec"
	"os/signal"
	"syscall"
	"time"
)

func terminate(pid int) {
	for {
		<-time.After(1 * time.Second)
		fmt.Println("trying to kill")
		syscall.Kill(pid, syscall.SIGTERM)
	}
}

func main() {
	signal.Ignore(syscall.SIGTERM)
	cmd := exec.Command("sleep", "3")

	if err := cmd.Start(); err != nil {
		panic(err)
	}

	fmt.Println("PID:", cmd.Process.Pid)
	go terminate(cmd.Process.Pid)
	if state, err := cmd.Process.Wait(); err != nil {
		panic(err)
	} else {
		fmt.Println("Success:", state.Success(), state)
	}
}

What did you expect to see?

When run the program should print

PID: <pid>
trying to kill
Success: false signal: terminated

A signal terminate to the sleep process should still kill it since execve system call should reset all signal handlers to the default ones. But for some reason this doesn't happen with ignore. To work around this I had to add empty signal handler for the terminate signal so only the parent process ignore the signal while the children still have their default handler in place.

What did you see instead?

PID: <pid>
trying to kill
trying to kill
trying to kill
Success: true exit status 0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions