Avoid Invalid Memory Object error #13677
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
















Description
Windows runs of Chef get intermittent
ArgumentErrors for "Invalid Memory Object". This is due to an attempt to compare a legitimate WindowsHANDLEretrieved fromGetCurrentProcesswith anFFI::Pointer. The "Invalid Memory Object" error itself comes from trying to compare a memory object (FFI::Pointerwith the return value fromGetCurrentProcess(), aHANDLEfrom Windows)Windows data types
These errors do not result in breakage because of their happening in a finalizer, but
Error example
Analysis
Chef::ReservedNames::Win32::Security.open_process_tokenpasses a unsigned long andChef::ReservedNames::Win32::Process.get_current_processthe actualHANDLEfromGetCurrentProcess()... these finalize properly, because they are compared as numerical values inhandle == GetCurrentProcess().Chef::ReservedNames::Win32::Security.logon_userpasses the result ofFFI::Buffer#read_pointertoHandle.newinstead, which results in anFFI::Pointerbeing stored as the handle.Prior to Ruby 3.1/Chef 18/UCRT, this mismatch was at least not as noisy as it is in the current versions of everything.
Solution
Use
FFI::Buffer#read_ulongto prevent the 8-byte value from being interpreted as a Ruby memory object so that Ruby will assume it's comparing a primitive 8-byte value to the return value ofGetCurrentProcess(). WhileHANDLEis avoid *in Windows, Ruby has no type information or heap access to know what to do with the memory.Related Issue
Types of changes
Checklist: