mirror of
https://github.com/go-gitea/gitea.git
synced 2026-12-04 15:54:57 +00:00
fix(git): fall back to a plain copy when the git proxy can't splice (#40368)
On old vendor kernels like Synology's 3.10, `splice()` from a TCP socket fails with `EBADF` instead of `EINVAL`, so Go doesn't fall back to a normal copy and the git proxy's CONNECT relay dies at once, breaking every HTTPS clone, mirror and migration. The relay and the `git://` tunnel now retry with a plain copy when splice fails with `EBADF` before anything was written, so normal kernels keep using splice. Fixes: https://github.com/go-gitea/gitea/issues/40367 Assisted-by: Claude Code:claude-opus-5-5 --------- Signed-off-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: TheFox0x7 <thefox0x7@gmail.com>
This commit is contained in:
2 files changed
+23
-5
No files matched your search
@@ -22,6 +22,7 @@ import (
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"gitea.dev/modules/egress"
|
||||
@@ -146,8 +147,7 @@ func tunnel(proxyURL, target string) error {
|
||||
_, _ = io.Copy(conn, os.Stdin)
|
||||
closeWrite(conn)
|
||||
}()
|
||||
_, err = io.Copy(os.Stdout, conn)
|
||||
return err
|
||||
return copyConn(os.Stdout, conn)
|
||||
}
|
||||
|
||||
// proxyTLSConfig loads the files of git's http.proxySSL* options, like curl the CA file replaces the system roots
|
||||
@@ -377,17 +377,27 @@ func withBuffered(conn net.Conn, r *bufio.Reader) net.Conn {
|
||||
func relay(client, upstream net.Conn) {
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
_, _ = io.Copy(upstream, client)
|
||||
_ = copyConn(upstream, client)
|
||||
closeWrite(upstream)
|
||||
close(done)
|
||||
}()
|
||||
_, _ = io.Copy(client, upstream)
|
||||
_ = copyConn(client, upstream)
|
||||
closeWrite(client)
|
||||
<-done
|
||||
_ = client.Close()
|
||||
_ = upstream.Close()
|
||||
}
|
||||
|
||||
// copyConn falls back to a plain copy as some vendor kernels fail splice with EBADF, Go only does so on EINVAL
|
||||
// needed because of https://github.com/go-gitea/gitea/issues/40367
|
||||
func copyConn(dst io.Writer, src io.Reader) error {
|
||||
n, err := io.Copy(dst, src)
|
||||
if n == 0 && errors.Is(err, syscall.EBADF) {
|
||||
_, err = io.Copy(struct{ io.Writer }{dst}, struct{ io.Reader }{src})
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func closeWrite(conn net.Conn) {
|
||||
if cw, ok := conn.(interface{ CloseWrite() error }); ok {
|
||||
_ = cw.CloseWrite()
|
||||
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
"testing"
|
||||
|
||||
"gitea.dev/modules/egress/policy"
|
||||
@@ -186,15 +187,22 @@ func TestWriteUpstreamError(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
type spliceFailConn struct{ net.Conn }
|
||||
|
||||
func (spliceFailConn) WriteTo(io.Writer) (int64, error) { return 0, syscall.EBADF }
|
||||
func (spliceFailConn) ReadFrom(io.Reader) (int64, error) { return 0, syscall.EBADF }
|
||||
|
||||
func TestRelay(t *testing.T) {
|
||||
t.Parallel()
|
||||
client, clientEnd := net.Pipe()
|
||||
upstreamEnd, upstream := net.Pipe()
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
relay(clientEnd, upstreamEnd)
|
||||
relay(spliceFailConn{clientEnd}, upstreamEnd)
|
||||
close(done)
|
||||
}()
|
||||
go func() { _, _ = io.Copy(upstream, upstream) }()
|
||||
assertEcho(t, client, bufio.NewReader(client))
|
||||
require.NoError(t, upstream.Close())
|
||||
_, err := client.Read(make([]byte, 1))
|
||||
assert.ErrorIs(t, err, io.EOF)
|
||||
|
||||
Reference in new issue
Block a user