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 server open connection leaks #15

Merged
merged 1 commit into from
Jan 5, 2021

Conversation

tengattack
Copy link
Contributor

@tengattack tengattack commented Jan 3, 2021

Hi,

I think we should close connection & sig in the server after io.ReadFull errors.

Some healthcheck method (K8s, AWS ELB Target Groups, etc.) will close connection and trigger the EOF error here.

@cla-checker-service
Copy link

cla-checker-service bot commented Jan 3, 2021

💚 CLA has been signed

@tengattack tengattack changed the title Fix server open fd leaks Fix server open connection leaks Jan 3, 2021
@andresrc
Copy link

andresrc commented Jan 4, 2021

@elastic/agent

@ruflin ruflin requested a review from urso January 4, 2021 11:49
@@ -238,6 +238,8 @@ func (s *server) handle(client net.Conn) {
go func() {
var buf [1]byte
if _, err := io.ReadFull(client, buf[:]); err != nil {
close(sig)
client.Close()
Copy link

Choose a reason for hiding this comment

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

Nice catch. We should always close sig. How about changing the go function to always close sig via defer? In that case the second go-routine will do the client.Close always.

e.g.

go func() {
  defer close(sig)
  
  var buf [1]byte
  // keep rest as is
}()

Actually we might consider to remove the client.Close at the end, as the second go-routine in handle waits for a) the server shutting down or b) the handling go-routine having finished. If sig is closed properly we will always close the client.

Copy link
Contributor Author

@tengattack tengattack Jan 4, 2021

Choose a reason for hiding this comment

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

Sure, defer would be better here. Actually, the second go-routine will not do the client.Close after sig closed as it placed in a difference case and the client connection will be used in the channel. So I kept the client.Close here.

@urso
Copy link

urso commented Jan 5, 2021

Change looks good. Thanks.

@urso urso merged commit add1e4e into elastic:master Jan 5, 2021
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

Successfully merging this pull request may close these issues.

3 participants