Add a error check in postHijacked to avoid docker exec command blocking.
When user execute docker exec command, docker daemon maybe have err return because
of ExecExists check, and then the hijack stream will not be close, it can lead to
docker exec command block.
package client
import (
+ "bytes"
"crypto/tls"
"errors"
"fmt"
+ "io/ioutil"
"net"
"net/http"
"net/http/httputil"
@@ -72,13 +74,25 @@ func (cli *Client) postHijacked(ctx context.Context, path string, query url.Valu
defer clientconn.Close()
// Server hijacks the connection, error 'connection closed' expected
- _, err = clientconn.Do(req)
+ resp, err := clientconn.Do(req)
+ if err != nil {
+ return types.HijackedResponse{}, err
+ }
+ defer resp.Body.Close()
+ switch resp.StatusCode {
+ case http.StatusOK, http.StatusSwitchingProtocols:
rwc, br := clientconn.Hijack()
-
return types.HijackedResponse{Conn: rwc, Reader: br}, err
}
+ errbody, err := ioutil.ReadAll(resp.Body)
+ if err != nil {
+ return types.HijackedResponse{}, err
+ }
+ return types.HijackedResponse{}, fmt.Errorf("Error response from daemon: %s", bytes.TrimSpace(errbody))
+}
+
func tlsDial(network, addr string, config *tls.Config) (net.Conn, error) {
return tlsDialWithDialer(new(net.Dialer), network, addr, config)
}
