I'm encountering an odd problem that only shows up in production and only after the process has been running for days, sometimes weeks. The code triggering the error is very straight-forward. It's looking in a report directory to delete old data:
public void DeleteOldReports(){
DateTime now=DateTime.Now;
foreach(String filePath in
new DirectoryInfo(ReportPath)
.GetFiles()
.Where(x=>x.Name.StartsWith("webReport-",StringComparison.CurrentCultureIgnoreCase) && (now-x.LastWriteTime)>WebReportLifetime)
.Select(x=>x.FullName)
.ToList()
){File.Delete(filePath);}
}
The exception is thrown on the new DirectoryInfo(ReportPath).GetFiles()
call, which is a simple file read operation.
The exception is:
System.UnauthorizedAccessException: Access to the path '\\inst7\appsupp\btc_webreports' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileSystemEnumerableIterator
1.CommonInit() at System.IO.FileSystemEnumerableIterator
1..ctor(String path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler`1 resultHandler, Boolean checkHost) at System.IO.DirectoryInfo.InternalGetFiles(String searchPattern, SearchOption searchOption) at cf.core.WebReportManager.DeleteOldReports() in WebReportManager.cs:line 26
This routine works fine for days or often weeks, but once that first exception is thrown, it'll continue throwing the exception on each access attempt. It never works again until the process is restarted.
Now, if it only happened once and then recovered, I wouldn't worry. Perhaps 'inst7' was temporarily offline. Or perhaps the domain controller or some other VM that plays a role in determining access to shares was briefly unavailable. But that's not what's happening here. It's almost as though some particular token or right that allows my process to access that path has expired or has been revoked. Restarting the process fixes the problem immediately.
Once the process enters this bad state, there's no way to regain access as far as I can tell. This, despite the fact that I can navigate to the folder in question with no problem in Windows Explorer, and furthermore, permission tests via 'View Effective Access' indicate no problem.
All VMs involved in the interaction are running Windows 2016 Server on AWS.
How can I troubleshoot this?
Best,
Festus