Skip to content

Commit d732547

Browse files
authored
Fix chunk encoding issue (#93)
Root cause: The .Flush() was called after releasing the write mutex lock (since defers are called in reverse order). This allowed concurrent writes to the http.ResponseWriter, which in turn caused stdlib's chunk encoder to produce invalid sequence of data. The JavaScript client then failed with ERR_INVALID_CHUNKED_ENCODING error (stream lost) in the browser.
1 parent 8b43a3c commit d732547

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

server.go.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,15 @@ func (s *{{$serviceName}}) serve{{firstLetterToUpper $method.Name}}JSONStream(ct
206206

207207
// Call service method implementation.
208208
if err {{if or (eq (len $method.Inputs) 0) (gt (len $method.Outputs) 0)}}:{{end}}= s.{{$name}}.{{$method.Name}}(ctx{{range $i, $_ := $method.Inputs}}, reqPayload.Arg{{$i}}{{end}}, streamWriter); err != nil {
209+
cancel()
209210
rpcErr, ok := err.(WebRPCError)
210211
if !ok {
211212
rpcErr = ErrWebrpcEndpoint.WithCause(err)
212213
}
214+
streamWriter.mu.Lock()
213215
streamWriter.sendError(w, r, rpcErr)
216+
streamWriter.f.Flush()
217+
streamWriter.mu.Unlock()
214218
return
215219
}
216220
}

types.go.tmpl

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,22 +150,21 @@ func (w *streamWriter) keepAlive(ctx context.Context) {
150150
}
151151

152152
func (w *streamWriter) ping() error {
153-
defer w.f.Flush()
154-
155153
w.mu.Lock()
156-
defer w.mu.Unlock()
157-
158154
_, err := w.w.Write([]byte("\n"))
155+
w.f.Flush()
156+
w.mu.Unlock()
157+
159158
return err
160159
}
161160

162161
func (w *streamWriter) write(respPayload interface{}) error {
163-
defer w.f.Flush()
164-
165162
w.mu.Lock()
166-
defer w.mu.Unlock()
163+
err := w.e.Encode(respPayload)
164+
w.f.Flush()
165+
w.mu.Unlock()
167166

168-
return w.e.Encode(respPayload)
167+
return err
169168
}
170169
{{- end }}
171170

0 commit comments

Comments
 (0)