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

accept tcp4 0.0.0.0:6988: use of closed network connection #1363

Closed
un000 opened this issue Aug 25, 2022 · 2 comments
Closed

accept tcp4 0.0.0.0:6988: use of closed network connection #1363

un000 opened this issue Aug 25, 2022 · 2 comments

Comments

@un000
Copy link
Contributor

un000 commented Aug 25, 2022

Hey, there is a problem when service stops and calls .Shutdown(). ListenAndServe errors with a:

{"level":"error","ts":"2022-08-25T10:30:36.236Z","caller":"http/server.go:256","msg":"error closing server","version":"ce33dc19", "system":"http","error":"accept tcp4 0.0.0.0:6988: use of closed network connection"}

ListenAndServe started returning errors after an upgrading fasthttp from 1.31 -> 1.39. Then I've added IdleTimeout,ReadTimeout, WriteTimeout and it continues doing error. Fasthttp app is behind nginx with keep-alives.

Before to stop fasthttp server, k8s deregister app from DNS, then nginx is closing connections in 2 seconds, then server waits 10 seconds to call .Shutdown().

&fasthttp.Server{
		NoDefaultDate:                true,
		NoDefaultServerHeader:        true,
		NoDefaultContentType:         true,
		DisablePreParseMultipartForm: true,
		TCPKeepalive:                 true,
		TCPKeepalivePeriod:           10 * time.Second,
		ReadTimeout:                  10 * time.Second,
		WriteTimeout:                 10 * time.Second,
		IdleTimeout:                  5 * time.Second,
		ReadBufferSize:               2 << 14,
		MaxRequestsPerConn:           2 << 16,
	}
func (s *Server) Run() {
	go func() {
		s.logger.Info("starting http server", zap.String("address", s.config.Address))
		go middlewares.HTTPServerStatMonitor(s.server)

		if err := s.server.ListenAndServe(s.config.Address); err != nil {
			if strings.Contains(err.Error(), "use of closed network connection") {
				s.logger.Error("error closing server", field.Error(err))
			} else {
				s.logger.Fatal("error while starting http server", field.Error(err))
			}
		}
	}()
}

func (s *Server) Stop(timeout time.Duration) {
        time.Sleep(10 * time.Second)
	done := make(chan struct{})
	go func() {
		if err := s.server.Shutdown(); err != nil {
			s.logger.Error("error stopping server", field.Error(err))
		}
		close(done)
	}()

	ctx, cancel := context.WithTimeout(context.Background(), timeout)
	defer cancel()
	select {
	case <-ctx.Done():
		s.logger.Error("server is not gracefully stopped")
		return
	case <-done:
		s.logger.Info("server stopped")
		return
	}
}

How to prevent this behaviour?

@un000 un000 changed the title use of closed network connection accept tcp4 0.0.0.0:6988: use of closed network connection Aug 25, 2022
@un000
Copy link
Contributor Author

un000 commented Aug 25, 2022

It reproduces from fasthttp v1.38+ (Consolidate TCPKeepalive in server.Serve (#1320) #1324 )

This code doesn't check "use of closed network connection" as it was before
https://github.com/valyala/fasthttp/pull/1324/files#diff-366e46a40f6f60b4f7614eb0976bb51820364bf5ca6ccc4787eb49d7bdbef3e6R1869

@erikdubbelboer
Copy link
Collaborator

You are right, good find. I fixed it in: 28bec71

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants