Amourspirit / python_ooo_dev_tools

Apache License 2.0
21 stars 5 forks source link

ResourceWarning: subprocess is still running #671

Open JulienPalard opened 5 days ago

JulienPalard commented 5 days ago

Using ooo-dev-tools 0.47.18 (also on 0.47.17 and I don't remember for previous ones) I'm getting a ResourceWarning about un unkilled subprocess:

$ PYTHONTRACEMALLOC=20 python repro.py
05/10/2024 09:24:20 - DEBUG - LoInst - Root: LoInst Init
05/10/2024 09:24:20 - DEBUG - LoInst - Root: LoInst created
05/10/2024 09:24:20 - DEBUG - LoInst - Root: load_office()
05/10/2024 09:24:20 - DEBUG - LoInst - Root: on_reset() Triggering RESET
05/10/2024 09:24:20 - DEBUG - LoInst - Root: on_reset() RESET Triggered. Current Doc is None
05/10/2024 09:24:20 - DEBUG - LoInst - Root: on_office_loading() Triggering OFFICE_LOADING
05/10/2024 09:24:20 - DEBUG - LoInst - Root: on_office_loading() Triggered OFFICE_LOADING.
Warning: failed to launch javaldx - java may not function correctly
05/10/2024 09:24:21 - DEBUG - LoInst - Root: on_office_loaded() Triggering OFFICE_LOADED
05/10/2024 09:24:21 - DEBUG - LoInst - Root: on_office_loaded() Triggered OFFICE_LOADED
05/10/2024 09:24:21 - DEBUG - LoInst - Root: load_office() Loaded Office
05/10/2024 09:24:21 - DEBUG - LoInst - Root: Closing Office
05/10/2024 09:24:21 - DEBUG - LoInst - Root: on_office_closing() Triggering OFFICE_CLOSING
05/10/2024 09:24:21 - DEBUG - LoInst - Root: on_office_closing() Triggered OFFICE_CLOSING
05/10/2024 09:24:21 - DEBUG - LoInst - Root: Office terminated
05/10/2024 09:24:21 - DEBUG - LoInst - Root: Office bridge has gone!!
05/10/2024 09:24:21 - DEBUG - LoInst - Root: on_office_closed() Triggering OFFICE_CLOSED
05/10/2024 09:24:21 - DEBUG - LoInst - Root: on_office_closed() Triggered OFFICE_CLOSED
05/10/2024 09:24:21 - DEBUG - LoInst - Root: on_reset() Triggering RESET
05/10/2024 09:24:21 - DEBUG - LoInst - Root: on_reset() RESET Triggered. Current Doc is None
/usr/lib/python3.12/subprocess.py:1127: ResourceWarning: subprocess 1006971 is still running
  _warn("subprocess %s is still running" % self.pid,
Object allocated at (most recent call last):
  File "repro.py", lineno 12
    b()
  File "repro.py", lineno 9
    with Lo.Loader(Lo.ConnectSocket(headless=True), opt=options) as loader:
  File ".venv/lib/python3.12/site-packages/ooodev/loader/lo.py", lineno 212
    self.loader = Lo.load_office(
  File ".venv/lib/python3.12/site-packages/ooodev/loader/lo.py", lineno 752
    result = cls._lo_inst.load_office(connector=connector, cache_obj=cache_obj)
  File ".venv/lib/python3.12/site-packages/ooodev/loader/inst/lo_inst.py", lineno 730
    lo_loader = LoLoader(connector=b_connector, cache_obj=cache_obj, opt=opt)
  File ".venv/lib/python3.12/site-packages/ooodev/loader/inst/lo_loader.py", lineno 64
    self._lo_inst.connect()
  File ".venv/lib/python3.12/site-packages/ooodev/conn/connect.py", lineno 612
    self._popen()
  File ".venv/lib/python3.12/site-packages/ooodev/conn/connect.py", lineno 667
    self._popen_from_args(args, shutdown)
  File ".venv/lib/python3.12/site-packages/ooodev/conn/connect.py", lineno 292
    self._soffice_process = subprocess.Popen(

with repro.py containing:

import logging

from ooodev.loader.inst.options import Options as LoOptions
from ooodev.loader.lo import Lo

def b():
    options = LoOptions(log_level=logging.DEBUG)
    with Lo.Loader(Lo.ConnectSocket(headless=True), opt=options) as loader:
        pass

b()
Amourspirit commented 5 days ago

I don't know how to reproduce this. Do your have more information?

BTW: I am working from Ubuntu 24.04

Amourspirit commented 5 days ago

Here is the script I am running:

import logging

from ooodev.loader.inst.options import Options as LoOptions
from ooodev.loader.lo import Lo
from pathlib import Path
import tempfile

def b():
    options = LoOptions(log_level=logging.DEBUG)
    with tempfile.TemporaryDirectory() as tmpdirname:
        tmpdir = Path(tmpdirname)
        with Lo.Loader(
            Lo.ConnectSocket(
                headless=True,
                extended_args=[
                    f"-env:UserInstallation={tmpdir.as_uri()}/user_profile",
                    f"-env:UNO_SHARED_PACKAGES_CACHE={tmpdir.as_uri()}/shared_packages_cache",
                ],
            ),
            opt=options,
        ) as loader:
            pass

b()

The env args just tells LibreOffice to not load any user or shared extensions by setting their paths to a temp directory.

On the command Line I run:

PYTHONTRACEMALLOC=20 python repro.py

Entire Results are:

05/10/2024 12:42:15 - DEBUG - LoInst - Root: LoInst Init
05/10/2024 12:42:15 - DEBUG - LoInst - Root: LoInst created
05/10/2024 12:42:15 - DEBUG - LoInst - Root: load_office()
05/10/2024 12:42:15 - DEBUG - LoInst - Root: on_reset() Triggering RESET
05/10/2024 12:42:15 - DEBUG - LoInst - Root: on_reset() RESET Triggered. Current Doc is None
05/10/2024 12:42:15 - DEBUG - LoInst - Root: on_office_loading() Triggering OFFICE_LOADING
05/10/2024 12:42:15 - DEBUG - LoInst - Root: on_office_loading() Triggered OFFICE_LOADING.
05/10/2024 12:42:17 - DEBUG - LoInst - Root: on_office_loaded() Triggering OFFICE_LOADED
05/10/2024 12:42:17 - DEBUG - LoInst - Root: on_office_loaded() Triggered OFFICE_LOADED
05/10/2024 12:42:17 - DEBUG - LoInst - Root: load_office() Loaded Office
05/10/2024 12:42:17 - DEBUG - LoInst - Root: Closing Office
05/10/2024 12:42:17 - DEBUG - LoInst - Root: on_office_closing() Triggering OFFICE_CLOSING
05/10/2024 12:42:17 - DEBUG - LoInst - Root: on_office_closing() Triggered OFFICE_CLOSING
05/10/2024 12:42:17 - DEBUG - LoInst - Root: Office terminated
05/10/2024 12:42:17 - DEBUG - LoInst - Root: Office bridge has gone!!
05/10/2024 12:42:18 - DEBUG - LoInst - Root: on_office_closed() Triggering OFFICE_CLOSED
05/10/2024 12:42:18 - DEBUG - LoInst - Root: on_office_closed() Triggered OFFICE_CLOSED
05/10/2024 12:42:18 - DEBUG - LoInst - Root: on_reset() Triggering RESET
05/10/2024 12:42:18 - DEBUG - LoInst - Root: on_reset() RESET Triggered. Current Doc is None
Amourspirit commented 5 days ago

Hmm... I noticed Warning: failed to launch javaldx - java may not function correctly in our output. I wonder if this is related?

Amourspirit commented 5 days ago

The next release has extra steps in place when closing. Most likely this will be resolved.

JulienPalard commented 4 days ago

Forgot to say there's an export PYTHONDEVMODE=y in my bashrc, this enables cpython to show this kind of warnings, maybe that's just what's missing?

sry

JulienPalard commented 4 days ago

Warning: failed to launch javaldx - java may not function correctly

Seens this too, but I have no idea what this is :D Tried $ apt search javaldx, found nothing (I'm running Debian here).

Amourspirit commented 4 days ago

I have found the default version of Debian to be a bit problematic. When I tried to install APSO or LibrePythonista (the only two I tried) on a default installation of Debian they would not install. I notiecd the default Debian did not have LibreOffice Base installed. Also when I checked in LibreOffice Settings there was no Java installed.

I tried so many ways to install java on Debian for LibreOffice and had no luck. I figured the failed installs of APSO and Librepyhthonista in Debian were due to missing Java. I could not fix it so I installed a Flatpak version of LibreOffice and then it worked fine.

Amourspirit commented 3 days ago

Version 0.47.19 has been released to day. I now monitors the subprocess that starts LibreOffice and shuts it down when the main thread stops.