Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use sync.Pool for json.NewEncoder target buffers #421

Merged
merged 2 commits into from
Sep 12, 2021
Merged

Don't use sync.Pool for json.NewEncoder target buffers #421

merged 2 commits into from
Sep 12, 2021

Commits on Apr 10, 2021

  1. Don't release buf to sync.Pool to early

    The slice returned by `noescapeJSONMarshal` continues to be accessed
    even after `pool.Put` was called on the buffer. This might lead to
    incorrect data being sent in request body if the buffer is acquired and
    re-written by a concurrently running goroutine.
    
    So make sure we release the buffer once the request completes.
    pborzenkov committed Apr 10, 2021
    Configuration menu
    Copy the full SHA
    882ef52 View commit details
    Browse the repository at this point in the history
  2. Release request body buf back to sync.Pool only after body is closed

    net/http.RoundTripper may access request body in a separate goroutine,
    so we need to wait release the buf back to sync.Pool only after the
    body is closed. From the docs:
    
      	// RoundTrip must always close the body, including on errors,
    	// but depending on the implementation may do so in a separate
    	// goroutine even after RoundTrip returns. This means that
    	// callers wanting to reuse the body for subsequent requests
    	// must arrange to wait for the Close call before doing so.
    pborzenkov committed Apr 10, 2021
    Configuration menu
    Copy the full SHA
    8d2ac82 View commit details
    Browse the repository at this point in the history