@@ -125,10 +125,11 @@ func NewContainer(ctx context.Context, platform rproc.Platform, r *task.CreateTa
125125 return nil , errdefs .ToGRPC (err )
126126 }
127127 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 {}),
132133 }
133134 pid := process .Pid ()
134135 if pid > 0 {
@@ -189,9 +190,10 @@ type Container struct {
189190 // Bundle path
190191 Bundle string
191192
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 {}
195197}
196198
197199// All processes in the container
@@ -256,18 +258,35 @@ func (c *Container) Process(id string) (rproc.Process, error) {
256258 return p , nil
257259}
258260
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 ()) {
261267 c .mu .Lock ()
262268 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+ }
265282}
266283
267284// ProcessAdd adds a new process to the container
268285func (c * Container ) ProcessAdd (process rproc.Process ) {
269286 c .mu .Lock ()
270287 defer c .mu .Unlock ()
288+
289+ delete (c .reservedProcess , process .ID ())
271290 c .processes [process .ID ()] = process
272291}
273292
0 commit comments