Skip to content

Commit

Permalink
Fix potential leak in Websocket configuration (#3310)
Browse files Browse the repository at this point in the history
When setting up a WebSocket connection for receiving data the generated
code would create a context cancel function that would never be called.
  • Loading branch information
raphael committed Jul 4, 2023
1 parent 444d6e8 commit bdcae1a
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 73 deletions.
24 changes: 12 additions & 12 deletions codegen/service/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ func TestClient(t *testing.T) {
DSL func()
Code string
}{
{"single", testdata.SingleEndpointDSL, testdata.SingleMethodClient},
{"use", testdata.UseEndpointDSL, testdata.UseMethodClient},
{"multiple", testdata.MultipleEndpointsDSL, testdata.MultipleMethodsClient},
{"no-payload", testdata.NoPayloadEndpointDSL, testdata.NoPayloadMethodsClient},
{"with-result", testdata.WithResultEndpointDSL, testdata.WithResultMethodClient},
{"streaming-result", testdata.StreamingResultMethodDSL, testdata.StreamingResultMethodClient},
{"streaming-result-no-payload", testdata.StreamingResultNoPayloadMethodDSL, testdata.StreamingResultNoPayloadMethodClient},
{"streaming-payload", testdata.StreamingPayloadMethodDSL, testdata.StreamingPayloadMethodClient},
{"streaming-payload-no-payload", testdata.StreamingPayloadNoPayloadMethodDSL, testdata.StreamingPayloadNoPayloadMethodClient},
{"streaming-payload-no-result", testdata.StreamingPayloadNoResultMethodDSL, testdata.StreamingPayloadNoResultMethodClient},
{"bidirectional-streaming", testdata.BidirectionalStreamingMethodDSL, testdata.BidirectionalStreamingMethodClient},
{"bidirectional-streaming-no-payload", testdata.BidirectionalStreamingNoPayloadMethodDSL, testdata.BidirectionalStreamingNoPayloadMethodClient},
{"client-single", testdata.SingleEndpointDSL, testdata.SingleMethodClient},
{"client-use", testdata.UseEndpointDSL, testdata.UseMethodClient},
{"client-multiple", testdata.MultipleEndpointsDSL, testdata.MultipleMethodsClient},
{"client-no-payload", testdata.NoPayloadEndpointDSL, testdata.NoPayloadMethodsClient},
{"client-with-result", testdata.WithResultEndpointDSL, testdata.WithResultMethodClient},
{"client-streaming-result", testdata.StreamingResultMethodDSL, testdata.StreamingResultMethodClient},
{"client-streaming-result-no-payload", testdata.StreamingResultNoPayloadMethodDSL, testdata.StreamingResultNoPayloadMethodClient},
{"client-streaming-payload", testdata.StreamingPayloadMethodDSL, testdata.StreamingPayloadMethodClient},
{"client-streaming-payload-no-payload", testdata.StreamingPayloadNoPayloadMethodDSL, testdata.StreamingPayloadNoPayloadMethodClient},
{"client-streaming-payload-no-result", testdata.StreamingPayloadNoResultMethodDSL, testdata.StreamingPayloadNoResultMethodClient},
{"client-bidirectional-streaming", testdata.BidirectionalStreamingMethodDSL, testdata.BidirectionalStreamingMethodClient},
{"client-bidirectional-streaming-no-payload", testdata.BidirectionalStreamingNoPayloadMethodDSL, testdata.BidirectionalStreamingNoPayloadMethodClient},
}
for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
Expand Down
28 changes: 14 additions & 14 deletions codegen/service/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ func TestEndpoint(t *testing.T) {
DSL func()
Code string
}{
{"single", testdata.SingleEndpointDSL, testdata.SingleEndpoint},
{"use", testdata.UseEndpointDSL, testdata.UseEndpoint},
{"multiple", testdata.MultipleEndpointsDSL, testdata.MultipleEndpoints},
{"no-payload", testdata.NoPayloadEndpointDSL, testdata.NoPayloadEndpoint},
{"with-result", testdata.WithResultEndpointDSL, testdata.WithResultEndpoint},
{"with-result-multiple-views", testdata.WithResultMultipleViewsEndpointDSL, testdata.WithResultMultipleViewsEndpoint},
{"streaming-result", testdata.StreamingResultEndpointDSL, testdata.StreamingResultMethodEndpoint},
{"streaming-result-no-payload", testdata.StreamingResultNoPayloadEndpointDSL, testdata.StreamingResultNoPayloadMethodEndpoint},
{"streaming-result-with-views", testdata.StreamingResultWithViewsMethodDSL, testdata.StreamingResultWithViewsMethodEndpoint},
{"streaming-payload", testdata.StreamingPayloadEndpointDSL, testdata.StreamingPayloadMethodEndpoint},
{"streaming-payload-no-payload", testdata.StreamingPayloadNoPayloadMethodDSL, testdata.StreamingPayloadNoPayloadMethodEndpoint},
{"streaming-payload-no-result", testdata.StreamingPayloadNoResultMethodDSL, testdata.StreamingPayloadNoResultMethodEndpoint},
{"bidirectional-streaming", testdata.BidirectionalStreamingEndpointDSL, testdata.BidirectionalStreamingMethodEndpoint},
{"bidirectional-streaming-no-payload", testdata.BidirectionalStreamingNoPayloadMethodDSL, testdata.BidirectionalStreamingNoPayloadMethodEndpoint},
{"endpoint-single", testdata.SingleEndpointDSL, testdata.SingleEndpoint},
{"endpoint-use", testdata.UseEndpointDSL, testdata.UseEndpoint},
{"endpoint-multiple", testdata.MultipleEndpointsDSL, testdata.MultipleEndpoints},
{"endpoint-no-payload", testdata.NoPayloadEndpointDSL, testdata.NoPayloadEndpoint},
{"endpoint-with-result", testdata.WithResultEndpointDSL, testdata.WithResultEndpoint},
{"endpoint-with-result-multiple-views", testdata.WithResultMultipleViewsEndpointDSL, testdata.WithResultMultipleViewsEndpoint},
{"endpoint-streaming-result", testdata.StreamingResultEndpointDSL, testdata.StreamingResultMethodEndpoint},
{"endpoint-streaming-result-no-payload", testdata.StreamingResultNoPayloadEndpointDSL, testdata.StreamingResultNoPayloadMethodEndpoint},
{"endpoint-streaming-result-with-views", testdata.StreamingResultWithViewsMethodDSL, testdata.StreamingResultWithViewsMethodEndpoint},
{"endpoint-streaming-payload", testdata.StreamingPayloadEndpointDSL, testdata.StreamingPayloadMethodEndpoint},
{"endpoint-streaming-payload-no-payload", testdata.StreamingPayloadNoPayloadMethodDSL, testdata.StreamingPayloadNoPayloadMethodEndpoint},
{"endpoint-streaming-payload-no-result", testdata.StreamingPayloadNoResultMethodDSL, testdata.StreamingPayloadNoResultMethodEndpoint},
{"endpoint-bidirectional-streaming", testdata.BidirectionalStreamingEndpointDSL, testdata.BidirectionalStreamingMethodEndpoint},
{"endpoint-bidirectional-streaming-no-payload", testdata.BidirectionalStreamingNoPayloadMethodDSL, testdata.BidirectionalStreamingNoPayloadMethodEndpoint},
}
for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions http/codegen/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,13 @@ func (c *{{ .ClientStruct }}) {{ .EndpointInit }}({{ if .MultipartRequestEncoder
return nil, goahttp.ErrRequestError("{{ .ServiceName }}", "{{ .Method.Name }}", err)
}
if c.configurer.{{ .Method.VarName }}Fn != nil {
{{- if eq .ClientWebSocket.SendName "" }}
var cancel context.CancelFunc
ctx, cancel = context.WithCancel(ctx)
conn = c.configurer.{{ .Method.VarName }}Fn(conn, cancel)
{{- else }}
conn = c.configurer.{{ .Method.VarName }}Fn(conn, nil)
{{- end }}
}
{{- if eq .ClientWebSocket.SendName "" }}
go func() {
Expand Down
Loading

0 comments on commit bdcae1a

Please sign in to comment.