JDK-8068370 : (process) Executing recently modified file results in ETXTBSY
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 7u71,8,9
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: linux
  • CPU: x86,x86_64
  • Submitted: 2014-12-03
  • Updated: 2020-04-10
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbd_minorUnresolved
Related Reports
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)


ADDITIONAL OS VERSION INFORMATION :
Linux tuohi 3.16.5-gentoo #1 SMP Thu Oct 30 08:55:22 EET 2014 x86_64 Intel(R) Core(TM) i5-2400 CPU @ 3.10GHz GenuineIntel GNU/Linux

EXTRA RELEVANT SYSTEM CONFIGURATION :
Filesystem is ext3

A DESCRIPTION OF THE PROBLEM :
See the attachment. There is a shell script that compiles and runs a java program that reproduces the problem.

In a nutshell: many threads run a loop that deletes and recreates a thread-specific temporary directory, writes a shell script under the directory and tries to run it. Sometimes the script run fails and throws java.io.IOException: error=26, Text file busy.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
See attachment

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
No exception thrown, all script runs work.
ACTUAL -
A lot of stack traces, then

done, errors: 35
failures: 35
successes: 4965
failure ratio 0.7%
missing out files: 35


ERROR MESSAGES/STACK TRACES THAT OCCUR :
java.io.IOException: error=26, Text file busy.

REPRODUCIBILITY :
This bug can be reproduced occasionally.

---------- BEGIN SOURCE ----------
Ah, you don't support sending attachments? So, here's a base64 encoded tar.gz that contains mentioned script and java file:

H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omCJrrywTgPNNOV90g7YP
GDatFuMHUCbsFUMfeOZ+71/PP/5Y8f0H+lfAh4+0h/rIs/r7D6w1F///02rvrr//PMUo5bPrmvR1
3d9P358ePvehzal09Z7XvYolyvt+QugIVfuwIImw5r56fdztnnYPFaGmrmGYu+q+gaoTYDQMnute
vHln2KCiuWyMqkxqArJXFlBz91XnzZvO2+55x2rOi3jwOWTuR7BXKrdcHvqEvnyB8obQdCBP05lf
3RwKMdmv1QD2aSMYh5eYBvI7p3prlrSXX5tBqzStwlTuvQn1x5VfnCJUjSbz21Tj0onRc2gUNDbO
kp9WgUH5dfNn9cF0s6IVWT1abr+s8B6f54LFb1PGhd8bYeuxHuuxHuvxXxz/Ahs5mbgAKAAA

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
I haven't tried, but maybe retry can help. But that would require parsing the exception message, and there is a risk that we suppress other problems by accident.


Comments
Specifying O_CLOEXEC doesn't completely resolve the problem, but makes it happen less frequent.
10-06-2016

My suspicion is that the reason is due to the following race: When one thread starts a child process, it forks the parent process and copies the table of opened file descriptors. If another thread at that moment finishes writing, closes file and tries to execute it, it would fail because the first forked process still has the file opened. Update: the same behavior observed even with -Djdk.lang.Process.launchMechanism=VFORK When vfork() is used, the parent should get suspended until the child calls exec_(). exec() functions inherit all the opened files unless they have FD_CLOEXEC flag set on the descriptor. Update2: a comment in ProcessImpl_md.c says: "Files are created FD_CLOEXEC in Java.", but in fact they are not. Adding O_CLOEXEC to the open flags resolves the issue. Now, need to understand what would be a drawback of adding this flag to every open() call.
14-04-2016

Managed to reproduce it with nio -------------------------- import java.nio.*; import java.util.*; import java.nio.file.*; import java.nio.file.attribute.*; public class U { public static void main(String[] args) throws Throwable { for (int j = 0; j != 3; ++j) { Path subdir = Files.createDirectory(Paths.get("subdir" + j)); new Thread(() -> { for (int i = 0; i != 1000000; ++i) { try { Path sh = subdir.resolve(Paths.get("script" + i)); Files.write(sh, "#!/bin/bash\n".getBytes()); Files.setPosixFilePermissions(sh, Collections.singleton(PosixFilePermission.OWNER_EXECUTE)); new ProcessBuilder(sh.toString()).start().waitFor(); } catch (Exception e) { throw new RuntimeException(e); } }} ).start(); } } }
13-04-2016

I confirm the failure is observed with 7u, 8u and 9. The testcase can be reduced to the following short block being run concurrently in many threads: File sh = new File(workerDir, "script"); FileOutputStream wr = new FileOutputStream(sh); wr.write("#!/bin/bash\n".getBytes()); wr.close(); sh.setExecutable(true); new ProcessBuilder(sh.getAbsolutePath()).start().waitFor(); Here workerDir is unique for each run, so no threads should write to the same file. On my machine it results in one 'Text file busy' failures on every ~250 cycles. I suspect it may have something to do with the descriptor recycling. If fd.sync() is added right before close, the number of failures is greatly increased -- up to almost 1 failure on every 2 cycles.
19-01-2015

Comment from the original submitter: "Please run the program, even if you doubt it. No external process is locking any files, it's just the reproducer program itself. It's not my place to speculate, but if I have to, I think it's possible that a call to File.delete or File.setExecutable or Process.waitFor is not synchronous i.e. it keeps the file locked after returning. Or then it is something else, but definitely the bug is in the standard library, if not in the JVM."
19-01-2015

I have not looked at the reproducer but I doubt this is a JDK bug. The error "Text file busy" just means that someone has the script/program open for writing.
30-12-2014

Attached the test case file send by the submitter. Couldn't reproduce the issue as mentioned. However, moving thi sup for further review.
30-12-2014

Requested the submitter to send the script example as an attachment or any other useful information to be able to reproduce this issue.
04-12-2014