Skip to content

Commit 626a4f1

Browse files
author
lipengwei
committed
fix 1905 and add some notes
(cherry picked from commit 9d96199)
1 parent 7d41537 commit 626a4f1

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

middleware/timeout.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,19 @@ func (t echoHandlerFuncWrapper) ServeHTTP(rw http.ResponseWriter, r *http.Reques
116116
// we restore original writer only for cases we did not timeout. On timeout we have already sent response to client
117117
// and should not anymore send additional headers/data
118118
// so on timeout writer stays what http.TimeoutHandler uses and prevents writing headers/body
119-
t.ctx.Response().Writer = originalWriter
119+
// https://github.com/labstack/echo/issues/1905
120+
// error handling is a part of processes in Echo handler, if a handler returns an error
121+
// and if it did not timeout, the global error handler shall act on rw (`http.timeoutWriter` in our case)
122+
// to avoid superfluous response.WriteHeader call
120123
if err != nil {
121124
// call global error handler to write error to the client. This is needed or `http.TimeoutHandler` will send status code by itself
122125
// and after that our tries to write status code will not work anymore
123126
t.ctx.Error(err)
124127
// we pass error from handler to middlewares up in handler chain to act on it if needed. But this means that
125128
// global error handler is probably be called twice as `t.ctx.Error` already does that.
129+
// NB: later call of thr global error handler will not take any effect, as echo.Response will be marked as `committed`
130+
// when the first call take place.
131+
t.ctx.Response().Writer = originalWriter
126132
t.errChan <- err
127133
}
128134
}

0 commit comments

Comments
 (0)