Skip to content

Fix race condition by memoizing Tempfile objects - #461

Merged
amitkarsale merged 1 commit into
puppetlabs:mainfrom
alexjfisher:issue425
Feb 5, 2025
Merged

Fix race condition by memoizing Tempfile objects#461
amitkarsale merged 1 commit into
puppetlabs:mainfrom
alexjfisher:issue425

Conversation

@alexjfisher

Copy link
Copy Markdown
Contributor

Previously, the certificate and private_key methods created Tempfile objects without maintaining references after the methods exited. This allowed the Ruby garbage collector to collect and delete these temporary files at any time. As a result, there was a race condition where the keytool binary could fail with a java.io.FileNotFoundException if the temporary files were deleted before keytool accessed them.

This commit resolves the issue by memoizing the Tempfile objects using instance variables (@temp_certificate_file and @temp_private_key_file). By caching these objects, we prevent them from being garbage collected prematurely, ensuring that the temporary files remain available for the duration of the provider instance.

Fixes #425

Previously, the `certificate` and `private_key` methods created `Tempfile`
objects without maintaining references after the methods exited. This allowed
the Ruby garbage collector to collect and delete these temporary files at any
time. As a result, there was a race condition where the `keytool` binary could
fail with a `java.io.FileNotFoundException` if the temporary files were deleted
before `keytool` accessed them.

This commit resolves the issue by memoizing the `Tempfile` objects using
instance variables (`@temp_certificate_file` and `@temp_private_key_file`). By
caching these objects, we prevent them from being garbage collected
prematurely, ensuring that the temporary files remain available for the
duration of the provider instance.

Fixes puppetlabs#425
@alexjfisher
alexjfisher requested a review from a team as a code owner October 23, 2024 10:10
@amitkarsale
amitkarsale merged commit a9b22d0 into puppetlabs:main Feb 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

random failures in java_ks provider

3 participants