0

vim saved "./a.log" file when golang writing file. After that the golang program can't write file. There are no error messages and no hang. run for looping is very well except saved file.

Is there any solution?


import (
    "fmt"
    "io"
    "os"
    "time"
)

func main() {
    var f1 *os.File

    f1, _ = os.OpenFile("./a.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)

    iCnt := 0
    format := ""

    for {
        iCnt++
        format = fmt.Sprintf("aaaaaaaaaaa[%d]\r\n", iCnt)

        n, err := io.WriteString(f1, format)
        if err != nil {
            panic(err)
        }
        f1.Sync()
        fmt.Printf("Writed %d bytes\n", n)
        fmt.Printf("%v\n", f1)

        time.Sleep(3 * time.Second)

    }
}

Terminal1) run binary file and see the log file

console screenshot

Brits
  • 14,829
  • 2
  • 18
  • 31
TheK
  • 1
  • 2
  • What makes you believe that it was vim that saved the file? Is it possible that you started your program (either in the debugger or another terminal); this would exhibit the symptoms you describe (there will be a way of checking what has the file open but you did not mention what OS you are using). – Brits Mar 09 '21 at 06:44
  • CentOS 7 or higher – TheK Mar 09 '21 at 06:51
  • `Test scenario. 1. Run golang 2. tail -F ./a.log -> golang append message to ./a.log file 3. Open file using vim 4. save file in vim 5. Check number 2 whether file write or not. – TheK Mar 09 '21 at 06:54
  • 2
    I have attempted to update your question based upon your comments. What you are seeing is not all that surprising; see [this question](https://stackoverflow.com/questions/2028874/what-happens-to-an-open-file-handle-on-linux-if-the-pointed-file-gets-moved-or-d) for some relevant info. The outcome depends upon what Vim is doing when it overwrites the file (I'd guess it's renaming it (i.e. `*.ext~`) then writing a new file). – Brits Mar 09 '21 at 07:19
  • Yup but in this case i used "wq" that is vi command and inode is not deleted or changed because if i see the log os.File then the address is not chaged. – TheK Mar 09 '21 at 07:48
  • What is _the log os.File_? What is _the address_? You'd better post exactly what you see, not describe it ambiguously. – Armali Mar 09 '21 at 08:21
  • f1, _ := os.OpenFile("./a.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) fmt.Printf("%v\n", f1) I don't konw which part is ambiguouse to you. I updated original content. – TheK Mar 09 '21 at 08:32
  • 1
    @TheK please modify your question rather than posting code in the comments; I had a go at doing this to try and give you an idea what is needed but you have reverted the changes. Please include a transcript of the console showing the issue (every command you enter along with the output). Rather than saying "because if i see the log os.File then the address is not chaged" show us how you checked this (because the meaning of that sentence is unclear). – Brits Mar 09 '21 at 08:51
  • 1
    Your screenshot establishes that `f1` has has remained at the same address in memory (which is expected). This is unrelated to what is happening on the filesystem (and the inode is something quite different). – Brits Mar 09 '21 at 10:14

0 Answers0