Skip to content
This repository was archived by the owner on Nov 13, 2026. It is now read-only.
This repository was archived by the owner on Nov 13, 2026. It is now read-only.

Crash on library unload via Python #6993

Closed

Description

@letmaik

The following code invokes the NativeLibrary sample shared library in Python on Windows using CFFI. It exits with code -1073741819. Note that "checks pass" is printed and the crash happens on shutdown / library unloading or similar.

# pip install cffi
# dotnet publish /p:NativeLib=Shared -r win-x64 -c Debug

import os
from cffi import FFI

PROJ_DIR = os.path.join(os.path.dirname(__file__))
LIB_BASE_DIR = os.path.join(PROJ_DIR, 'bin', 'Debug', 'netstandard2.0')
LIB_PATH = os.path.join(LIB_BASE_DIR, 'win-x64', 'native', 'NativeLibrary.dll')

ffi = FFI()
ffi.cdef('long add(long a, long b);')
lib = ffi.dlopen(LIB_PATH)

assert lib.add(1, 2) == 3
assert lib.add(2147483647, 1) == -2147483648
print('checks pass')

Simply copy this into a test.py file in the project folder and run python test.py. Check the exit code with echo %errorlevel% if using command prompt/Anaconda prompt.

When running this under WinDbg I can see that the crash happens here:

<Unloaded_NativeLibrary.dll>+0x2ca820
ntdll!RtlProcessFlsData+0x145
ntdll!LdrShutdownProcess+0x92
ntdll!RtlExitUserProcess+0xad
KERNEL32!FatalExit+0xa
ucrtbase!exit+0x1d4
ucrtbase!exit+0x7f
python!__scrt_common_main_seh+0x13d
KERNEL32!BaseThreadInitThunk+0x14
ntdll!RtlUserThreadStart+0x21

The log says:

(5540.5dbc): Access violation - code c0000005 (first chance)

For reference, this uses Python 3.6.7 64 bit from the Anaconda distribution.

Activity

  1. letmaik commented on Feb 11, 2019

    @letmaik
    Author

    Interestingly I don't get the crash when using ctypes:

    import os
    from ctypes import *
    
    PROJ_DIR = os.path.join(os.path.dirname(__file__))
    LIB_BASE_DIR = os.path.join(PROJ_DIR, 'bin', 'Debug', 'netstandard2.0')
    LIB_PATH = os.path.join(LIB_BASE_DIR, 'win-x64', 'native', 'NativeLibrary.dll')
    
    lib = CDLL(LIB_PATH)
    
    assert lib.add(1, 2) == 3
    assert lib.add(2147483647, 1) == -2147483648
    print('checks pass')
  2. letmaik commented on Feb 11, 2019

    @letmaik
    Author

    I reported this as well at the CFFI issue tracker as it may be a bug on their end, even though it's quite odd as I've used CFFI before for other things and the example couldn't get any more trivial.

  3. MichalStrehovsky commented on Feb 12, 2019

    @MichalStrehovsky
    Member

    Also reproes if one adds a FreeLibrary call to the SharedLibrary test in the repo.

    Likely because we have a FlsAlloc call in the runtime without a matching FlsFree. We'll need to shut down the runtime before the library is unloaded, otherwise we leave a dangling callback pointer. This probably includes things like shutting down the finalizer thread and probably a bunch of other things.

  4. added a commit that references this issue on Feb 13, 2019
    04fb46f
  5. letmaik commented on Feb 14, 2019

    @letmaik
    Author

    Verified, all fine now. Thanks for the quick fix :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions