AIX Open Source

 View Only
  • 1.  golang Sync() error on AIX

    IBM Champion
    Posted Fri September 11, 2020 09:59 AM
    AIX 7.2 TL3, Go version 1.14.2:

    # oslevel -s
    7200-03-02-1846

    # rpm -qa | grep golang
    golang-1.14.2-1.ppc
    golang-src-1.14.2-1.noarch
    golang-bin-1.14.2-1.ppc

    Very simple example with os.OpenFile() and Sync():

    package main

    import (
      "fmt"
      "os"
    )

    func main() {
      f, err := os.OpenFile("/tmp/1", os.O_RDONLY, 0644)
      if err != nil {
        fmt.Printf("os.OpenFile: %s\n", err)
        return
      }
      defer f.Close()

      err = f.Sync()
      if err != nil {
        fmt.Printf("os.Sync(): %s\n", err)
        pe, ok := err.(*os.PathError)
        if ok {
          fmt.Printf("Op: %s, Path: %s, Err: %v\n", pe.Op, pe.Path, pe.Err)
        } else {
          fmt.Printf("not ok to PathError\n")
        }
      }
    }

    No Problems on Linux and Mac OS X, but on AIX:

    os.Sync(): sync /tmp/1: bad file number
    Op: sync, Path: /tmp/1, Err: bad file number

    I think, sync() is quite usual function and should work ;-)

    ------------------------------
    Andrey Klyachkin
    ------------------------------


  • 2.  RE: golang Sync() error on AIX

    Posted Mon September 14, 2020 03:59 AM

    Hi Andrey, 

    The problem is that AIX fsync syscall fails when a file is open in Read-Only mode. 
    From "man fsync":
     Note: The file identified by the FileDescriptor parameter must be open for writing when the fsync subroutine
    is issued or the call is unsuccessful. This restriction was not enforced in BSD systems. The fsync_range
    subroutine does not require write access.

    Maybe fsync_range can be called instead of fsync. I'll check that.
    Meanwhile, could you open an issue in Golang github (https://github.com/golang/go) ? That way, the community and not only us can answer. 

    Thanks
    Clément



    ------------------------------
    Clement CHIGOT
    ------------------------------



  • 3.  RE: golang Sync() error on AIX

    IBM Champion
    Posted Mon September 14, 2020 04:24 AM
    Thank you Clément for the pointing out to the man! If I change to O_RDWR, it works. I opened the issue on github - https://github.com/golang/go/issues/41372

    ------------------------------
    Andrey Klyachkin
    ------------------------------



  • 4.  RE: golang Sync() error on AIX

    Posted Mon September 14, 2020 03:59 AM
    You are doing Sync() on a read only file descriptor which is not allowed.
    May be in Linux, it silently ignores it without throwing any error.

    ------------------------------
    Ayappan P
    ------------------------------



  • 5.  RE: golang Sync() error on AIX

    IBM Champion
    Posted Mon September 14, 2020 04:27 AM
    we usually use high-level languages instead of C, because the function the same or similar way on all platforms. If it is silently ignored on Linux, it should be  ignored on AIX too. AFAIU the golang runtime (syscalls package?) should be changed to make it possible.

    ------------------------------
    Andrey Klyachkin
    ------------------------------



  • 6.  RE: golang Sync() error on AIX

    Posted Mon September 14, 2020 06:35 AM
    Edited by Clement CHIGOT Mon September 14, 2020 06:35 AM
    Linux doesn't have this restriction. Thus, there is no "silent error", it works.
    However, using fsync_range(fd, O_SYNC, 0, 0) seems to have the same behavior and will work for every opened files included read-only ones. I'll send the patch to the community (once I'm sure it isn't breaking anything), and I suppose it can be backported easily to older versions.

    Thanks for the issue. 

    ------------------------------
    Clement CHIGOT
    ------------------------------



  • 7.  RE: golang Sync() error on AIX

    Posted Tue September 15, 2020 04:47 AM
    The patch have been accepted by the community https://go-review.googlesource.com/c/go/+/254657/
    @Andrey Klyachkin you can either download and compile the master, or wait a little for AIXToolbox or BullFreeware to release the new version with the fix.

    Clément​

    ------------------------------
    Clement CHIGOT
    ------------------------------



  • 8.  RE: golang Sync() error on AIX

    IBM Champion
    Posted Wed September 16, 2020 11:39 AM
    Thank you @Clement CHIGOT! ​

    ------------------------------
    Andrey Klyachkin
    ------------------------------