Skip to content

Commit ff627d2

Browse files
ianlancetaylorgopherbot
authored andcommitted
os: improve comments for process support, minor code cleanup
Change-Id: I97ecbc6fc0c73c6d8469144f86a7ad8c2655a658 Reviewed-on: https://go-review.googlesource.com/c/go/+/638581 Reviewed-by: Robert Griesemer <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Cherry Mui <[email protected]>
1 parent 646d285 commit ff627d2

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/os/exec.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
// ErrProcessDone indicates a [Process] has finished.
1818
var ErrProcessDone = errors.New("os: process already finished")
1919

20+
// processStatus describes the status of a [Process].
2021
type processStatus uint32
2122

2223
const (
@@ -110,13 +111,15 @@ func (ph *processHandle) release() {
110111
}
111112
}
112113

114+
// newPIDProcess returns a [Process] for the given PID.
113115
func newPIDProcess(pid int) *Process {
114116
p := &Process{
115117
Pid: pid,
116118
}
117119
return p
118120
}
119121

122+
// newHandleProcess returns a [Process] with the given PID and handle.
120123
func newHandleProcess(pid int, handle uintptr) *Process {
121124
ph := &processHandle{
122125
handle: handle,
@@ -136,6 +139,9 @@ func newHandleProcess(pid int, handle uintptr) *Process {
136139
return p
137140
}
138141

142+
// newDoneProcess returns a [Process] for the given PID
143+
// that is already marked as done. This is used on Unix systems
144+
// if the process is known to not exist.
139145
func newDoneProcess(pid int) *Process {
140146
p := &Process{
141147
Pid: pid,
@@ -144,6 +150,8 @@ func newDoneProcess(pid int) *Process {
144150
return p
145151
}
146152

153+
// handleTransientAcquire returns the process handle or,
154+
// if the process is not ready, the current status.
147155
func (p *Process) handleTransientAcquire() (uintptr, processStatus) {
148156
if p.handle == nil {
149157
panic("handleTransientAcquire called in invalid mode")
@@ -169,13 +177,15 @@ func (p *Process) handleTransientAcquire() (uintptr, processStatus) {
169177
return 0, status
170178
}
171179

180+
// handleTransientRelease releases a handle returned by handleTransientAcquire.
172181
func (p *Process) handleTransientRelease() {
173182
if p.handle == nil {
174183
panic("handleTransientRelease called in invalid mode")
175184
}
176185
p.handle.release()
177186
}
178187

188+
// pidStatus returns the current process status.
179189
func (p *Process) pidStatus() processStatus {
180190
if p.handle != nil {
181191
panic("pidStatus called in invalid mode")
@@ -301,6 +311,10 @@ func (p *Process) doRelease(newStatus processStatus) processStatus {
301311
// created in newHandleProcess.
302312
if p.handle != nil {
303313
// No need for more cleanup.
314+
// We must stop the cleanup before calling release;
315+
// otherwise the cleanup might run concurrently
316+
// with the release, which would cause the reference
317+
// counts to be invalid, causing a panic.
304318
p.cleanup.Stop()
305319

306320
p.handle.release()

src/os/pidfd_linux.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func getPidfd(sysAttr *syscall.SysProcAttr, needDup bool) (uintptr, bool) {
6666
return uintptr(h), true
6767
}
6868

69+
// pidfdFind returns the process handle for pid.
6970
func pidfdFind(pid int) (uintptr, error) {
7071
if !pidfdWorks() {
7172
return 0, syscall.ENOSYS
@@ -78,6 +79,8 @@ func pidfdFind(pid int) (uintptr, error) {
7879
return h, nil
7980
}
8081

82+
// pidfdWait waits for the process to complete,
83+
// and updates the process status to done.
8184
func (p *Process) pidfdWait() (*ProcessState, error) {
8285
// When pidfd is used, there is no wait/kill race (described in CL 23967)
8386
// because the PID recycle issue doesn't exist (IOW, pidfd, unlike PID,
@@ -120,6 +123,7 @@ func (p *Process) pidfdWait() (*ProcessState, error) {
120123
}, nil
121124
}
122125

126+
// pidfdSendSignal sends a signal to the process.
123127
func (p *Process) pidfdSendSignal(s syscall.Signal) error {
124128
handle, status := p.handleTransientAcquire()
125129
switch status {
@@ -133,10 +137,12 @@ func (p *Process) pidfdSendSignal(s syscall.Signal) error {
133137
return convertESRCH(unix.PidFDSendSignal(handle, s))
134138
}
135139

140+
// pidfdWorks returns whether we can use pidfd on this system.
136141
func pidfdWorks() bool {
137142
return checkPidfdOnce() == nil
138143
}
139144

145+
// checkPidfdOnce is used to only check whether pidfd works once.
140146
var checkPidfdOnce = sync.OnceValue(checkPidfd)
141147

142148
// checkPidfd checks whether all required pidfd-related syscalls work. This

0 commit comments

Comments
 (0)