Skip to content

Fix: add threading lock around JVM startup to prevent parallel connect crash (issue #60)#92

Open
HenryNebula wants to merge 3 commits intodevfrom
triage/60-jvm-parallel-crash
Open

Fix: add threading lock around JVM startup to prevent parallel connect crash (issue #60)#92
HenryNebula wants to merge 3 commits intodevfrom
triage/60-jvm-parallel-crash

Conversation

@HenryNebula
Copy link
Copy Markdown
Owner

Summary

Fixes #60: JVM crash when multiple threads call connect() simultaneously.

Changes

  • Added threading.Lock (_jvm_startup_lock) to serialize JVM startup in _jdbc_connect_jpype()
  • The check-then-act pattern (if not isJVMStarted(): startJVM()) was not atomic, allowing two threads to both attempt to start the JVM causing a SIGSEGV
  • Added mock test verifying parallel connections work correctly
  • Added integration test with HSQLDB verifying parallel connects

Test plan

  • Added ParallelConnectTest.test_parallel_connects_after_jvm_started in test/test_mock.py
  • Added ParallelConnectTest.test_jvm_startup_lock_exists in test/test_mock.py
  • Added ParallelConnectTest.test_parallel_connects_with_hsqldb in test/test_integration.py
  • Verified fix passes all 62 mock tests and 16 HSQLDB integration tests

Closes #60

Generated with Claude Code

@HenryNebula HenryNebula force-pushed the triage/60-jvm-parallel-crash branch 2 times, most recently from 38c2e7e to d7950e2 Compare April 27, 2026 17:44
HenryNebula and others added 3 commits April 28, 2026 09:02
…t crash (issue #60)

Multiple threads calling connect() simultaneously could both see
isJVMStarted() return False and both attempt to start the JVM,
causing a SIGSEGV crash. Add a threading.Lock to serialize JVM startup.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The original implementation held _jvm_startup_lock during the entire
startJVM() call. If JPype internally spawns threads during JVM
initialization that re-enter the module (e.g., via imports), the
non-reentrant Lock would deadlock. Refactored to only hold the lock
briefly when checking/setting a flag, then run startJVM() outside the
lock.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Worker threads that call connect() get attached to the JVM via
jpype.attachThreadToJVM(). Without explicit detachment, these
threads leave zombie Java threads that prevent JVM shutdown,
causing CI to time out.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@HenryNebula HenryNebula force-pushed the triage/60-jvm-parallel-crash branch from 80e25a4 to fd83627 Compare April 28, 2026 13:02
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

Successfully merging this pull request may close these issues.

1 participant