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

Fix possible race condition on request ctx done #1662

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,9 +430,12 @@
mu sync.Mutex
open int32
stop int32

done chan struct{}

rejectedRequestsCount uint32
rerjectedRequestsCount uint32

closeDone sync.Once
}

// TimeoutHandler creates RequestHandler, which returns StatusRequestTimeout
Expand Down Expand Up @@ -1835,7 +1838,7 @@
atomic.AddInt32(&s.open, 1)
if !wp.Serve(c) {
atomic.AddInt32(&s.open, -1)
atomic.AddUint32(&s.rejectedRequestsCount, 1)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, ubuntu-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, ubuntu-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, ubuntu-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, ubuntu-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, ubuntu-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, ubuntu-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, macos-14)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, macos-14)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, macos-14)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, macos-14)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / lint

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / lint

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / lint

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / lint

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, macos-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, macos-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, macos-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, macos-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, macos-14)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, macos-14)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, macos-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, macos-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, macos-14)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, macos-14)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, macos-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, macos-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, windows-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, windows-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, windows-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, windows-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, windows-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, windows-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, windows-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 1841 in server.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, windows-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)
s.writeFastError(c, StatusServiceUnavailable,
"The connection cannot be served because Server.Concurrency limit exceeded")
c.Close()
Expand Down Expand Up @@ -1900,7 +1903,9 @@
}

if s.done != nil {
close(s.done)
s.closeDone.Do(func() {
close(s.done)
})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to reset s.closeDone when s.done is reset. This change now makes it impossible to reuse the Server struct after closing it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh I see, and since you want to reuse the Server struct, you may create the s.done again

I must think about this, because if I reset s.closeDone we may ended with the same code and same behaviour

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mean here exactly. You have to reset it when a Serve is called I think. That way you don't have the same behaviour anymore, only when you reuse the Server struct and then it's intended.

Copy link
Contributor

@byte0o byte0o Jun 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erikdubbelboer @peczenyj I think it can be modified like this

func (ctx *RequestCtx) Done() <-chan struct{} {
	//fix  Use locks to prevent concurrent modifications, 
	//and use new variables to prevent panic caused by modifying the original done chan to nil.
	ctx.s.mu.Lock()
	defer ctx.s.mu.Unlock()
	if ctx.s.done == nil {
		tmp := make(chan struct{}, 1)
		tmp <- struct{}{}
		return tmp
	}
	doneChan := ctx.s.done
	return doneChan
}
func (ctx *RequestCtx) Err() error {
	select {
	case <-ctx.Done(): // //fix  Use unified functions instead of reference variables to converge fetching into one place
		return context.Canceled
	default:
		return nil
	}
}

}

// Closing the listener will make Serve() call Stop on the worker pool.
Expand All @@ -1927,7 +1932,6 @@
}
}

s.done = nil
s.ln = nil
return err
}
Expand Down Expand Up @@ -2085,7 +2089,7 @@
//
// This function is intended be used by monitoring systems.
func (s *Server) GetRejectedConnectionsCount() uint32 {
return atomic.LoadUint32(&s.rejectedRequestsCount)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, ubuntu-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, ubuntu-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, ubuntu-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, ubuntu-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, ubuntu-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, ubuntu-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, macos-14)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, macos-14)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, macos-14)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, macos-14)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / lint

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)) (typecheck)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / lint

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)) (typecheck)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / lint

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount) (typecheck)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, macos-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, macos-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, macos-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, macos-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, macos-14)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, macos-14)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, macos-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, macos-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, macos-14)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, macos-14)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, macos-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, macos-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, windows-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, windows-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, windows-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, windows-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, windows-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, windows-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, windows-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)

Check failure on line 2092 in server.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, windows-latest)

s.rejectedRequestsCount undefined (type *Server has no field or method rejectedRequestsCount)
}

func (s *Server) getConcurrency() int {
Expand Down
Loading