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

OSError with "The operation completed successfully." #51

Closed
njzjz opened this issue Aug 6, 2024 · 4 comments
Closed

OSError with "The operation completed successfully." #51

njzjz opened this issue Aug 6, 2024 · 4 comments

Comments

@njzjz
Copy link

njzjz commented Aug 6, 2024

We got this error message in the GitHub Actions (see log):

 tests\io\abinit\test_pseudos.py:11: in <module>
    from pymatgen.io.abinit.pseudos import Pseudo, PseudoTable
src\pymatgen\io\abinit\__init__.py:5: in <module>
    from .netcdf import (
src\pymatgen\io\abinit\netcdf.py:25: in <module>
    import netCDF4
C:\Users\runneradmin\micromamba\envs\pmg\lib\site-packages\netCDF4\__init__.py:28: in <module>
    _delvewheel_patch_1_7_0()
C:\Users\runneradmin\micromamba\envs\pmg\lib\site-packages\netCDF4\__init__.py:25: in _delvewheel_patch_1_7_0
    raise OSError('Error loading {}; {}'.format(lib, ctypes.FormatError(ctypes.get_last_error())))
E   OSError: Error loading charset-233f44715e5aacaf3a688c2faff5ddf7.dll; The operation completed successfully.

which is a piece of code in the netCDF4 package generated by delvewheel.

if os.path.isfile(lib_path) and not kernel32.LoadLibraryExW(ctypes.c_wchar_p(lib_path), None, 0x00000008):
raise OSError('Error loading {{}}; {{}}'.format(lib, ctypes.FormatError(ctypes.get_last_error())))

According to the Windows documentation, this error message means no error:

ERROR_SUCCESS
0 (0x0)
The operation completed successfully.

So the error message does not make sense to me.

@adang1345
Copy link
Owner

That's a strange error. Is this error intermittent? How frequently does it happen?

@njzjz
Copy link
Author

njzjz commented Aug 6, 2024

Not very frequently, but I have seen this message several times. When searching on GitHub, I also see someone got the same error message in a different package: rdkit/rdkit#7072

adang1345 added a commit that referenced this issue Aug 7, 2024
…ly.`

When LoadLibraryExW() is called from `ctypes`, the 64-bit void* return
value is cast to a 32-bit C int by default. If the base address of the
loaded DLL is a multiple of 2**32, then the lower 32 bits are 0, so
Python sees a return value of 0 and assumes that the DLL failed to load.
Set the return type properly so that the downcast is not done.

This fixes an intermittent issue with Python <= 3.7 or Conda Python <=
3.10 where an `OSError` is raised saying that a DLL failed to load but
also that the operation completed successfully.

Fix #51
@adang1345
Copy link
Owner

I have found the cause of the error. Unless told otherwise, ctypes assumes that the return type of a function is a 32-bit C int. LoadLibraryExW() returns a 64-bit pointer to where the DLL is loaded. If the DLL happens to be loaded at an address that is a multiple of 0x100000000, then casting the address to a 32-bit C int results in the value 0. Python then sees a return value of 0 and thinks that the DLL failed to load. The problem is intermittent because the address at which the DLL is loaded is random.

The solution is to tell ctypes what the return type is.

@janosh
Copy link

janosh commented Aug 8, 2024

thanks for the timely fix @adang1345! 👍

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

No branches or pull requests

3 participants