1
0
mirror of https://github.com/golang/go synced 2024-11-12 07:40:23 -07:00

net: never use backlog > 65535

The system call takes an int, but the kernel stores it in a uint16.
At least one Linux system sets /proc/sys/net/core/somaxconn
to 262144, which ends up being 0 in the uint16. Avoid being tricked.

FreeBSD sources also store the backlog in a uint16.
Assume the problem is systemic and fix it everywhere.

Fixes #5030.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/7480046
This commit is contained in:
Russ Cox 2013-03-12 01:48:48 -04:00
parent 019754ed40
commit e64f3f211a
3 changed files with 13 additions and 0 deletions

View File

@ -27,5 +27,11 @@ func maxListenerBacklog() int {
if n == 0 || err != nil {
return syscall.SOMAXCONN
}
// FreeBSD stores the backlog in a uint16, as does Linux.
// Assume the other BSDs do too. Truncate number to avoid wrapping.
// See issue 5030.
if n > 1<<16-1 {
n = 1<<16 - 1
}
return int(n)
}

View File

@ -21,5 +21,11 @@ func maxListenerBacklog() int {
if n == 0 || !ok {
return syscall.SOMAXCONN
}
// Linux stores the backlog in a uint16.
// Truncate number to avoid wrapping.
// See issue 5030.
if n > 1<<16-1 {
n = 1<<16 - 1
}
return n
}

View File

@ -8,6 +8,7 @@ import "syscall"
func maxListenerBacklog() int {
// TODO: Implement this
// NOTE: Never return a number bigger than 1<<16 - 1. See issue 5030.
return syscall.SOMAXCONN
}