Skip to content

fix: ctypes callbacks crash on macOS ARM64 #112

@metaist

Description

@metaist

Created by Claude Opus 4 during discussion with @metaist.

Problem

ctypes callbacks crash with "Bus error: 10" on macOS ARM64 when using Cosmopolitan Python. This affects all Python versions, not just 3.14.

Reproduction:

import ctypes

CFUNC = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int)

def py_callback(x):
    return x * 2

cb = CFUNC(py_callback)  # Bus error here

Root Cause

Cosmopolitan's ffi_closure_alloc implementation has issues on macOS ARM64. The function is used internally by ctypes to create callable wrappers around Python functions.

Related

  • Python 3.14 added a warmup call to ffi_closure_alloc during _ctypes module init (cpython#128485), which caused import ctypes to hang. This was fixed in fix: ctypes callbacks crash on macOS ARM64 #112 by removing the warmup code.
  • The underlying ffi_closure_alloc issue remains and affects callback creation on all Python versions.

Impact

  • Basic ctypes usage works (c_int, Structure, Array, etc.)
  • Loading C libraries via ctypes does not work (expected - no dlopen)
  • Creating Python callbacks for C code does not work on macOS ARM64

Workaround

Avoid using ctypes.CFUNCTYPE callbacks on macOS ARM64. Use alternative approaches like:

  • Pre-compiled C extensions via cosmoext
  • Pure Python implementations

Notes

  • This may be a Cosmopolitan libffi bug that should be reported upstream
  • Linux x86_64 behavior is untested for this specific issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    aigenAI-generated content

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions