TheElectronWill / night-config

Powerful java configuration library for toml, yaml, hocon, json and in-memory configurations. Serialization/deserialization framework.
GNU Lesser General Public License v3.0
242 stars 28 forks source link

AccessDeniedException when using WritingMode.REPLACE_ATOMIC #183

Open shartte opened 3 months ago

shartte commented 3 months ago

On Windows, we sometimes run into AccessDeniedExceptions when saving a config rapidly likely due to WindowsDefender locking the config file, preventing it from being replaced.

new TomlWriter().write(config, file, WritingMode.REPLACE_ATOMIC);
com.electronwill.nightconfig.core.io.WritingException: Failed to write (REPLACE_ATOMIC) the config to: C:\AE2\Forge\run\config\ae2-client.toml
    at MC-BOOTSTRAP/com.electronwill.nightconfig.core@3.8.0/com.electronwill.nightconfig.core.io.ConfigWriter.write(ConfigWriter.java:105) ~[core-3.8.0.jar%23136!/:?] {}
    at MC-BOOTSTRAP/com.electronwill.nightconfig.core@3.8.0/com.electronwill.nightconfig.core.io.ConfigWriter.write(ConfigWriter.java:76) ~[core-3.8.0.jar%23136!/:?] {}
[...]
Caused by: java.nio.file.AccessDeniedException: C:\AE2\Forge\run\config\ae2-client.toml.new.tmp -> C:\AE2\Forge\run\config\ae2-client.toml
    at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:89) ~[?:?] {}
    at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103) ~[?:?] {}
    at java.base/sun.nio.fs.WindowsFileCopy.move(WindowsFileCopy.java:328) ~[?:?] {}
    at java.base/sun.nio.fs.WindowsFileSystemProvider.move(WindowsFileSystemProvider.java:291) ~[?:?] {}
    at java.base/java.nio.file.Files.move(Files.java:1430) ~[?:?] {}
    at MC-BOOTSTRAP/com.electronwill.nightconfig.core@3.8.0/com.electronwill.nightconfig.core.io.ConfigWriter.write(ConfigWriter.java:92) ~[core-3.8.0.jar%23136!/:?] {}
    ... 44 more

In our other tooling we've opted to implement a short retry-loop specifically on Windows and on AccessDeniedExceptions to work around this.

TheElectronWill commented 3 months ago

Would it be possible add the config files to the Windows Defender whitelist? A TOML file is just text after all, it's not an executable that should be scanned (the ".tmp" extension may have triggered Defender somehow?)

shartte commented 3 months ago

Would it be possible add the config files to the Windows Defender whitelist? A TOML file is just text after all, it's not an executable that should be scanned (the ".tmp" extension may have triggered Defender somehow?)

This is not something I can control for players 😓

What I am doing to work around it is to just try to avoid writing the config file in rapid succession, but you know how it goes. If this can crash, it will for some poor chap.

TheElectronWill commented 3 months ago

Okay, since multiple people seem to be affected, I'll try to add a workaround. I'll need someone to test it though, as I do not use Windows :)

Donastria commented 3 months ago

I would be happy to test if you need. I have WAY too many mods that require ForgeConfig that aren't working atm...

TheElectronWill commented 3 months ago

I've opened #184 with a potential fix!