Search Unity

File access error in Assets directory

Discussion in 'Editor & General Support' started by samanthastahlke, Aug 12, 2019.

  1. samanthastahlke

    samanthastahlke

    Joined:
    Jun 24, 2017
    Posts:
    3
    Hello,

    First post, so please bear with me and let me know if anything is unclear or should be reformatted.

    I'm encountering an error with file access while running my project from the Editor in Unity 2019.2.0f1 on Windows 10.

    The situation is that I have a filestream open at runtime (for a custom logging system), created like so:

    Code (CSharp):
    1. logStream = new FileStream(filename,
    2.     FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
    3.  
    4. logOutput = new StreamWriter(logStream);
    5. logOutput.AutoFlush = true;
    I write to this stream during playmode to collect some telemetry on objects in the scene (it's a text file, nothing overly fancy, and it is disposed in OnApplicationQuit). Everything works as expected.

    Here's where it gets tricky - I want to allow users of my tool to write logs to any folder they like. I have no issues if the folder is located outside of the Assets folder.

    However, if the logs are being written to a location inside Assets (e.g., a subdirectory), then I have a problem if I click away from the Editor while the project is running. When I click back to the Editor, I am given an error dialog claiming that the file in question is being used by another process (if I'm writing multiple logs, the dialog will pop up for each one).



    If I hit cancel, playmode resumes, with an error logged to the console that simply says "File couldn't be read" (no stacktrace or further information is given), which persists after playmode stops. If I click away from and back to the Editor again (with playmode stopped), this message then disappears from the console (any other messages in the console remain).

    Perhaps most oddly of all, if I open the file after all of this is said and done, the log is written exactly as expected - seemingly no interruptions, and resumed writing just fine after hitting cancel on the dialog (playmode pauses while the dialog is active).

    Just to re-iterate, this only seems to happen if the stream is located somewhere in the Assets folder, and only if I click away from and back to the Editor while the project is running.

    I am aware that it's easy to "work around" this by simply saving the logs somewhere else, or leaving the Editor window in focus while it's running, but I'd prefer to not have to impose this restriction on the eventual users of my tool. Frankly, at this point I'm also just curious as to what's happening.

    Here's some more details:
    • Both Windows Resource Monitor and Process Explorer claim Unity is the only process accessing the file in question when the error is thrown. I tried refreshing Resource Monitor/Process Explorer when the dialog was displayed, and they still show Unity as the only process accessing the file.
    • It seems the dialog is thrown before I can try to handle anything in OnApplicationFocus - so if an exception is causing this, I'm not sure how I could catch it and just ignore it (since the file seems to be written just fine).
    • I tried whitelisting the project folder in Windows Defender, but this didn't make a difference.
    • I have Run in Background enabled in the Player Settings, I tried disabling it just to see if that would make a difference, but it does not.
    • Just to see if anything would change, I tried setting FileShare to None and turning off AutoFlush, but that didn't seem to make a difference either.

    Any help, ideas, or insight is appreciated. Thanks very much!
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    This is behavior I would expect. Don't write log files into the Assets folder. By default when you click away from the editor and then again focus the editor it scans the Assets folder for any changes. If it finds anything new it will attempt to import the new asset or recompile your scripts. Obviously you don't want log files imported as assets into your project, so don't put them in the Assets folder.

    Though you could try giving your log files a .tmp file extension, since they are automatically excluded from import.

    https://docs.unity3d.com/Manual/SpecialFolders.html
     
  3. samanthastahlke

    samanthastahlke

    Joined:
    Jun 24, 2017
    Posts:
    3
    Thanks for the reply, Joe! That makes total sense, though I didn't realize the Editor would try to auto-import absolutely anything, and I'm still not certain why it would throw an error if the file was created with sharing enabled.

    I'll instruct users not to write logs to the Assets folder, but I wonder if there's a setting somewhere in Unity that I'm unaware of that would disable this for certain custom filetypes, or specific subfolders - just out of curiosity.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You're too fast! :p Edited above with the documentation on what is automatically excluded (scroll to the bottom of the page).
     
  5. samanthastahlke

    samanthastahlke

    Joined:
    Jun 24, 2017
    Posts:
    3
    Ah, excellent, thanks! :)