Skip to content

Panic when flushing http response in engine #10228

Closed

Description

@stevvooe

Various crashes have been seen in the daemon with the following stack trace when pushing:

goroutine 92 [running]:
bufio.(*Writer).flush(0xc208208900, 0x0, 0x0)
    /usr/local/go/src/bufio/bufio.go:530 +0xda
bufio.(*Writer).Flush(0xc208208900, 0x0, 0x0)
    /usr/local/go/src/bufio/bufio.go:519 +0x3a
net/http.(*response).Flush(0xc2085bfae0)
    /usr/local/go/src/net/http/server.go:1047 +0x4c
github.com/docker/docker/utils.(*WriteFlusher).Write(0xc2089f5350, 0xc208ccc140, 0xce, 0x135, 0xce, 0x0, 0x0)
    /go/src/github.com/docker/docker/utils/utils.go:266 +0x140
github.com/docker/docker/engine.(*Output).Write(0xc20860ab80, 0xc208ccc140, 0xce, 0x135, 0x0, 0x0, 0x0)
    /go/src/github.com/docker/docker/engine/streams.go:90 +0x17f
github.com/docker/docker/utils.(*WriteFlusher).Write(0xc208a05890, 0xc208ccc140, 0xce, 0x135, 0x0, 0x0, 0x0)
    /go/src/github.com/docker/docker/utils/utils.go:265 +0xf2
github.com/docker/docker/utils.(*progressReader).Close(0xc208832500, 0x0, 0x0)
    /go/src/github.com/docker/docker/utils/progressreader.go:42 +0xf1
net/http.(*Request).closeBody(0xc2087e4750)
    /usr/local/go/src/net/http/request.go:919 +0x42
net/http.(*persistConn).writeLoop(0xc20866c160)
    /usr/local/go/src/net/http/transport.go:957 +0x307
created by net/http.(*Transport).dialConn
    /usr/local/go/src/net/http/transport.go:661 +0xcbc

Full trace here, originally here.

The cause is unknown but seems to be related to missing io.Writer target on a *bufio.Writer.

This may also be related to #10169.

@tiborvass @dmcgowan @dmp42

Activity

  1. BrianBland commented on Jan 20, 2015

    @BrianBland
    Contributor

    I received the same issue when pushing (504 error on layer upload). Stack trace here.

  2. stevvooe commented on Jan 20, 2015

    @stevvooe
    ContributorAuthor

    For reference, @BrianBland encountered this panic with the binaries from here: #9784 (comment).

  3. added this to the 1.5.0 milestone on Jan 20, 2015
  4. stevvooe commented on Jan 21, 2015

    @stevvooe
    ContributorAuthor

    The issue seems to be a double close on the value of (*engine.Job).Stdout when calling (*TagStore).pushV2Repository from (*TagStore).CmdPush. job.Stdout is allocated as a (*utils.WriteFlusher) and flows into (*Session).PutV2ImageBlob through the out argument of ProgressReader. When the job completes, during some server error, the call stack returns early, closing all of the output streams. Because (*engine.Job).Stdout was associated with an *http.Request in (*Session).PutV2ImageBlob, the http package also attempts to shutdown the writer, resulting in a panic. The confusing aspect here is that (*engine.Job).Stdout, an io.Writer is linked to the lifecycle of the http.Request through the ProgressReader.

    The simplest solution is something like this:

    diff --git a/registry/session_v2.go b/registry/session_v2.go
    index 11b96bd..fa02bd3 100644
    --- a/registry/session_v2.go
    +++ b/registry/session_v2.go
    @@ -226,7 +226,7 @@ func (r *Session) PutV2ImageBlob(ep *Endpoint, imageName, sumType, sumStr string
    
            method := "PUT"
            log.Debugf("[registry] Calling %q %s", method, location)
    -       req, err = r.reqFactory.NewRequest(method, location, blobRdr)
    +       req, err = r.reqFactory.NewRequest(method, location, ioutil.NopCloser(blobRdr))
            if err != nil {
                    return err
            }

    This simply detaches the (*engine.Job).Stdout from the http package, leaving control over the lifecycle of the body up to the caller.

    There are other solutions that may help:

    1. Remove the use of ioutils.WriteFlusher. This object tries to synchronize writes and flushes without managing the close aspect. An all in one line buffering, that correctly synchronizes Close may help.
    2. Another solution (thanks @dmcgowan) is to allow multiple closes on *engine.Output but setting the dests field to nil after a Close, relegating subsequent closes to noops.
  5. jessfraz commented on Jan 21, 2015

    @jessfraz
    Contributor

    I wish there was a way to test it

  6. dmcgowan commented on Jan 21, 2015

    @dmcgowan
    Member

    The fix I am trying to test (still haven't been able to reproduce)

    diff --git a/engine/streams.go b/engine/streams.go
    index 99e876e..ec703c9 100644
    --- a/engine/streams.go
    +++ b/engine/streams.go
    @@ -111,6 +111,7 @@ func (o *Output) Close() error {
                    }
            }
            o.tasks.Wait()
    +       o.dests = nil
            return firstErr
     }
  7. LK4D4 commented on Jan 21, 2015

    @LK4D4
    Contributor

    @dmcgowan seems legit. Still not sure how to reproduce this :/

  8. tiborvass commented on Jan 21, 2015

    @tiborvass
    Contributor

    Have trouble reproducing it now :(

  9. tiborvass commented on Jan 21, 2015

    @tiborvass
    Contributor

    @stevvooe @dmcgowan sorry guys, I tried to reproduce it but can't. Should we still make one of those changes? /cc @jfrazelle @LK4D4 @icecrime

  10. LK4D4 commented on Jan 21, 2015

    @LK4D4
    Contributor

    @tiborvass nil-ing dests seems good thing to do. We can try.
    Reproducing of crash is pretty hard really because it's involving network failures.

  11. icecrime commented on Jan 21, 2015

    @icecrime
    Contributor

    I just looked into engine/streams.go and I see no code path where dests being nil could have unwanted side effects, so it's worth trying.

  12. stevvooe commented on Jan 21, 2015

    @stevvooe
    ContributorAuthor

    @icecrime @LK4D4 @tiborvass @dmcgowan @jfrazelle Please talk to @BrianBland about a reproduction. He has seen the crash several times.

  13. crosbymichael commented on Jan 21, 2015

    @crosbymichael
    Contributor

    Maybe a better fix would be to prevent a double close on the progress reader since it writes some final bytes to the underlying writer before doing a close, it's not re-entrant

  14. stevvooe commented on Jan 21, 2015

    @stevvooe
    ContributorAuthor

    @crosbymichael That is probably the "write" fix.

    I have a small patch to change WriteFlusher to intercept close correctly. I'll try to get that out today.

  15. dmcgowan commented on Jan 22, 2015

    @dmcgowan
    Member

    Also #9913 appears to be the same panic during a v1 registry operation

  16. stevvooe commented on Jan 22, 2015

    @stevvooe
    ContributorAuthor

    I have a minimal reproduction and have filed golang/go#9657.

  17. stevvooe commented on Jan 22, 2015

    @stevvooe
    ContributorAuthor

    @icecrime Update: bug was insta-closed. Basically, providing the noop closer in #10244 should fix the problem by leaving body lifecycle to calling function (which is actually the server-side of the http server). There is more history here: https://groups.google.com/forum/#!msg/golang-dev/o9i_SbGRrzI/n4FPqT1eLlYJ.

  18. jessfraz commented on Jan 22, 2015

    @jessfraz
    Contributor

    closed by #10244

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions