Browse code

Container#AllocateNetwork: Simplify error handling.

The defer logic was a little tricky and was hiding one bug: `err` was
being redefined (with `:=`) and thus it escaped the defer error checking
logic.

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>

Andrea Luzzardi authored on 2014/10/07 09:59:12
Showing 1 changed files
... ...
@@ -441,7 +441,7 @@ func (container *Container) buildHostnameAndHostsFiles(IP string) error {
441 441
 	return container.buildHostsFiles(IP)
442 442
 }
443 443
 
444
-func (container *Container) AllocateNetwork() (err error) {
444
+func (container *Container) AllocateNetwork() error {
445 445
 	mode := container.hostConfig.NetworkMode
446 446
 	if container.Config.NetworkDisabled || !mode.IsPrivate() {
447 447
 		return nil
... ...
@@ -449,6 +449,7 @@ func (container *Container) AllocateNetwork() (err error) {
449 449
 
450 450
 	var (
451 451
 		env *engine.Env
452
+		err error
452 453
 		eng = container.daemon.eng
453 454
 	)
454 455
 
... ...
@@ -456,25 +457,22 @@ func (container *Container) AllocateNetwork() (err error) {
456 456
 	if env, err = job.Stdout.AddEnv(); err != nil {
457 457
 		return err
458 458
 	}
459
-	if err := job.Run(); err != nil {
459
+	if err = job.Run(); err != nil {
460 460
 		return err
461 461
 	}
462 462
 
463 463
 	// Error handling: At this point, the interface is allocated so we have to
464 464
 	// make sure that it is always released in case of error, otherwise we
465 465
 	// might leak resources.
466
-	defer func() {
467
-		if err != nil {
468
-			eng.Job("release_interface", container.ID).Run()
469
-		}
470
-	}()
471 466
 
472 467
 	if container.Config.PortSpecs != nil {
473
-		if err := migratePortMappings(container.Config, container.hostConfig); err != nil {
468
+		if err = migratePortMappings(container.Config, container.hostConfig); err != nil {
469
+			eng.Job("release_interface", container.ID).Run()
474 470
 			return err
475 471
 		}
476 472
 		container.Config.PortSpecs = nil
477
-		if err := container.WriteHostConfig(); err != nil {
473
+		if err = container.WriteHostConfig(); err != nil {
474
+			eng.Job("release_interface", container.ID).Run()
478 475
 			return err
479 476
 		}
480 477
 	}
... ...
@@ -503,7 +501,8 @@ func (container *Container) AllocateNetwork() (err error) {
503 503
 	container.NetworkSettings.PortMapping = nil
504 504
 
505 505
 	for port := range portSpecs {
506
-		if err := container.allocatePort(eng, port, bindings); err != nil {
506
+		if err = container.allocatePort(eng, port, bindings); err != nil {
507
+			eng.Job("release_interface", container.ID).Run()
507 508
 			return err
508 509
 		}
509 510
 	}