Skip to content

Commit

Permalink
fix: Deadlock in megatools.__shellExec and some other fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Itz-fork committed Dec 30, 2023
1 parent da33686 commit d45e302
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
2 changes: 1 addition & 1 deletion megadl/helpers/cypher.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ async def cy_run(client: Client, msg: Message):
# Other exceptions
except Exception as e:
await self.cyeor(msg, f"**Oops 🫨, Somethig bad happend!** \n\n`{e}`")
await self.full_cleanup(self.dl_loc, uid)
await self.full_cleanup(f"{self.dl_loc}/{uid}", uid)
logging.warning(_emsg.format(self.version, func.__module__, e))

return cy_run
Expand Down
60 changes: 32 additions & 28 deletions megadl/lib/megatools.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,6 @@ async def prepare_string(nodes, shared_key, depth=0):
async def __shellExec(
self, cmd: str, user_id: int, chat_id: int = None, msg_id: int = None, **kwargs
):
print(cmd)
run = await asyncio.create_subprocess_shell(
cmd,
stdout=asyncio.subprocess.PIPE,
Expand All @@ -302,36 +301,41 @@ async def __shellExec(
)
self.client.mega_running[user_id] = run.pid

while True:
out = (
lambda _ots: _ots.decode("utf-8") if isinstance(_ots, bytes) else _ots
)(await run.stdout.readline())
err = (
lambda _ots: _ots.decode("utf-8") if isinstance(_ots, bytes) else _ots
)(await run.stderr.readline())

if err:
print(err)
self.__checkErrors(err)

print(f"out is: {out}")
if out and out != "":
print(out)
try:
await self.client.edit_message_text(
chat_id, msg_id, f"**Process info:** \n`{out}`", **kwargs
async def read_stream(stream, handler):
while True:
line = await stream.readline()
if line:
await handler(
line.decode("utf-8") if isinstance(line, bytes) else line
)
except:
pass
else:
break

async def handle_stdout(out):
try:
await self.client.edit_message_text(
chat_id, msg_id, f"**Process info:** \n`{out}`", **kwargs
)
except:
pass

async def handle_stderr(err):
if run.returncode is None:
await self.__checkErrors(err)

if run.poll() is not None:
break
stdout = read_stream(run.stdout, handle_stdout)
stderr = read_stream(run.stderr, handle_stderr)

try:
await asyncio.gather(stdout, stderr)
except asyncio.CancelledError:
asyncio.create_task(self.__terminate_sub(run))

await run.wait()

# sh_out = run.stdout.read()[:-1]
async def __terminate_sub(run):
run.terminate()
await run.wait()
sh_out = (await run.stdout.read()).decode("utf-8").strip()
self.__checkErrors(sh_out)
return sh_out

def __genErrorMsg(self, bs):
return f"""
Expand All @@ -342,7 +346,7 @@ def __genErrorMsg(self, bs):
You can open a new issue if the problem persists - https://github.com/Itz-fork/Mega.nz-Bot/issues
"""

def __checkErrors(self, out):
async def __checkErrors(self, out):
if "command not found" in out:
raise MegatoolsNotFound()

Expand Down
2 changes: 1 addition & 1 deletion megadl/modules/mega_dl.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ async def dl_from_cb(client: CypherClient, query: CallbackQuery):
),
)
if not f_list:
raise FileNotFoundError("Download failed")
return

await query.edit_message_text("`Successfully downloaded the content 🥳`")
# update download count
Expand Down

0 comments on commit d45e302

Please sign in to comment.