有没有办法获取“http: response.WriteHeader on hijacked connection”错误的堆栈跟踪?

huangapple go评论96阅读模式
英文:

Any way to get a stack trace for 'http: response.WriteHeader on hijacked connection' errors?

问题

我们的网络应用程序在标准错误流中记录了大量的“http: response.WriteHeader on hijacked connection”消息。

有没有办法让http库输出堆栈跟踪或其他调试信息,并将其与此消息一起输出(或将其升级为错误),以便能够追踪到我们应用程序中出现此问题的位置?

英文:

Our web application is logging a large number of 'http: response.WriteHeader on hijacked connection' messages to stderr.

Is there any way to make the http library output a stack trace, or additional debugging information, along with this message, (or upgrade it to an error) to make it possible to track down where in our application this is occurring?

答案1

得分: 2

由于错误从不返回,而是直接写入http.Server.ErrorLog,因此这是你唯一可以拦截错误的地方。

你可以在调试器中运行它,并在那一点上中断,但如果这是在生产环境中运行,这可能没有用。

你可以创建一个新的*log.Logger,其中包含一个io.Writer,当遇到特定的消息时,它会将堆栈跟踪添加到输出中。

type stackWriter struct {
    w io.Writer
}
func (s stackWriter) Write(d []byte) (int, error) {
    if bytes.Contains(d, []byte("WriteHeader on hijacked connection")) {
        s.w.Write(debug.Stack())
    }
    return s.w.Write(d)
}

当然,另一种选择是修改net/http/server.go,在那一点打印堆栈跟踪。

英文:

Since the error is never returned, and written directly to the http.Server.ErrorLog, that is the only place you could intercept it.

You can run it in a debugger and break at that point, but that may not be useful if this is running in production.

You can create a new *log.Logger with an io.Writer which adds the stack trace to the output when it encounters that particular message.

type stackWriter struct {
	w io.Writer
}
func (s stackWriter) Write(d []byte) (int, error) {
	if bytes.Contains(d, []byte("WriteHeader on hijacked connection")) {
		s.w.Write(debug.Stack())
	}
	return s.w.Write(d)
}

Another option of course is to modify net/http/server.go to print the stack at that point.

答案2

得分: 0

如果您正在使用调试器,请在记录消息的行上设置一个断点(https://github.com/golang/go/blob/24c52ee57046f5e58ce6db158b0efad02ced6606/src/net/http/server.go#L1049)。从那里检查堆栈。

如果不使用调试器,则在该行上临时添加一个调用 panic 的语句,并使用 go install net/http 重新构建 net/http 包。堆栈跟踪将显示问题的原因。

另一种选择是创建一个在每次调用 Write 时调用 debug.PrintStack 的 io.Writer。使用此写入器创建一个 log.Logger(https://godoc.org/log#New)。将此记录器设置为服务器的 ErrorLog(https://godoc.org/net/http#Server.ErrorLog)。检查打印的堆栈跟踪以找到错误的原因。

英文:

If you are using a debugger, set a breakpoint on the line where the message is logged. Examine the stack from there.

If not using a debugger, then temporarily add a call to panic at the line and rebuild the net/http package with go install net/http. The stack trace will show the cause of the problem.

Another option is to create an io.Writer that calls debug.PrintStack on every call to Write. Use this writer to create a log.Logger. Set this logger as the server's ErrorLog. Examine the printed stack traces to find the cause of the error.

huangapple
  • 本文由 发表于 2017年4月22日 00:19:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/43547565.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定