apernet / hysteria

Hysteria is a powerful, lightning fast and censorship resistant proxy.
https://v2.hysteria.network/
MIT License
13.86k stars 1.56k forks source link

sconn.remoteAddr的竞态条件问题 #727

Closed happyharryh closed 9 months ago

happyharryh commented 9 months ago

描述问题 sconn.remoteAddr存在竞态条件问题,疑似与Hysteria connection migration有关

如何复现go build时加入-race参数用于检测竞态条件,以最简单的参数启动服务器端和客户端时,服务器端会出现竞态信息

==================
WARNING: DATA RACE
Read at 0x00c00018b2e0 by goroutine 27:
  github.com/apernet/quic-go.(*sconn).RemoteAddr()
      github.com/apernet/quic-go@v0.39.1-0.20230930045547-13cecb45baa8/send_conn.go:91 +0x30
  github.com/apernet/quic-go.(*connection).RemoteAddr()
      github.com/apernet/quic-go@v0.39.1-0.20230930045547-13cecb45baa8/connection.go:2387 +0x44
  github.com/apernet/quic-go/http3.(*Server).handleRequest()
      github.com/apernet/quic-go@v0.39.1-0.20230930045547-13cecb45baa8/http3/server.go:578 +0x398
  github.com/apernet/quic-go/http3.(*Server).handleConn.func1()
      github.com/apernet/quic-go@v0.39.1-0.20230930045547-13cecb45baa8/http3/server.go:454 +0xd8

Previous write at 0x00c00018b2e0 by goroutine 21:
  github.com/apernet/quic-go.(*sconn).SetRemoteAddr()
      github.com/apernet/quic-go@v0.39.1-0.20230930045547-13cecb45baa8/send_conn.go:95 +0x3c
  github.com/apernet/quic-go.(*connection).handlePacketImpl()
      github.com/apernet/quic-go@v0.39.1-0.20230930045547-13cecb45baa8/connection.go:874 +0xe9c
  github.com/apernet/quic-go.(*connection).run()
      github.com/apernet/quic-go@v0.39.1-0.20230930045547-13cecb45baa8/connection.go:579 +0x1240
  github.com/apernet/quic-go.(*baseServer).handleInitialImpl.func5()
      github.com/apernet/quic-go@v0.39.1-0.20230930045547-13cecb45baa8/server.go:674 +0x44

Goroutine 27 (running) created at:
  github.com/apernet/quic-go/http3.(*Server).handleConn()
      github.com/apernet/quic-go@v0.39.1-0.20230930045547-13cecb45baa8/http3/server.go:453 +0x2a4
  github.com/apernet/quic-go/http3.(*Server).ServeQUICConn()
      github.com/apernet/quic-go@v0.39.1-0.20230930045547-13cecb45baa8/http3/server.go:253 +0xec
  github.com/apernet/hysteria/core/server.(*serverImpl).handleClient()
      github.com/apernet/hysteria/core/server/server.go:84 +0x218
  github.com/apernet/hysteria/core/server.(*serverImpl).Serve.func1()
      github.com/apernet/hysteria/core/server/server.go:67 +0x4c

Goroutine 21 (running) created at:
  github.com/apernet/quic-go.(*baseServer).handleInitialImpl()
      github.com/apernet/quic-go@v0.39.1-0.20230930045547-13cecb45baa8/server.go:674 +0xb1c
  github.com/apernet/quic-go.(*baseServer).handlePacketImpl()
      github.com/apernet/quic-go@v0.39.1-0.20230930045547-13cecb45baa8/server.go:443 +0xed0
  github.com/apernet/quic-go.(*baseServer).run()
      github.com/apernet/quic-go@v0.39.1-0.20230930045547-13cecb45baa8/server.go:277 +0x1b4
  github.com/apernet/quic-go.newServer.func1()
      github.com/apernet/quic-go@v0.39.1-0.20230930045547-13cecb45baa8/server.go:259 +0x34
==================

预期行为 不再出现竞态信息

日志 服务器端配置

listen: 127.0.0.1:8081
bandwidth:
  up: 1 gbps
  down: 1 gbps
tls:
  cert: ca.crt
  key: ca.key
auth:
  type: password
  password: test

服务器端日志

2023-10-04T09:14:08+08:00   INFO    server mode
2023-10-04T09:14:08+08:00   INFO    server up and running
2023-10-04T09:14:09+08:00   INFO    client connected    {"addr": "127.0.0.1:62391", "id": "user", "tx": 13107200}

客户端配置

server: 127.0.0.1:8081
auth: test
bandwidth:
  up: 20 mbps
  down: 100 mbps
http:
  listen: 127.0.0.1:8080
tls:
  insecure: true

客户端日志

2023-10-04T09:14:09+08:00   INFO    client mode
2023-10-04T09:14:09+08:00   INFO    connected to server {"count": 1}
2023-10-04T09:14:09+08:00   INFO    use this URI to share your server   {"uri": "hysteria2://test@127.0.0.1:8081/?insecure=1"}
2023-10-04T09:14:09+08:00   INFO    HTTP proxy server listening {"addr": "127.0.0.1:8080"}

设备和操作系统 MacOS Sonoma 14.0

额外信息Hysteria connection migration相关代码注释掉后,竞态条件就不再出现了

tobyxdd commented 9 months ago

感谢反馈, bd03e59a7769b38afbe70adb2efea1c1a8c9dcd1 已修复