clj-python / clj-python/libpython-clj

How to properly hold onto an object in embedded mode?

オープン
#267 コメント 19 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
Clojure
スター
1.2k
フォーク
74
PR マージ指標
30日以内にマージされた PR はありません

説明

Hello! First, I'd like to thank you for the amazing work on this library. It's a mind-bending level of inter-language interaction, IMO. 👏

I was experimenting with it in the embedded mode. Not sure if I was using it in the intended way, but... I took a considerably sized web-app and tried to add some kind of a debugging/introspection interface to it with clj & cljs.

The app uses gevent internally for non-blocking IO operations, so the setup was a bit involving, but in the end it worked out. I'll explain it to get a better picture:

  • app starts as a Python process
  • gevent is called at the earliest to do its monkey-patching of IO
  • python app continues it's startup
  • separate python thread (real OS thread) is started, which does all JVM/clojure initialization (one more thread I guess) and than calls -main of the clojure's part of the app
  • in the main thread of the python app a queue is created and worker greenlet (fake thread or coroutine) is started to watch it
  • when clojure world needs to call something from python world, that involves gevent, it puts a task into a queue for the worker to execute it on the main thread
  • if task needs to return something to clojure world, a one-off queue is created to transfer the return value

It worked fine, I've even made a little introspection tool, that can peer into a running python app vars and call functions, toggle stuff and so on.

Then, I've tried to implement a tap> like system (just putting python objects into an atom for later inspection), to send info from python to clojure... And was abruptly stopped by a SIGSEGV. :(

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x0000aaaac541ef74, pid=1685, tid=1869
#
# JRE version: OpenJDK Runtime Environment (17.0.12+7) (build 17.0.12+7-Ubuntu-1ubuntu222.04)
# Java VM: OpenJDK 64-Bit Server VM (17.0.12+7-Ubuntu-1ubuntu222.04, mixed mode, sharing, tiered, compressed oops, compressed class ptrs, g1 gc, linux-aarch64)
# Problematic frame:
# C  [python3+0xcef74]
#
# No core dump will be written. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /var/www/hs_err_pid1685.log
#
# If you would like to submit a bug report, please visit:
#   https://bugs.launchpad.net/ubuntu/+source/openjdk-17
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

It seems that objects I send through "tap" got GC'ed on python part. I've tried to find something about holding to objects in the documentation, but nothing was particularly fitting to my case. Digging through the code, I've stumbled upon track-pyobject and incref-and-track. Using the latter on the objects I send to clojure world did not help though... I still got a SIGSEGV, but in different internal python function. To validate my assumption about objects being GD'ed, I've implemented a dumb "borrowing" mechanics on python side (putting objects, to be sent to clojure, in a dict) an it worked okay.

I'd prefer not to do ->jvm on objects, because the idea is to use them again in a python app to replay some actions that it performs.

My question: Is there a way to hold onto python objects from clojure in embedded mode so that refcounts will be decreased when references to the pyobjs are GC'ed on the JVM?

Thank you for your time :)

Python 3.10.12
Clojure 1.12
openjdk 17.0.12 2024-07-16
OpenJDK Runtime Environment (build 17.0.12+7-Ubuntu-1ubuntu222.04)
OpenJDK 64-Bit Server VM (build 17.0.12+7-Ubuntu-1ubuntu222.04, mixed mode, sharing)

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

レポートでは、埋め込みモードと track-pyobject および incref-and-track のエントリポイントが挙げられている。まず、それらの所有権と refcount の挙動を追跡しながら、Python 3.10 と Clojure 1.12 で SIGSEGV を再現する。完了条件は、JVM のガベージコレクションをまたいで Python オブジェクトを安全に保持する方法を確立するか、埋め込みモードではこの用途をサポートしないことを文書化することになる。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
clojure, python
領域
backend
issue の種類
バグ
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。