Skip to content

Conversation

@graebm
Copy link
Contributor

@graebm graebm commented May 28, 2022

ISSUE: On Windows, the .dll file extracted to the temp directory was never being deleted. And repeated runs of the program were filling the disk with old .dll files.

DIAGNOSIS: We were using File.deleteOnExit() to clean up the shared lib, which is working OK on other platforms, but not on Windows. On Windows, files cannot be deleted while they're in use, and it appears that Java doesn't try to "unload" the .dll, so it's apparently impossible for a Java process to delete a .dll that it's using.

I did an experiment with a dead-simple JNI library. I simply loaded it, and called File.deleteOnExit(). That .dll didn't get deleted either. So it's not just a problem with our .dll being very complex.

DESCRIPTION OF CHANGES: On Windows only, during startup, scan the temp dir for old instances of AWSCRT_*aws-crt-jni.dll and try to delete them.

SIDE-NOTE:
I realized that on non-Windows platforms, the shared-lib won't be deleted on exit if Java dies suddenly (crash in C, OS terminated process, etc). But on non-Windows platforms you can delete the shared-lib from disk immediately after loading it, you don't need to wait until the process exits. I did experiments on Mac and this works. But I left it out of this PR because this is an emergency fix for windows and I don't want to introduce non-necessary changes.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

// for old instances of the .dll and try to delete them. If another
// process is still using the .dll, the delete will fail, which is fine.
String os = getOSIdentifier();
if (os == "windows") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel like platform id shouldn't be a string, but that's a larger change.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.equals()?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another great reason to make it not-a-string!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I started making it not-a-string but it was turning into a big refactor so going with .equals() as suggested by Dombo

@graebm graebm merged commit a67b148 into main May 31, 2022
@graebm graebm deleted the file-cleanup branch May 31, 2022 19:30
graebm added a commit to aws/aws-iot-device-sdk-java-v2 that referenced this pull request May 31, 2022
Update to latest aws-crt. Contains [fix](awslabs/aws-crt-java#493) that, on startup, deletes any .dll files it finds from previous runs of the library.
graebm added a commit to aws/aws-iot-device-sdk-java-v2 that referenced this pull request May 31, 2022
Update to latest aws-crt. Contains [fix](awslabs/aws-crt-java#493) that, on startup, deletes any .dll files it finds from previous runs of the library.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants