Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Too Many Open File Error in Golang

Tags:

go

I am simply reading the /proc/diskstats file. My code is:

func ReadFromFile(filepath string)(string){
    defer func() {
        if err1 := recover(); err1 != nil {
            fmt.Println("!!!!!!!!!!!!!!!!Panic Occured and Recovered in readFromFile(), Error Info: ", err1)
        }
     }()

    strData := ""

    data, err := ioutil.ReadFile(filepath)
    if err != nil{
        fmt.Println("File read error: ", err)
        return ""
    }

    strData = string(data)
    return strData
}

The error I am getting is:

File read error: open /proc/diskstats: too many open files

Not only for this file, I am also getting the same error for some other files.

I have also run this command:

root@golang:~# lsof|wc -l

785

Please guide me.

like image 418
GKV Avatar asked Jun 14 '16 06:06

GKV


1 Answers

I ran into the same problem (maybe different circumstances or setup) and fixed it differently:

func some_func(file_name []string) {
    for _, file_name := range file_names {
        f, _ := os.Create(file_name)
        // defer f.Close() // bad idea
        _, _ = f.Write([]byte("some text"))
        f.Close() // better idea
    }
}

The problem is that defer will be executed, when the function will return, which could take a while - depending on the loop size (bad idea). So just do it explicit (better idea )

like image 91
Michael Dorner Avatar answered Oct 02 '22 05:10

Michael Dorner