@@ -20,7 +20,6 @@ import (
20
20
"os"
21
21
"os/exec"
22
22
"strconv"
23
- "strings"
24
23
"sync"
25
24
"syscall"
26
25
"time"
@@ -115,15 +114,15 @@ func (s *local) CreateVM(requestCtx context.Context, req *proto.CreateVMRequest)
115
114
// We determine if there is already a shim managing a VM with the current VMID by attempting
116
115
// to listen on the abstract socket address (which is parameterized by VMID). If we get
117
116
// EADDRINUSE, then we assume there is already a shim for the VM and return an AlreadyExists error.
118
- shimSocketAddress , err := fcShim .SocketAddress (requestCtx , id )
117
+ shimSocketAddress , err := shim .SocketAddress (requestCtx , s . containerdAddress , id )
119
118
if err != nil {
120
119
err = errors .Wrap (err , "failed to obtain shim socket address" )
121
120
s .logger .WithError (err ).Error ()
122
121
return nil , err
123
122
}
124
123
125
124
shimSocket , err := shim .NewSocket (shimSocketAddress )
126
- if isEADDRINUSE (err ) {
125
+ if shim . SocketEaddrinuse (err ) {
127
126
return nil , status .Errorf (codes .AlreadyExists , "VM with ID %q already exists (socket: %q)" , id , shimSocketAddress )
128
127
} else if err != nil {
129
128
err = errors .Wrapf (err , "failed to open shim socket at address %q" , shimSocketAddress )
@@ -132,7 +131,6 @@ func (s *local) CreateVM(requestCtx context.Context, req *proto.CreateVMRequest)
132
131
}
133
132
134
133
// If we're here, there is no pre-existing shim for this VMID, so we spawn a new one
135
- defer shimSocket .Close ()
136
134
if err := os .Mkdir (s .config .ShimBaseDir , 0700 ); err != nil && ! os .IsExist (err ) {
137
135
s .logger .WithError (err ).Error ()
138
136
return nil , errors .Wrapf (err , "failed to make shim base directory: %s" , s .config .ShimBaseDir )
@@ -165,7 +163,7 @@ func (s *local) CreateVM(requestCtx context.Context, req *proto.CreateVMRequest)
165
163
// containerd does not currently expose the shim server for us to register the fccontrol service with too.
166
164
// This is likely addressable through some relatively small upstream contributions; the following is a stop-gap
167
165
// solution until that time.
168
- fcSocketAddress , err := fcShim .FCControlSocketAddress (requestCtx , id )
166
+ fcSocketAddress , err := fcShim .FCControlSocketAddress (requestCtx , s . containerdAddress , id )
169
167
if err != nil {
170
168
err = errors .Wrap (err , "failed to obtain shim socket address" )
171
169
s .logger .WithError (err ).Error ()
@@ -179,8 +177,6 @@ func (s *local) CreateVM(requestCtx context.Context, req *proto.CreateVMRequest)
179
177
return nil , err
180
178
}
181
179
182
- defer fcSocket .Close ()
183
-
184
180
cmd , err := s .newShim (ns , id , s .containerdAddress , shimSocket , fcSocket )
185
181
if err != nil {
186
182
return nil , err
@@ -223,14 +219,14 @@ func (s *local) shimFirecrackerClient(requestCtx context.Context, vmID string) (
223
219
return nil , errors .Wrap (err , "invalid id" )
224
220
}
225
221
226
- socketAddr , err := fcShim .FCControlSocketAddress (requestCtx , vmID )
222
+ socketAddr , err := fcShim .FCControlSocketAddress (requestCtx , s . containerdAddress , vmID )
227
223
if err != nil {
228
224
err = errors .Wrap (err , "failed to get shim's fccontrol socket address" )
229
225
s .logger .WithError (err ).Error ()
230
226
return nil , err
231
227
}
232
228
233
- return fcclient .New (" \x00 " + socketAddr )
229
+ return fcclient .New (socketAddr )
234
230
}
235
231
236
232
// StopVM stops running VM instance by VM ID. This stops the VM, all tasks within the VM and the runtime shim
@@ -289,7 +285,7 @@ func (s *local) ResumeVM(ctx context.Context, req *proto.ResumeVMRequest) (*empt
289
285
}
290
286
291
287
func (s * local ) waitForShimToExit (ctx context.Context , vmID string ) error {
292
- socketAddr , err := fcShim .SocketAddress (ctx , vmID )
288
+ socketAddr , err := shim .SocketAddress (ctx , s . containerdAddress , vmID )
293
289
if err != nil {
294
290
return err
295
291
}
@@ -458,14 +454,18 @@ func (s *local) newShim(ns, vmID, containerdAddress string, shimSocket *net.Unix
458
454
}
459
455
}
460
456
461
- // Close all Unix abstract sockets.
457
+ // Close all Unix sockets.
462
458
if err := shimSocketFile .Close (); err != nil {
463
459
logger .WithError (err ).Errorf ("failed to close %q" , shimSocketFile .Name ())
464
460
}
465
461
if err := fcSocketFile .Close (); err != nil {
466
462
logger .WithError (err ).Errorf ("failed to close %q" , fcSocketFile .Name ())
467
463
}
468
464
465
+ if err := s .removeSockets (ns , vmID ); err != nil {
466
+ logger .WithError (err ).Errorf ("failed to remove sockets" )
467
+ }
468
+
469
469
if err := os .RemoveAll (shimDir .RootPath ()); err != nil {
470
470
logger .WithError (err ).Errorf ("failed to remove %q" , shimDir .RootPath ())
471
471
}
@@ -480,8 +480,33 @@ func (s *local) newShim(ns, vmID, containerdAddress string, shimSocket *net.Unix
480
480
return cmd , nil
481
481
}
482
482
483
- func isEADDRINUSE (err error ) bool {
484
- return err != nil && strings .Contains (err .Error (), "address already in use" )
483
+ func (s * local ) removeSockets (ns string , vmID string ) error {
484
+ var result * multierror.Error
485
+
486
+ // This context is only used for passing the namespace.
487
+ ctx := namespaces .WithNamespace (context .Background (), ns )
488
+
489
+ address , err := shim .SocketAddress (ctx , s .containerdAddress , vmID )
490
+ if err != nil {
491
+ result = multierror .Append (result , err )
492
+ } else {
493
+ err := shim .RemoveSocket (address )
494
+ if err != nil {
495
+ result = multierror .Append (result , err )
496
+ }
497
+ }
498
+
499
+ address , err = fcShim .FCControlSocketAddress (ctx , s .containerdAddress , vmID )
500
+ if err != nil {
501
+ result = multierror .Append (result , err )
502
+ } else {
503
+ err = shim .RemoveSocket (address )
504
+ if err != nil {
505
+ result = multierror .Append (result , err )
506
+ }
507
+ }
508
+
509
+ return result .ErrorOrNil ()
485
510
}
486
511
487
512
func setShimOOMScore (shimPid int ) error {
0 commit comments