瀏覽代碼

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 10 年之前
父節點
當前提交
300c51c3a4
共有 1 個文件被更改,包括 9 次插入10 次删除
  1. 9 10
      daemon/container.go

+ 9 - 10
daemon/container.go

@@ -441,7 +441,7 @@ func (container *Container) buildHostnameAndHostsFiles(IP string) error {
 	return container.buildHostsFiles(IP)
 	return container.buildHostsFiles(IP)
 }
 }
 
 
-func (container *Container) AllocateNetwork() (err error) {
+func (container *Container) AllocateNetwork() error {
 	mode := container.hostConfig.NetworkMode
 	mode := container.hostConfig.NetworkMode
 	if container.Config.NetworkDisabled || !mode.IsPrivate() {
 	if container.Config.NetworkDisabled || !mode.IsPrivate() {
 		return nil
 		return nil
@@ -449,6 +449,7 @@ func (container *Container) AllocateNetwork() (err error) {
 
 
 	var (
 	var (
 		env *engine.Env
 		env *engine.Env
+		err error
 		eng = container.daemon.eng
 		eng = container.daemon.eng
 	)
 	)
 
 
@@ -456,25 +457,22 @@ func (container *Container) AllocateNetwork() (err error) {
 	if env, err = job.Stdout.AddEnv(); err != nil {
 	if env, err = job.Stdout.AddEnv(); err != nil {
 		return err
 		return err
 	}
 	}
-	if err := job.Run(); err != nil {
+	if err = job.Run(); err != nil {
 		return err
 		return err
 	}
 	}
 
 
 	// Error handling: At this point, the interface is allocated so we have to
 	// Error handling: At this point, the interface is allocated so we have to
 	// make sure that it is always released in case of error, otherwise we
 	// make sure that it is always released in case of error, otherwise we
 	// might leak resources.
 	// might leak resources.
-	defer func() {
-		if err != nil {
-			eng.Job("release_interface", container.ID).Run()
-		}
-	}()
 
 
 	if container.Config.PortSpecs != nil {
 	if container.Config.PortSpecs != nil {
-		if err := migratePortMappings(container.Config, container.hostConfig); err != nil {
+		if err = migratePortMappings(container.Config, container.hostConfig); err != nil {
+			eng.Job("release_interface", container.ID).Run()
 			return err
 			return err
 		}
 		}
 		container.Config.PortSpecs = nil
 		container.Config.PortSpecs = nil
-		if err := container.WriteHostConfig(); err != nil {
+		if err = container.WriteHostConfig(); err != nil {
+			eng.Job("release_interface", container.ID).Run()
 			return err
 			return err
 		}
 		}
 	}
 	}
@@ -503,7 +501,8 @@ func (container *Container) AllocateNetwork() (err error) {
 	container.NetworkSettings.PortMapping = nil
 	container.NetworkSettings.PortMapping = nil
 
 
 	for port := range portSpecs {
 	for port := range portSpecs {
-		if err := container.allocatePort(eng, port, bindings); err != nil {
+		if err = container.allocatePort(eng, port, bindings); err != nil {
+			eng.Job("release_interface", container.ID).Run()
 			return err
 			return err
 		}
 		}
 	}
 	}