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

Close Websocket Connection on Context close/cancel #1728

Merged
merged 4 commits into from
Nov 26, 2021
Merged

Close Websocket Connection on Context close/cancel #1728

merged 4 commits into from
Nov 26, 2021

Conversation

RobinCPel
Copy link
Contributor

This issue explains it all: #1727

I have:

  • Added tests covering the bug / feature (see testing)

Not needed:

  • Updated any relevant documentation (see docs)

@coveralls
Copy link

coveralls commented Nov 25, 2021

Coverage Status

Coverage increased (+0.1%) to 70.451% when pulling f74739b on RobinCPel:ws-ctx-done-close into 4581fcc on 99designs:master.

Signed-off-by: Steve Coffman <[email protected]>
Comment on lines 270 to 278
h := testserver.New()
h.AddTransport(transport.Websocket{
InitFunc: func(ctx context.Context, _ transport.InitPayload) (context.Context, error) {
newCtx, _ := context.WithTimeout(transport.AppendCloseReason(ctx, "beep boop"), time.Millisecond*5)
return newCtx, nil
},
})
srv := httptest.NewServer(h)
defer srv.Close()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
h := testserver.New()
h.AddTransport(transport.Websocket{
InitFunc: func(ctx context.Context, _ transport.InitPayload) (context.Context, error) {
newCtx, _ := context.WithTimeout(transport.AppendCloseReason(ctx, "beep boop"), time.Millisecond*5)
return newCtx, nil
},
})
srv := httptest.NewServer(h)
defer srv.Close()
h := testserver.New()
var cancel context.CancelFunc
h.AddTransport(transport.Websocket{
InitFunc: func(ctx context.Context, _ transport.InitPayload) (context.Context, error) {
var newCtx context.Context
newCtx, cancel = context.WithTimeout(transport.AppendCloseReason(ctx, "beep boop"), time.Millisecond*5)
return newCtx, nil
},
})
defer cancel()
srv := httptest.NewServer(h)
defer srv.Close()

I know it's just for a test, but what do you think about retaining the closeFunc and calling it in a defer somewhere to satisfy the linter that there's no leaks?

Copy link
Collaborator

@StevenACoffman StevenACoffman left a comment

Choose a reason for hiding this comment

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

Thanks! I hope you don't mind, but I ran gofmt and pushed that change to your remote branch. The linter is still complaining about the possible context leak, and I provided a suggestion for your consideration. Overall, this looks pretty good to me. @zdraganov do you have any thoughts?

@zdraganov
Copy link
Contributor

LGTM

@StevenACoffman StevenACoffman merged commit 497227f into 99designs:master Nov 26, 2021
@StevenACoffman
Copy link
Collaborator

Thanks so much!

@RobinCPel
Copy link
Contributor Author

And thank you for the quick support! ;)

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

Successfully merging this pull request may close these issues.

4 participants