Skip to content

Instantly share code, notes, and snippets.

@cyfdecyf
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cyfdecyf/f9c14c48b802b3bf5dfd to your computer and use it in GitHub Desktop.
Save cyfdecyf/f9c14c48b802b3bf5dfd to your computer and use it in GitHub Desktop.
Too many open file error in net.Listener.Accept loop
package main
/*
Observe different behavior of net.Listener.Accept on Linux and OS X.
Steps:
1. Build this program on Linux or OS X.
2. Execute "ulimit -Sn 5" to limit the number of open file. (You may need to
change the number to 6 or other values.)
3. Run the program.
4. Execute "nc 127.0.0.1 2345" several times.
On OS X, each connection will generate one failure log.
On Linux, each connection will generate hundreds or thousands of logs.
*/
import (
"fmt"
"net"
"os"
)
func main() {
ln, err := net.Listen("tcp", "127.0.0.1:2345")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
errcnt := 0
for {
_, err := ln.Accept()
if err != nil {
errcnt++
fmt.Println(err, errcnt)
continue
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment