I have a web application running in Wildfly, and part of the application uses a temporary file via:
File.createTempFile(...)
.
That temporary file is then used as the destination file for a read-write java.io.RandomAccessFile stream. The java.nio.channels.FileChannel for that RandomAccessFile is what is interacted with and eventually truncated (when I'm done with it). Portions of the file do get read-write mapped via FileChannel.map()
After all interaction with the file is done, the file is truncated to the proper size using fileChannel.truncate(size)
. This is where an IOException is always thrown
at sun.nio.ch.FileDispatcherImpl.truncate0(Native Method)
The IOException message is generic:
java.io.IOException: The requested operation cannot be performed on a file with a user-mapped section open
.
This is a temp file created explicitly by the same process and it has remained open for the duration of work with it, a matter of a few seconds probably. Googling around suggests maybe AV software has a handle on the file, or some other application is using it, but I've tried running this on various VM's and regular windows boxes with different configurations and the problem are reliably reproducible. I think there must be some real abuse of the File facilities occurring, but I'm at a loss.
Also, this exception is never seen on a Linux machine, the truncation works reliably. There is no file locking being performed, this is the only file in use, things are being buffered and written to the channel, but nothing very strange is going on.
For the record, I have created some sample test apps creating streams, channels, temp files, performing truncations, etc and I am unable to reproduce the problem in a standalone app. Unfortunately, I can't share the source directly, so I am doing my best to describe what's occurring in the hopes that someone has run into something similar and can offer some guidance.