You basically already showed how to do it on Linux: inside your Go prog, you know the file descriptor number (f.Fd()). Then do an os.Readlink(fmt.Sprintf("/proc/self/fd/%d", f.Fd()) and check the result (if no error) with strings.HasSuffix(link, "(deleted)").
On Sunday, December 10, 2023 at 5:41:00 PM UTC+1 Jason E. Aten wrote: > I'm debugging a weird situation where my open binary log file > has been deleted and replaced by an > identical copy by my doing a git commit (and maybe git rebase) > on it. The Go process is still running, still has the origional > file handle open, and is still "writing" to the > deleted log file to no effect (no further appends are happening). > > The /proc listing shows the Go process's file handle to the file as > (deleted). See below. > > My question is: is there a way to have the Go process detect if the file > it is writing to has been deleted by another process (git in this case) so > that attempting to append to the file is no longer effective? This example > is on Linux on xfs filesystem; Ubuntu 18.04. I suppose I could Sync then > Stat the file before and after and if it is not growing by the appropriate > number of bytes, take corrective action... but I wonder if there is a more > elegant way? > > go version go1.21.2 linux/amd64 > > Fortunately I noticed this before too much had gone missing to the > binary log, and was able to confirm that it was going missing by comparing > to a textual version of the log that I also keep. > > I would be happy to check the file descriptor on each append operation, to > ensure that the logging will be effective. But writes are succeeding to the > deleted file: I am checking the error returned by the os.File.Write() > operation, and it is always nil indicating no error on the Write. > > ~~~ > > myuser*@*host* /proc/94913/fd $* *ls -al* > > total 0 > > dr-x------ 2 myuser myuser 0 Dec 10 10:02 *.* > > dr-xr-xr-x 9 myuser myuser 0 Dec 10 10:02 *..* > > lrwx------ 1 myuser myuser 64 Dec 10 10:02 *0* -> /dev/pts/8 > > lrwx------ 1 myuser myuser 64 Dec 10 10:02 *1* -> /dev/pts/8 > > lrwx------ 1 myuser myuser 64 Dec 10 10:02 *10* -> > 'anon_inode:[eventpoll]' > > ... > > lrwx------ 1 myuser myuser 64 Dec 10 10:02 *8* -> > '/mnt/b/data/logs/my.binarylog.host (deleted)' ## << how to detect this? > > ... > > myuser*@*host* /proc/94913/fd $* *mount|grep mnt/b* > > /dev/sdb on /*mnt/b* type xfs > (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota) > > myuser*@*host* /proc/94913/fd $* *uname -a* > > Linux host 5.4.0-126-generic #142~18.04.1-Ubuntu SMP Thu Sep 1 16:25:16 > UTC 2022 x86_64 x86_64 x86_64 GNU/Linux > > myuser*@*host* /proc/94913/fd $* *cat /etc/lsb-release * > > DISTRIB_ID=Ubuntu > > DISTRIB_RELEASE=18.04 > > DISTRIB_CODENAME=bionic > > DISTRIB_DESCRIPTION="Ubuntu 18.04.5 LTS" > > myuser*@*host* /proc/94913/fd $* > > ~~~ > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/ff3512f0-8282-4249-91be-b45cacaf73f0n%40googlegroups.com.