Browse code

Fix non exiting client issue

Guillaume J. Charmes authored on 2013/05/08 15:32:17
Showing 3 changed files
... ...
@@ -97,9 +97,9 @@ func getContainersExport(srv *Server, w http.ResponseWriter, r *http.Request) ([
97 97
 
98 98
 	in, out, err := hijackServer(w)
99 99
 	if err != nil {
100
-		defer in.Close()
101 100
 		return nil, err
102 101
 	}
102
+	defer in.Close()
103 103
 	fmt.Fprintf(out, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
104 104
 	if err := srv.ContainerExport(name, out); err != nil {
105 105
 		fmt.Fprintf(out, "Error: %s\n", err)
... ...
@@ -117,9 +117,9 @@ func getImages(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, err
117 117
 	if viz {
118 118
 		in, out, err := hijackServer(w)
119 119
 		if err != nil {
120
-			defer in.Close()
121 120
 			return nil, err
122 121
 		}
122
+		defer in.Close()
123 123
 		fmt.Fprintf(out, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
124 124
 		if err := srv.ImagesViz(out); err != nil {
125 125
 			fmt.Fprintf(out, "Error: %s\n", err)
... ...
@@ -269,9 +269,9 @@ func postImages(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, er
269 269
 
270 270
 	in, out, err := hijackServer(w)
271 271
 	if err != nil {
272
-		defer in.Close()
273 272
 		return nil, err
274 273
 	}
274
+	defer in.Close()
275 275
 	fmt.Fprintf(out, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
276 276
 	if image != "" { //pull
277 277
 		registry := r.Form.Get("registry")
... ...
@@ -317,9 +317,9 @@ func postImagesInsert(srv *Server, w http.ResponseWriter, r *http.Request) ([]by
317 317
 
318 318
 	in, out, err := hijackServer(w)
319 319
 	if err != nil {
320
-		defer in.Close()
321 320
 		return nil, err
322 321
 	}
322
+	defer in.Close()
323 323
 	fmt.Fprintf(out, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
324 324
 	if err := srv.ImageInsert(name, url, path, out); err != nil {
325 325
 		fmt.Fprintf(out, "Error: %s\n", err)
... ...
@@ -340,9 +340,9 @@ func postImagesPush(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte
340 340
 
341 341
 	in, out, err := hijackServer(w)
342 342
 	if err != nil {
343
-		defer in.Close()
344 343
 		return nil, err
345 344
 	}
345
+	defer in.Close()
346 346
 	fmt.Fprintf(out, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
347 347
 	if err := srv.ImagePush(name, registry, out); err != nil {
348 348
 		fmt.Fprintln(out, "Error: %s\n", err)
... ...
@@ -354,9 +354,9 @@ func postImagesPush(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte
354 354
 func postBuild(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
355 355
 	in, out, err := hijackServer(w)
356 356
 	if err != nil {
357
-		defer in.Close()
358 357
 		return nil, err
359 358
 	}
359
+	defer in.Close()
360 360
 	fmt.Fprintf(out, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
361 361
 	if err := srv.ImageCreateFromFile(in, out); err != nil {
362 362
 		fmt.Fprintln(out, "Error: %s\n", err)
... ...
@@ -487,9 +487,9 @@ func postContainersAttach(srv *Server, w http.ResponseWriter, r *http.Request) (
487 487
 
488 488
 	in, out, err := hijackServer(w)
489 489
 	if err != nil {
490
-		defer in.Close()
491 490
 		return nil, err
492 491
 	}
492
+	defer in.Close()
493 493
 
494 494
 	fmt.Fprintf(out, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
495 495
 	if err := srv.ContainerAttach(name, logs, stream, stdin, stdout, stderr, in, out); err != nil {
... ...
@@ -1140,7 +1140,6 @@ func CmdRun(args ...string) error {
1140 1140
 			return err
1141 1141
 		}
1142 1142
 	}
1143
-
1144 1143
 	if !config.AttachStdout && !config.AttachStderr {
1145 1144
 		fmt.Println(out.Id)
1146 1145
 	}
... ...
@@ -497,66 +497,66 @@ func (srv *Server) ContainerWait(name string) (int, error) {
497 497
 }
498 498
 
499 499
 func (srv *Server) ContainerAttach(name string, logs, stream, stdin, stdout, stderr bool, in io.ReadCloser, out io.Writer) error {
500
-	if container := srv.runtime.Get(name); container != nil {
501
-		//logs
502
-		if logs {
503
-			if stdout {
504
-				cLog, err := container.ReadLog("stdout")
505
-				if err != nil {
506
-					Debugf(err.Error())
507
-				} else if _, err := io.Copy(out, cLog); err != nil {
508
-					Debugf(err.Error())
509
-				}
500
+	container := srv.runtime.Get(name)
501
+	if container == nil {
502
+		return fmt.Errorf("No such container: %s", name)
503
+	}
504
+	//logs
505
+	if logs {
506
+		if stdout {
507
+			cLog, err := container.ReadLog("stdout")
508
+			if err != nil {
509
+				Debugf(err.Error())
510
+			} else if _, err := io.Copy(out, cLog); err != nil {
511
+				Debugf(err.Error())
510 512
 			}
511
-			if stderr {
512
-				cLog, err := container.ReadLog("stderr")
513
-				if err != nil {
514
-					Debugf(err.Error())
515
-				} else if _, err := io.Copy(out, cLog); err != nil {
516
-					Debugf(err.Error())
517
-				}
513
+		}
514
+		if stderr {
515
+			cLog, err := container.ReadLog("stderr")
516
+			if err != nil {
517
+				Debugf(err.Error())
518
+			} else if _, err := io.Copy(out, cLog); err != nil {
519
+				Debugf(err.Error())
518 520
 			}
519 521
 		}
522
+	}
520 523
 
521
-		//stream
522
-		if stream {
523
-			if container.State.Ghost {
524
-				return fmt.Errorf("Impossible to attach to a ghost container")
525
-			}
524
+	//stream
525
+	if stream {
526
+		if container.State.Ghost {
527
+			return fmt.Errorf("Impossible to attach to a ghost container")
528
+		}
526 529
 
527
-			var (
528
-				cStdin           io.ReadCloser
529
-				cStdout, cStderr io.Writer
530
-				cStdinCloser     io.Closer
531
-			)
532
-
533
-			if stdin {
534
-				r, w := io.Pipe()
535
-				go func() {
536
-					defer w.Close()
537
-					defer Debugf("Closing buffered stdin pipe")
538
-					io.Copy(w, in)
539
-				}()
540
-				cStdin = r
541
-				cStdinCloser = in
542
-			}
543
-			if stdout {
544
-				cStdout = out
545
-			}
546
-			if stderr {
547
-				cStderr = out
548
-			}
530
+		var (
531
+			cStdin           io.ReadCloser
532
+			cStdout, cStderr io.Writer
533
+			cStdinCloser     io.Closer
534
+		)
549 535
 
550
-			<-container.Attach(cStdin, cStdinCloser, cStdout, cStderr)
536
+		if stdin {
537
+			r, w := io.Pipe()
538
+			go func() {
539
+				defer w.Close()
540
+				defer Debugf("Closing buffered stdin pipe")
541
+				io.Copy(w, in)
542
+			}()
543
+			cStdin = r
544
+			cStdinCloser = in
545
+		}
546
+		if stdout {
547
+			cStdout = out
548
+		}
549
+		if stderr {
550
+			cStderr = out
551
+		}
551 552
 
552
-			// If we are in stdinonce mode, wait for the process to end
553
-			// otherwise, simply return
554
-			if container.Config.StdinOnce && !container.Config.Tty {
555
-				container.Wait()
556
-			}
553
+		<-container.Attach(cStdin, cStdinCloser, cStdout, cStderr)
554
+
555
+		// If we are in stdinonce mode, wait for the process to end
556
+		// otherwise, simply return
557
+		if container.Config.StdinOnce && !container.Config.Tty {
558
+			container.Wait()
557 559
 		}
558
-	} else {
559
-		return fmt.Errorf("No such container: %s", name)
560 560
 	}
561 561
 	return nil
562 562
 }