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

Free-threaded CPython and threads that start the JVM together: serialize start_jvm(), define the JPype proxy classes once, add a thread stress test #453

Closed

Description

@tae898

Threads whose first call is create_database() or open_database() race into jpype.startJVM, and a free-threaded CPython (no GIL) shows more races. We ran the bindings on 3.14.5 free-threaded (PYTHON_GIL=0, JPype 1.7.1, which publishes cp313t and cp314t wheels and keeps the GIL off) against 3.14.5 with the GIL as the control, with a new stress test that runs many threads against one database and one database each and compares the result with a sequential run.

Two bugs in the bindings, both fixed on the branch ft-free-threading:

  1. start_jvm() checks isJVMStarted() and then calls startJVM() with no lock. With 16 threads that all start with create_database() or open_database(), 3 of 3 runs failed with Failed to start JVM: JVM is already started on ordinary 3.14 with the GIL, so this is a bug for every user who starts the first call from several threads, not only on free-threaded builds. On 3.14t all but one thread got Unable to start JVM. The fix is a lock around start_jvm(); the threads that lose the race take the "JVM already started" branch. After the fix 12 of 12 runs were clean on each interpreter.
  2. callbacks._function_proxy_class() and _event_proxy_class() check the cache and then define the @JImplements class. 16 threads that register their first function together defined 10 to 16 classes, and each one leaks, which is why the cache exists. 5 of 5 repeats of the new scenario failed before the fix and 5 of 5 pass with the definition under a lock and a second check.

Open, not fixable in the bindings, to document on the Known Engine Issues page:

  • On 3.14t only, the Python heap grows by about 520 to 610 bytes per register_function(), on(), remove() cycle, and not with the GIL. test_registering_again_and_again_does_not_grow_the_python_heap fails there. The cause is not attributed; JPype's cleanup of proxy objects is suspected (compare jp_gc.cpp: unsynchronized trigger state, accidental throttling, and a possible collection storm under memory pressure jpype-project/jpype#1506). The test becomes xfail (not strict) on a free-threaded build.
  • Rare SIGSEGV in JPype's JPHintsConversion::matches when several threads make their first Java calls together: 2 of 30 first-start runs on 3.14t, 0 of 27 with the GIL. A warm-up (open a database and run one statement on the main thread before the workers start) is the obvious precaution and is not verified.
  • A process in which a non-main thread used Java does not exit (Advice: Threading and JPype - JVM never ends jpype-project/jpype#1169), on every Python, with or without the GIL. jpype.java.lang.Thread.detach() at the end of the thread, or jpype.config.destroy_jvm = False (which skips the JVM shutdown hooks), make it exit. The bindings keep JPype's default.

The native backend needed no change: its shared state is per call, per thread or behind a lock, and 0 of the repeats failed on either interpreter.

Activity

  1. added
    bug
    on Oct 22, 2026
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions