@@ -17,6 +17,7 @@ import (
17
17
// ErrProcessDone indicates a [Process] has finished.
18
18
var ErrProcessDone = errors .New ("os: process already finished" )
19
19
20
+ // processStatus describes the status of a [Process].
20
21
type processStatus uint32
21
22
22
23
const (
@@ -110,13 +111,15 @@ func (ph *processHandle) release() {
110
111
}
111
112
}
112
113
114
+ // newPIDProcess returns a [Process] for the given PID.
113
115
func newPIDProcess (pid int ) * Process {
114
116
p := & Process {
115
117
Pid : pid ,
116
118
}
117
119
return p
118
120
}
119
121
122
+ // newHandleProcess returns a [Process] with the given PID and handle.
120
123
func newHandleProcess (pid int , handle uintptr ) * Process {
121
124
ph := & processHandle {
122
125
handle : handle ,
@@ -136,6 +139,9 @@ func newHandleProcess(pid int, handle uintptr) *Process {
136
139
return p
137
140
}
138
141
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.
139
145
func newDoneProcess (pid int ) * Process {
140
146
p := & Process {
141
147
Pid : pid ,
@@ -144,6 +150,8 @@ func newDoneProcess(pid int) *Process {
144
150
return p
145
151
}
146
152
153
+ // handleTransientAcquire returns the process handle or,
154
+ // if the process is not ready, the current status.
147
155
func (p * Process ) handleTransientAcquire () (uintptr , processStatus ) {
148
156
if p .handle == nil {
149
157
panic ("handleTransientAcquire called in invalid mode" )
@@ -169,13 +177,15 @@ func (p *Process) handleTransientAcquire() (uintptr, processStatus) {
169
177
return 0 , status
170
178
}
171
179
180
+ // handleTransientRelease releases a handle returned by handleTransientAcquire.
172
181
func (p * Process ) handleTransientRelease () {
173
182
if p .handle == nil {
174
183
panic ("handleTransientRelease called in invalid mode" )
175
184
}
176
185
p .handle .release ()
177
186
}
178
187
188
+ // pidStatus returns the current process status.
179
189
func (p * Process ) pidStatus () processStatus {
180
190
if p .handle != nil {
181
191
panic ("pidStatus called in invalid mode" )
@@ -301,6 +311,10 @@ func (p *Process) doRelease(newStatus processStatus) processStatus {
301
311
// created in newHandleProcess.
302
312
if p .handle != nil {
303
313
// 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.
304
318
p .cleanup .Stop ()
305
319
306
320
p .handle .release ()
0 commit comments