Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite loops in javalib Files.createTempFile and createTempDirectory* methods #3738

Closed
LeeTibbert opened this issue Feb 8, 2024 · 0 comments · Fixed by #3739
Closed

Infinite loops in javalib Files.createTempFile and createTempDirectory* methods #3738

LeeTibbert opened this issue Feb 8, 2024 · 0 comments · Fixed by #3739

Comments

@LeeTibbert
Copy link
Contributor

LeeTibbert commented Feb 8, 2024

Presenting problem

The javalib Files.createTempFile method loops infinitely when given a Path argument which
does not exist. Presumably its createTempDirectory kin all have a similar loop.

Later:

  • Yes, at least one createTempDirectory method exhibits the loop.

  • It appears that the reported problem can occur (only?) with the methods which
    take a Path as the first argument. The others usually default to the system temporary
    directory, which should exist. There may be a way for a user to set the a Property to
    change the temporary directory for that user to someplace bogus. In that case
    the same defect would become evident.

    The fix is the same in all cases.

Analysis

FileHelpers.scala allows doing something dumb, then the Files methods goes ahead
and does it.

From FileHelpers.scala:

  while ({
        result = genTempFile(prefix, newSuffix, tmpDir)
        !createNewFile(result.toString, throwOnError)
      }) ()

Consider the case where the throwOnError argument is passed in as "false" and
createNewFile continually returns false, say because the dir argument pased in does
not exist or can not be created, say because it is non-writable.
Those are conditions which are unlikely to be be changed by re-try. Yes, it could
happen, but not the expected case.

The Files createTemp* methods call FileHelper methods which default throwOnError to
"false", so they loop until the end of time when they pass down an argument which causes
errors in the underlying code.

The Files methods under consideration are documented as being capable of returning
IoException and its sub-classes. Allowing the underlying code to throw an Exception
would give the end user some clue as to the nature of the problem.

Someday, the FileHelpers code should be fixed. One can add a counter, but what should
it do when it has been explicitly told to not throw on error? I think the most robust thing
to do is to throw an IoException stating that the counter limit has been exceeded.

Either that require that the code execute on more cores so that the loop completes
faster.

LeeTibbert added a commit to LeeTibbert/scala-native-fork that referenced this issue Feb 9, 2024
@LeeTibbert LeeTibbert changed the title Infinite loops in javalib Files.createTempFile and createDirectoryFile* methods Infinite loops in javalib Files.createTempFile and createTempDirectory* methods Feb 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant