How we spent two weeks hunting an NFS bug in the Linux kernel
Really nice.  I have more respect for GitLab now.  That&#x27;s a great write-up, and it led me to read some of their other nice reports too.<p>It&#x27;s not exactly new for NFS to have cache coherency &quot;surprises&quot;.  But it should have &quot;close-to-open&quot; coherency at least, and the bug found by GitLab fails even that.<p>Here&#x27;s an anecdote.<p>A Mac client talking to Samba on Linux.  The client deletes random files that the client isn&#x27;t even looking at, but which happen to be changed on the server around the time the client looks at the directory containing those files.<p>I am not joking.  Randomly deleting files it&#x27;s not even reading.<p>It delayed a product rollout for about 8 months.  I was sure there must be a flaw in some file-updating code, somewhere in application code running on Linux.  What else would make update-by-rename-over files disappear once every few weeks?  Surely the usual tmpfile-fsync-rename dance was durable on Linux, on ext4?  It must have been a silly, embarrasing error in the application code right?  Calling unlink() with the wrong string or something.<p>But no, application was fine.  Libraries were fine.  And the awful bugs in VMware Fusion&#x27;s file sharing were not to blame this time.  (Ahem, another anecdote...)<p>It only happened every few weeks.  A random file would disappear and be noticed.  A web application would be told to update a file, and it&#x27;d spontaneously complain that the file was gone.  It wasn&#x27;t reproducible until we went all-out on trying to make it happen more often.  But they kept disappearing.<p>Things like invoices data files and edited documents.  Once every few weeks for no obvious reason.  Not happy.  And not safe to deploy.<p>Eventually, we found a very old bug in Emacs which deletes the file that&#x27;s being saved in rare circumstances that only manifest when file attributes change at the wrong moment, which does happen with the weird and wonderful Mac SMB client&#x27;s way of caching attributes.  We thought we&#x27;d found the cause with great relief, and could proceed to rollout.  Until after a few weeks, another file disappeared.  No!<p>It took weeks of tracing, reproducing, and learning new debugging tools (like auditd running permanently) to rule out faults in (1) the application code and libraries, (2) Linux itself, (3) Samba, (4) tools used on the Mac when viewing a directory, and viewing and editing files.<p>Nope, it wasn&#x27;t a bug in application code after all.  There weren&#x27;t any faulty calls or wrong strings.  Logging would have caught them.  Linux rename() was fine, not to blame.  It wasn&#x27;t a durability problem on power loss (the reason you need fsync with rename).  Nor VMware disk image snapshots, even though other bugs were spotted with those.  Nor was it the Emacs bug although that was a surprise to find.<p>The reproducer turned out to be &quot;run cat a lot on the Mac, on a file which isn&#x27;t being changed at all, while repeatedly updating another file on Linux in the same directory, using rename to update.  Watch the updated file disappear eventually&quot;.<p>auditd showed Samba was doing the deletes, so I suspected a crazy bug in Samba and had to work quite hard to convince myself Samba was only doing what it was told by the client.  I hoped it was Samba, because that&#x27;s open source and I can fix that.<p>No, it was an astonishingly crappy bug called &quot;delete random files once in a blue moon, hahaha!&quot; in the Mac SMB client, which happened to occasionally be used to look in the same directory, which happened to be shared over Samba for convenience to look at it.<p>The confirmation of cause was from watching the SMB protocol, looking at Samba logs set to maximum verbosity, and lots of reading.<p>atq2119 says: &quot;Imagine this same scenario if GitLab was using a closed source operating system. Would they have been able to track this down?&quot;<p>I think I&#x27;ve had an experience like that - the above bug in the Mac SMB client.  (Seriously, deleting random files.)<p>Googling reveals similar-sounding bugs at least two versions of OSX later.  Yuck.  I have no idea how to meaningfully get these things fixed or usefully reported.  And I&#x27;ve had enough to stop caring anyway.  The workaround is &quot;force it to use SMB v1&quot; (ye olde anciente).  I can imagine the cause is something trivial in directory caching; it&#x27;s probably just a few lines to fix.<p>I&#x27;m certain if the Linux client had a bug like that, it would be fixed very quickly, and probably backported by the big distros.  I&#x27;m certain a Linux SMBFS developer would have been very helpful.  And, there&#x27;s a fairly good chance I could have fixed it myself and submitted the patch - probably less work than finding the cause, in this instance.<p>As it is, I don&#x27;t think I could have found the culprit if I couldn&#x27;t look at the Samba source to understand in detail what was going on in the SMB network protocol, or if I didn&#x27;t have excellent tracing tools in Linux to find which process was responsible for stray deletions (i.e. not my application code, but Samba, which was doing as requested).