Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,20 @@ func (c *Client) NewUploadRequest(urlStr string, reader io.Reader, size int64, m
return nil, err
}

req, err := http.NewRequest("POST", u.String(), reader)
requestBody := reader
if reader != nil {
// Wrap the provided reader so transport code does not observe concrete body types
// (for example *os.File) and switch to platform-specific sendfile fast paths.
//
// Why this exists:
// race-enabled test runs on Windows have surfaced data races in the sendfile path
// while request read/write loops run concurrently. Hiding concrete type information
// keeps uploads on the generic io.Reader copy path, which is race-stable and preserves
// request semantics (same bytes, same headers, same content length).
requestBody = uploadRequestBodyReader{Reader: reader}
}
Comment on lines +639 to +650
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wrapper forces uploads onto the generic copy path in all builds and on all platforms. Could we limit this only to Windows, where the issue exists?

For Windows: wrap into uploadRequestBodyReader, for non-Windows use the original reader.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch on the sendfile drop. tbh I'm leaning against splitting this into OS-specific build tags though.

since this is an API wrapper and not a CDN, the perf delta between kernel sendfile and a user-space copy for a github release asset is basically zero. maintaining github_windows.go and github_unix.go just to save a microsecond of cpu isn't really worth the friction.

also, the root cause is mostly just the Go -race detector freaking out on windows sockets, not a production bug. sticking to the generic copy path keeps the architecture clean across the board.

what do you think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we limit this only to Windows, where the issue exists?

I was taking the "safe route" thinking that if the data race could happen on Windows using the battle-tested Go standard library, then what is to say that it could not happen on other platforms?

Additionally, we have seen some "flaky tests" in this repo over the last year and I was hoping that this fix might eliminate them entirely.

I'm open to experimenting by changing this to a Windows-only fix if someone wants to make a PR, with the understanding (as always, actually) that we may need to revert it.


req, err := http.NewRequest("POST", u.String(), requestBody)
if err != nil {
return nil, err
}
Expand All @@ -658,6 +671,12 @@ func (c *Client) NewUploadRequest(urlStr string, reader io.Reader, size int64, m
return req, nil
}

// uploadRequestBodyReader intentionally wraps an io.Reader to hide concrete reader types.
// See NewUploadRequest for why this prevents race-prone transport optimizations.
type uploadRequestBodyReader struct {
io.Reader
}

// Response is a GitHub API response. This wraps the standard http.Response
// returned from GitHub and provides convenient access to things like
// pagination links.
Expand Down
23 changes: 23 additions & 0 deletions github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"errors"
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"net/url"
Expand All @@ -31,6 +32,17 @@ const (
baseURLPath = "/api-v3"
)

// raceSafeTestConn wraps a net.Conn to hide concrete connection types such as *net.TCPConn.
//
// Go's HTTP transport may enable OS-level sendfile optimizations when it sees a concrete
// TCP connection and an *os.File request body. Under the race detector on Windows, that
// specific optimized path can trigger a known data race in internal polling structures.
// Returning this wrapper from DialContext keeps behavior identical for tests while forcing
// the transport onto the generic copy path, which is stable under -race.
type raceSafeTestConn struct {
net.Conn
}

// setup sets up a test HTTP server along with a github.Client that is
// configured to talk to that test server. Tests should register handlers on
// mux which provide mock responses for the API method being tested.
Expand All @@ -57,8 +69,19 @@ func setup(t *testing.T) (client *Client, mux *http.ServeMux, serverURL string)
// server is a test HTTP server used to provide mock API responses.
server := httptest.NewServer(apiHandler)

testDialer := &net.Dialer{Timeout: 30 * time.Second}

// Create a custom transport with isolated connection pool
transport := &http.Transport{
// Wrap dialed connections so transport does not take concrete-TCP sendfile fast paths
// that can race under Windows + -race in upload tests.
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
conn, err := testDialer.DialContext(ctx, network, addr)
if err != nil {
return nil, err
}
return &raceSafeTestConn{Conn: conn}, nil
},
// Controls connection reuse - false allows reuse, true forces new connections for each request
DisableKeepAlives: false,
// Maximum concurrent connections per host (active + idle)
Expand Down
Loading