Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/http: Shutdown may lose an active connection #33313

monkey92t opened this issue Jul 27, 2019 · 6 comments

net/http: Shutdown may lose an active connection #33313

monkey92t opened this issue Jul 27, 2019 · 6 comments


Copy link

@monkey92t monkey92t commented Jul 27, 2019

What version of Go are you using (go version)?

go version go1.12.4 darwin/amd64

Does this issue reproduce with the latest release?

yes go 1.12

What operating system and processor architecture are you using (go env)?

go env Output
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build931930631=/tmp/go-build -gno-record-gcc-switches -fno-common"

my english is not very good. I try to describe it.

i found a problem in http shutdown(), when the shutdown function is executed, is executed. but at this time the net accept function can still be executed normally, execute c.setState(c.rwc, StateNew) put a new connection into activeConn and wait for

when shutdown() finishes processing all activeConn connections, it will execute and end the shutdown function, c.setState() gets the lock and puts a new connection into activeConn. At this point, there will still be an active connection after the shutdown() is completed.

Copy link

@medusar medusar commented Jul 27, 2019

I have encountered the same problem.
When Shutdown method is called, it will first close the Listener and then close all the active connections, but the code in method Serve will give a chance for a connection to be ignored.
When a connection is returned by Accept but has not been added to active connection array when shutdown is closing all the active connections, then the newly established connection will not be closed.

Parts of the codes in server.Serve:

             rw, e := l.Accept()
		if e != nil {
			select {
			case <-srv.getDoneChan():
				return ErrServerClosed
		// unrelated codes are ignored

		tempDelay = 0
		c := srv.newConn(rw)
               // connection will be added to arry in setState method
		c.setState(c.rwc, StateNew) // before Serve can return
		go c.serve(ctx)

With the codes here, you will be able reproduce the problem.
Codes Of The Problem

Copy link

@medusar medusar commented Jul 29, 2019

As this issue pointed out:

If you want the server to shut down gracefully, you can use the Shutdown method. With that approach, you'd need to communicate the start of shutdown to the handlers by whatever mechanism you like (such as canceling a context.Context), and they can report an appropriate error explicitly.

It seems that they don't think this problem as a bug. Maybe we should set the shutdown flag before we call Shutdown method, and just return an error if a request is received after the server shutdown.

@julieqiu julieqiu changed the title http shutdown() may lose an active connection. net/http: Shutdown may lose an active connection. Jul 29, 2019
@julieqiu julieqiu changed the title net/http: Shutdown may lose an active connection. net/http: Shutdown may lose an active connection Jul 29, 2019
Copy link

@FiloSottile FiloSottile commented Jul 30, 2019

This does not look like a duplicate of #31438. Here the connection has not reached the handler yet.

This looks like it might be a bug.

Copy link

@bradfitz bradfitz commented Nov 15, 2019

@FiloSottile, @andybons, if this is a bug report about Go 1.12, how is this a release blocker for Go 1.14?

Copy link

@andybons andybons commented Nov 15, 2019

Hm. I didn’t notice that. Will remove release-blocker for now pending more context from @FiloSottile.

@andybons andybons modified the milestones: Go1.14, Unplanned Nov 15, 2019
Copy link

@gopherbot gopherbot commented Jan 6, 2020

Change mentions this issue: net/http: fix Server.Shutdown race where it could miss an active connection

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants