Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Bug #261 destroyProcessByID not working #263

Open
wants to merge 30 commits into
base: master
Choose a base branch
from

Conversation

farimarwat
Copy link

@farimarwat farimarwat commented Dec 29, 2023

The ProcessBuilder() starts process for Python lib. Python lib further creates a process for ffmpeg if used for downloading. So the process returned by .start() method is only capable to kill child process(python) of the app not sub process(ffmpeg). As this(ffmpeg) belongs to the app tree, so we are able to kill it even android has restrctions to kill other's app process.
Tree for process:
App Process(PID)
-Python Process(PID)
--FFMPEG Process(PID)
Now the issue was that if we use "destroyProcessById()" the FFMPEG process still continue to download the app. So we have to manually kill this as well.
This patch will solve the bug.

Close #261

The ProcessBuilder() starts process for Python lib. Python lib further
creates a process for ffmpeg if used for downloading. So the process
returned by .start() method is only capable to kill child
process(python) of the app not sub process(ffmpeg). As this(ffmpeg)
belongs to the app tree, so we are able to kill it even android has
restrctions to kill other's app process.
Tree for process:
App Process(PID)
-Python Process(PID)
--FFMPEG Process(PID)
Now the issue was that if we use "destroyProcessById()" the FFMPEG
process still continue to download the app. So we have to manually kill
this as well.
This patch will solve the bug.
@JunkFood02
Copy link
Collaborator

JunkFood02 commented Dec 29, 2023

Thanks for your PR! According to the code, it seems we're trying to terminate all the child processes when destroy a single yt-dlp process, this might not be the expected behavior if we want to allow multiple yt-dlp instance to be running at the same time

@JunkFood02
Copy link
Collaborator

Maybe we can try using pstree or something similiar to identify the child processes of the exact yt-dlp process we want to destroy

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need this permission when killing our own processes? See also: https://developer.android.com/reference/android/Manifest.permission#KILL_BACKGROUND_PROCESSES

@@ -120,6 +120,7 @@ object YoutubeDL {

fun destroyProcessById(id: String): Boolean {
if (idProcessMap.containsKey(id)) {
killChildProcesses()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should only terminate the child processes of the exact process to be destory

fun killChildProcesses() {
val process = Runtime.getRuntime().exec("ps -A -o pid,cmd")
process.inputStream.bufferedReader().useLines { lines ->
lines.filter { it.contains("ffmpeg") }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe aria2c too? Not sure about how many external executables in our libraies could have this issue

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this function should actually be named killAllFFmpegProcesses according to its implementation

This pathch will also issue of killing selective child process. The
process is to get PID from process which we want to kill(via
reflection). Next get child (ffmpeg) id for it and kill
@farimarwat
Copy link
Author

I have successfully updated the code to kill only selective ffmpeg process. Kindly test and feed back me

farimarwat added 4 commits May 16, 2024 19:40
Previously it was giving error of outofmemory and replaced with buffered reader but not working. Now wrapped with trycatch
Code optimized to handle memory exception
This bug is only found while using the library
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.

destroyProcessByID not working with external executables running in child processes
2 participants