termux / termux/termux-api-package

run_api_command leaks file descriptors

Open
#224 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Shell
Stars
1.7k
Forks
523
PR merge metrics
No merged PRs in 30d

Description

When using the C library calls, run_api_command appears to leak file descriptors. Multiple calls to run_api_command in the same process builds up UNIX-STREAM file descriptors (two per call, presumably the in and out descriptors for communicating with the Termux:API plugin).

The created sockets should be closed before returning.

Code to reproduce (via. Python):

import ctypes
import sys
from os import getpid
from signal import pause

run_api_command = ctypes.CDLL("libtermux-api.so").run_api_command
run_api_command.argtypes = [ ctypes.c_int, ctypes.POINTER(ctypes.c_char_p) ]
run_api_command.restype = ctypes.c_int

args = f"{sys.argv[0]} Usb -a list"
argb = args.encode('utf8').split(b' ')
argv = (ctypes.c_char_p * len(argb))(*argb)

run_api_command(len(argv), argv)
run_api_command(len(argv), argv)
run_api_command(len(argv), argv)

print(f"Process ID: {getpid()}")
pause()

Running lsof -p [pid] on the resulting pid will reveal the leaked file descriptors.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the run_api_command entry point in the C library and reproduce the problem with the provided Python ctypes script; inspect the UNIX-STREAM descriptors with lsof. Trace the input and output socket lifecycle and verify that repeated calls no longer leave descriptors open.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, python
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
66/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.