Skip to content

superfluous response.WriteHeader call still happens #7429

Open
@Zurvarian

Description

@Zurvarian

Description

As per release of https://github.com/open-telemetry/opentelemetry-go-contrib/releases/tag/v1.30.0
otelmux v0.55.0 has a fix to avoid superfluous calls to response.WriteHeader

However, even when using that version I still can see logs saying: http: superfluous response.WriteHeader call

And the log points towards: go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux.getRRW.func2.1
The code as seen in the library is as following.

func getRRW(writer http.ResponseWriter) *recordingResponseWriter {
	rrw := rrwPool.Get().(*recordingResponseWriter)
	rrw.written = false
	rrw.status = http.StatusOK
	rrw.writer = httpsnoop.Wrap(writer, httpsnoop.Hooks{
		Write: func(next httpsnoop.WriteFunc) httpsnoop.WriteFunc {
			return func(b []byte) (int, error) {
				if !rrw.written {
					rrw.written = true
				}
				return next(b)
			}
		},
		WriteHeader: func(next httpsnoop.WriteHeaderFunc) httpsnoop.WriteHeaderFunc {
			return func(statusCode int) {
				if !rrw.written {
					rrw.written = true
					rrw.status = statusCode
				}
				next(statusCode)
			}
		},
	})
	return rrw
}

In the above code there is a call to rrw.status = statusCode that is not protected with any semaphore, what makes me think this code can be invoked in a non thread-safe way, causing the log issue.
I'm not expert in OTEL code, so I can be wrong, though, simply it looks like the most obvious candidate.

Environment

  • OS: linux
  • Architecture: amd64
  • Go Version: 1.23
  • otelmux version: v0.55.0

Steps To Reproduce

  1. Using this code the otelmux in a HTTP Server
  2. Run any test endpoint
  3. See error when pushing the metrics into the collector.

Expected behavior

I expect no logs to be shown due to superfluous calls to response.WriteHeader.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions