termux / termux/termux-api-package
run_api_command leaks file descriptors
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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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