@@ -125,10 +125,11 @@ func NewContainer(ctx context.Context, platform rproc.Platform, r *task.CreateTa
125
125
return nil , errdefs .ToGRPC (err )
126
126
}
127
127
container := & Container {
128
- ID : r .ID ,
129
- Bundle : r .Bundle ,
130
- process : process ,
131
- processes : make (map [string ]rproc.Process ),
128
+ ID : r .ID ,
129
+ Bundle : r .Bundle ,
130
+ process : process ,
131
+ processes : make (map [string ]rproc.Process ),
132
+ reservedProcess : make (map [string ]struct {}),
132
133
}
133
134
pid := process .Pid ()
134
135
if pid > 0 {
@@ -189,9 +190,10 @@ type Container struct {
189
190
// Bundle path
190
191
Bundle string
191
192
192
- cgroup cgroups.Cgroup
193
- process rproc.Process
194
- processes map [string ]rproc.Process
193
+ cgroup cgroups.Cgroup
194
+ process rproc.Process
195
+ processes map [string ]rproc.Process
196
+ reservedProcess map [string ]struct {}
195
197
}
196
198
197
199
// All processes in the container
@@ -256,18 +258,35 @@ func (c *Container) Process(id string) (rproc.Process, error) {
256
258
return p , nil
257
259
}
258
260
259
- // ProcessExists returns true if the process by id exists
260
- func (c * Container ) ProcessExists (id string ) bool {
261
+ // ReserveProcess checks for the existence of an id and atomically
262
+ // reserves the process id if it does not already exist
263
+ //
264
+ // Returns true if the process id was sucessfully reserved and a
265
+ // cancel func to release the reservation
266
+ func (c * Container ) ReserveProcess (id string ) (bool , func ()) {
261
267
c .mu .Lock ()
262
268
defer c .mu .Unlock ()
263
- _ , ok := c .processes [id ]
264
- return ok
269
+
270
+ if _ , ok := c .processes [id ]; ok {
271
+ return false , nil
272
+ }
273
+ if _ , ok := c .reservedProcess [id ]; ok {
274
+ return false , nil
275
+ }
276
+ c .reservedProcess [id ] = struct {}{}
277
+ return true , func () {
278
+ c .mu .Lock ()
279
+ defer c .mu .Unlock ()
280
+ delete (c .reservedProcess , id )
281
+ }
265
282
}
266
283
267
284
// ProcessAdd adds a new process to the container
268
285
func (c * Container ) ProcessAdd (process rproc.Process ) {
269
286
c .mu .Lock ()
270
287
defer c .mu .Unlock ()
288
+
289
+ delete (c .reservedProcess , process .ID ())
271
290
c .processes [process .ID ()] = process
272
291
}
273
292
0 commit comments