Skip to content

Commit 8971d61

Browse files
os/signal: clarify signal doc
Based on comments from Thomas Bushnell. Update #9896. Change-Id: I603b1382d17dff00b5d18f17f8b5d011503e9e4c Reviewed-on: https://go-review.googlesource.com/18365 Reviewed-by: Russ Cox <[email protected]>
1 parent 161f2e8 commit 8971d61

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

src/os/signal/doc.go

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,18 @@ causes the program to exit with a stack dump. A SIGTSTP, SIGTTIN, or
4242
SIGTTOU signal gets the system default behavior (these signals are
4343
used by the shell for job control). The SIGPROF signal is handled
4444
directly by the Go runtime to implement runtime.CPUProfile. Other
45-
signals are ignored.
45+
signals will be caught but no action will be taken.
4646
47-
If the Go program is started with either SIGHUP or SIGINT ignored,
48-
they will remain ignored. Go always registers a handler for the other
49-
signals.
47+
If the Go program is started with either SIGHUP or SIGINT ignored
48+
(signal handler set to SIG_IGN), they will remain ignored.
5049
5150
If the Go program is started with a non-empty signal mask, that will
5251
generally be honored. However, some signals are explicitly unblocked:
5352
the synchronous signals, SIGILL, SIGTRAP, SIGSTKFLT, SIGCHLD, SIGPROF,
5453
and, on GNU/Linux, signals 32 (SIGCANCEL) and 33 (SIGSETXID)
55-
(SIGCANCEL and SIGSETXID are used internally by glibc).
54+
(SIGCANCEL and SIGSETXID are used internally by glibc). Subprocesses
55+
started by os.Exec, or by the os/exec package, will inherit the
56+
modified signal mask.
5657
5758
Changing the behavior of signals in Go programs
5859
@@ -65,12 +66,12 @@ channels. Specifically, it applies to the signals SIGHUP, SIGINT,
6566
SIGQUIT, SIGABRT, and SIGTERM. It also applies to the job control
6667
signals SIGTSTP, SIGTTIN, and SIGTTOU, in which case the system
6768
default behavior does not occur. It also applies to some signals that
68-
are otherwise ignored: SIGUSR1, SIGUSR2, SIGPIPE, SIGALRM, SIGCHLD,
69-
SIGURG, SIGXCPU, SIGXFSZ, SIGVTALRM, SIGWINCH, SIGIO, SIGPWR, SIGSYS,
70-
SIGINFO, SIGTHR, SIGWAITING, SIGLWP, SIGFREEZE, SIGTHAW, SIGLOST,
71-
SIGXRES, SIGJVM1, SIGJVM2, and any real time signals used on the
72-
system. Note that not all of these signals are available on all
73-
systems.
69+
otherwise cause no action: SIGUSR1, SIGUSR2, SIGPIPE, SIGALRM,
70+
SIGCHLD, SIGCONT, SIGURG, SIGXCPU, SIGXFSZ, SIGVTALRM, SIGWINCH,
71+
SIGIO, SIGPWR, SIGSYS, SIGINFO, SIGTHR, SIGWAITING, SIGLWP, SIGFREEZE,
72+
SIGTHAW, SIGLOST, SIGXRES, SIGJVM1, SIGJVM2, and any real time signals
73+
used on the system. Note that not all of these signals are available
74+
on all systems.
7475
7576
If the program was started with SIGHUP or SIGINT ignored, and Notify
7677
is called for either signal, a signal handler will be installed for
@@ -89,15 +90,24 @@ Notify for that signal, the signal will once again be blocked.
8990
9091
SIGPIPE
9192
92-
When a Go program receives an EPIPE error from the kernel while
93-
writing to file descriptors 1 or 2 (standard output or standard
94-
error), it will raise a SIGPIPE signal. If the program is not
95-
currently receiving SIGPIPE via a call to Notify, this will cause the
96-
program to exit with SIGPIPE. On descriptors other than 1 or 2, the
97-
write will return the EPIPE error. This means that, by default,
98-
command line programs will behave like typical Unix command line
99-
programs, while other programs will not crash with SIGPIPE when
100-
writing to a closed network connection.
93+
When a Go program writes to a broken pipe, the kernel will raise a
94+
SIGPIPE signal.
95+
96+
If the program has not called Notify to receive SIGPIPE signals, then
97+
the behavior depends on the file descriptor number. A write to a
98+
broken pipe on file descriptors 1 or 2 (standard output or standard
99+
error) will cause the program to exit with a SIGPIPE signal. A write
100+
to a broken pipe on some other file descriptor will take no action on
101+
the SIGPIPE signal, and the write will fail with an EPIPE error.
102+
103+
If the program has called Notify to receive SIGPIPE signals, the file
104+
descriptor number does not matter. The SIGPIPE signal will be
105+
delivered to the Notify channel, and the write will fail with an EPIPE
106+
error.
107+
108+
This means that, by default, command line programs will behave like
109+
typical Unix command line programs, while other programs will not
110+
crash with SIGPIPE when writing to a closed network connection.
101111
102112
Go programs that use cgo or SWIG
103113

0 commit comments

Comments
 (0)