@@ -135,10 +135,11 @@ type Cluster struct {
135
135
// helps in identifying the attachment ID via the taskID and the
136
136
// corresponding attachment configuration obtained from the manager.
137
137
type attacher struct {
138
- taskID string
139
- config * network.NetworkingConfig
140
- attachWaitCh chan * network.NetworkingConfig
141
- detachWaitCh chan struct {}
138
+ taskID string
139
+ config * network.NetworkingConfig
140
+ attachWaitCh chan * network.NetworkingConfig
141
+ attachCompleteCh chan struct {}
142
+ detachWaitCh chan struct {}
142
143
}
143
144
144
145
type node struct {
@@ -1262,12 +1263,24 @@ func (c *Cluster) WaitForDetachment(ctx context.Context, networkName, networkID,
1262
1263
agent := c .node .Agent ()
1263
1264
c .RUnlock ()
1264
1265
1265
- if ok && attacher != nil && attacher .detachWaitCh != nil {
1266
+ if ok && attacher != nil &&
1267
+ attacher .detachWaitCh != nil &&
1268
+ attacher .attachCompleteCh != nil {
1269
+ // Attachment may be in progress still so wait for
1270
+ // attachment to complete.
1266
1271
select {
1267
- case <- attacher .detachWaitCh :
1272
+ case <- attacher .attachCompleteCh :
1268
1273
case <- ctx .Done ():
1269
1274
return ctx .Err ()
1270
1275
}
1276
+
1277
+ if attacher .taskID == taskID {
1278
+ select {
1279
+ case <- attacher .detachWaitCh :
1280
+ case <- ctx .Done ():
1281
+ return ctx .Err ()
1282
+ }
1283
+ }
1271
1284
}
1272
1285
1273
1286
return agent .ResourceAllocator ().DetachNetwork (ctx , taskID )
@@ -1289,9 +1302,11 @@ func (c *Cluster) AttachNetwork(target string, containerID string, addresses []s
1289
1302
agent := c .node .Agent ()
1290
1303
attachWaitCh := make (chan * network.NetworkingConfig )
1291
1304
detachWaitCh := make (chan struct {})
1305
+ attachCompleteCh := make (chan struct {})
1292
1306
c .attachers [aKey ] = & attacher {
1293
- attachWaitCh : attachWaitCh ,
1294
- detachWaitCh : detachWaitCh ,
1307
+ attachWaitCh : attachWaitCh ,
1308
+ attachCompleteCh : attachCompleteCh ,
1309
+ detachWaitCh : detachWaitCh ,
1295
1310
}
1296
1311
c .Unlock ()
1297
1312
@@ -1306,6 +1321,11 @@ func (c *Cluster) AttachNetwork(target string, containerID string, addresses []s
1306
1321
return nil , fmt .Errorf ("Could not attach to network %s: %v" , target , err )
1307
1322
}
1308
1323
1324
+ c .Lock ()
1325
+ c .attachers [aKey ].taskID = taskID
1326
+ close (attachCompleteCh )
1327
+ c .Unlock ()
1328
+
1309
1329
logrus .Debugf ("Successfully attached to network %s with tid %s" , target , taskID )
1310
1330
1311
1331
var config * network.NetworkingConfig
@@ -1316,7 +1336,6 @@ func (c *Cluster) AttachNetwork(target string, containerID string, addresses []s
1316
1336
}
1317
1337
1318
1338
c .Lock ()
1319
- c .attachers [aKey ].taskID = taskID
1320
1339
c .attachers [aKey ].config = config
1321
1340
c .Unlock ()
1322
1341
return config , nil
0 commit comments