Skip to content

"TypeError: 'NoneType' object is not callable" during calls to wasmtime.Managed.__del__ #302

Closed

Description

@moreati

One or more exceptions occur during Python interpreter shutdown, after the loader_component.py example is executed in an interactive session (REPL).

Steps to reproduce

  1. Star a Python REPL
  2. Paste the contents of https://github.com/bytecodealliance/wasmtime-py/blob/main/examples/loader_component.py
  3. Exit the REPL

Result:

Python 3.14.0 (main, Oct 14 2025, 21:10:22) [Clang 20.1.4 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import wasmtime, wasmtime.loader
... 
... import loader_component_add  # type: ignore
... 
... 
... store = wasmtime.Store()
... component = loader_component_add.Root(store)
... assert component.add(store, 1, 2) == 3
... 
>>> 
Exception ignored while calling deallocator <function Managed.__del__ at 0x102961170>:
Traceback (most recent call last):
  File "/Users/alex/src/wasmtime-py/.venv/lib/python3.14/site-packages/wasmtime/_managed.py", line 95, in __del__
  File "/Users/alex/src/wasmtime-py/.venv/lib/python3.14/site-packages/wasmtime/_managed.py", line 44, in close
  File "/Users/alex/src/wasmtime-py/.venv/lib/python3.14/site-packages/wasmtime/_store.py", line 31, in _delete
TypeError: 'NoneType' object is not callable
Exception ignored while calling deallocator <function Managed.__del__ at 0x102961170>:
Traceback (most recent call last):
  File "/Users/alex/src/wasmtime-py/.venv/lib/python3.14/site-packages/wasmtime/_managed.py", line 95, in __del__
  File "/Users/alex/src/wasmtime-py/.venv/lib/python3.14/site-packages/wasmtime/_managed.py", line 44, in close
  File "/Users/alex/src/wasmtime-py/.venv/lib/python3.14/site-packages/wasmtime/_engine.py", line 19, in _delete
TypeError: 'NoneType' object is not callable

The exception does not occur when the example is run as a script

(wasmtime-py) ➜  examples git:(nox) ✗ python loader_component.py 
(wasmtime-py) ➜  examples git:(nox) ✗ echo $?   
0

Activity

  1. moreati commented on Oct 27, 2025

    @moreati
    ContributorAuthor

    The problem may be specific to Python >= 3.13. I could not reproduce it with 3.9 - 3.12 (as builkt for uv on macOS)

    Details
    ➜  examples git:(nox) ✗ uv venv --clear -p3.12 && uv pip install wasmtime && .venv/bin/python
    Using CPython 3.12.11
    Creating virtual environment at: .venv
    Activate with: source .venv/bin/activate
    Resolved 2 packages in 1ms
    Installed 2 packages in 2ms
     + importlib-resources==6.5.2
     + wasmtime==38.0.0
    Python 3.12.11 (main, Aug 18 2025, 19:02:39) [Clang 20.1.4 ] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import wasmtime, wasmtime.loader
    
    import loader_component_add  # type: ignore
    
    
    store = wasmtime.Store()
    component = loader_component_add.Root(store)
    assert component.add(store, 1, 2) == 3
    >>> 
    >>> import loader_component_add  # type: ignore
    >>> 
    >>> 
    >>> store = wasmtime.Store()
    >>> component = loader_component_add.Root(store)
    >>> assert component.add(store, 1, 2) == 3
    >>> ^D
    ➜  examples git:(nox) ✗ uv venv --clear -p3.13 && uv pip install wasmtime && .venv/bin/python
    Using CPython 3.13.7
    Creating virtual environment at: .venv
    Activate with: source .venv/bin/activate
    Resolved 2 packages in 1ms
    Installed 2 packages in 2ms
     + importlib-resources==6.5.2
     + wasmtime==38.0.0
    Python 3.13.7 (main, Aug 18 2025, 19:02:43) [Clang 20.1.4 ] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import wasmtime, wasmtime.loader
    ... 
    ... import loader_component_add  # type: ignore
    ... 
    ... 
    ... store = wasmtime.Store()
    ... component = loader_component_add.Root(store)
    ... assert component.add(store, 1, 2) == 3
    ... 
    >>> 
    Exception ignored in: <function Managed.__del__ at 0x10473cea0>
    Traceback (most recent call last):
      File "/Users/alex/src/wasmtime-py/examples/.venv/lib/python3.13/site-packages/wasmtime/_managed.py", line 95, in __del__
      File "/Users/alex/src/wasmtime-py/examples/.venv/lib/python3.13/site-packages/wasmtime/_managed.py", line 44, in close
      File "/Users/alex/src/wasmtime-py/examples/.venv/lib/python3.13/site-packages/wasmtime/_store.py", line 31, in _delete
    TypeError: 'NoneType' object is not callable
    Exception ignored in: <function Managed.__del__ at 0x10473cea0>
    Traceback (most recent call last):
      File "/Users/alex/src/wasmtime-py/examples/.venv/lib/python3.13/site-packages/wasmtime/_managed.py", line 95, in __del__
      File "/Users/alex/src/wasmtime-py/examples/.venv/lib/python3.13/site-packages/wasmtime/_managed.py", line 44, in close
      File "/Users/alex/src/wasmtime-py/examples/.venv/lib/python3.13/site-packages/wasmtime/_engine.py", line 19, in _delete
    TypeError: 'NoneType' object is not callable
  2. changed the title [-]"TypeError: 'NoneType' object is not callable" in wasmtime.Mananged.__del__[/-] [+]"TypeError: 'NoneType' object is not callable" during calls to wasmtime.Managed.__del__[/+] on Oct 27, 2025
  3. moreati commented on Oct 27, 2025

    @moreati
    ContributorAuthor

    Smallest reproducer so far

    >>> import wasmtime, wasmtime.loader
    >>> import loader_component_add
    >>> store = wasmtime.Store()
    Details
    ➜  examples git:(nox) .venv/bin/python                                                     
    Python 3.13.7 (main, Aug 18 2025, 19:02:43) [Clang 20.1.4 ] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import wasmtime, wasmtime.loader
    >>> import loader_component_add
    >>> store = wasmtime.Store()
    >>> 
    Exception ignored in: <function Managed.__del__ at 0x1035b8a40>
    Traceback (most recent call last):
      File "/Users/alex/src/wasmtime-py/examples/.venv/lib/python3.13/site-packages/wasmtime/_managed.py", line 95, in __del__
      File "/Users/alex/src/wasmtime-py/examples/.venv/lib/python3.13/site-packages/wasmtime/_managed.py", line 44, in close
      File "/Users/alex/src/wasmtime-py/examples/.venv/lib/python3.13/site-packages/wasmtime/_store.py", line 31, in _delete
    TypeError: 'NoneType' object is not callable
    Exception ignored in: <function Managed.__del__ at 0x1035b8a40>
    Traceback (most recent call last):
      File "/Users/alex/src/wasmtime-py/examples/.venv/lib/python3.13/site-packages/wasmtime/_managed.py", line 95, in __del__
      File "/Users/alex/src/wasmtime-py/examples/.venv/lib/python3.13/site-packages/wasmtime/_managed.py", line 44, in close
      File "/Users/alex/src/wasmtime-py/examples/.venv/lib/python3.13/site-packages/wasmtime/_engine.py", line 19, in _delete
    TypeError: 'NoneType' object is not callable
  4. alexcrichton commented on Oct 27, 2025

    @alexcrichton
    Member

    This might be due to something happening during process shutdown? As python variables are thrown away the store the loader uses gets disposed of but I guess the dll has already been unloaded which prevents destroying the store?

    I'm not sure if there's a way to force the modules to get destroyed in a different way or something like that. Or maybe register some sort of shutdown hook?

  5. moreati commented on Oct 28, 2025

    @moreati
    ContributorAuthor

    I'm still reading up and pondering. In general using __del__() is discouraged, first order advice is don't use it; use other mechanisms instead (e.g. __enter__()/__exit__(), weakref.finalize()).

    CPython docs have a list of warnings, PyPy documents further intricacies/differences.

    Open questions in my head

    1. How much is wamtime-py using __del__() as a load bearing part of shutdown/cleanup vs a just-in-case safety net for abnormal cases?
    2. How can we know our answer to 1 is the case, and remains the case?

    Avenues of investigation (non exhaustive, not mutually exclusive)

    1. Delete the __del__()s and see how much of the test suite passes
    2. Run the test suite with additional intrumentation/warnings enabled
    3. Emit warnings in the __del__()s, perhaps opt-in (e.g. enabled in CI). The subprocess module does this, which helped me identify/fix file descriptor leaks in https://github.com/mitogen-hq/mitogen.
  6. moreati commented on Oct 28, 2025

    @moreati
    ContributorAuthor

    Providing FFI callacbles to __del__() implementations (73e7f44) didn't eliminate the TypeErrors.

    Providing FFI callacbles to _delete() implementations (4475354) did.

    I'm not proposing these commits as fixes, they're just investigation results.

  7. alexcrichton commented on Oct 29, 2025

    @alexcrichton
    Member

    Yeah this one's a bit tough. I don't believe that __del__ can be removed as it's used here as a last-ditch attempt to cleanup resources. The test suite would probably pass if they were all deleted, but that would also mean that quite a lot of memory is leaked. All managed objects already support __enter__ and explicit close method calls, so it's possible to explicitly and manually manage memory but __del__ can't be deleted as that's not always required to be called.

    I think the main issue here is that there's a global which has wasm which is being deleted during process teardown. One option might be to, for that specific object, remove its destructor. That might be a specific override perhaps? But basically the loader's global store could be configured to never get dropped

  8. alexcrichton commented on Nov 1, 2025

    @alexcrichton
    Member

    My conclusion on this is that this is unfixable personally. I'd be ok deleting the loader module as while it's kind of "cute" to import a module I feel like this is an example where it's too magic.

  9. moreati commented on Nov 1, 2025

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions