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

Memory leak when calling run_script #338

Closed
Apprisco opened this issue Aug 17, 2024 · 6 comments
Closed

Memory leak when calling run_script #338

Apprisco opened this issue Aug 17, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@Apprisco
Copy link

Apprisco commented Aug 17, 2024

describe your issue

Had a python based AHK script running that consistently presses some values using asyncahk.
Memory kept increasing over time, currently over 4GB.``

ahk.version

latest

AutoHotkey version

v1

Code to reproduce the issue

from ahk import AHK,AsyncAHK

class Trove:
    ahk=AHK(directives=[NoTrayIcon])
    asyncAHK=AHK(directives=[NoTrayIcon])

while True:
    script='''.......'''
    ahk.run_script(script)
    asyncAHK.run_script(script)

Traceback/Error message

Observe memory usage increasing over time.

@spyoungtech spyoungtech added the bug Something isn't working label Aug 17, 2024
@spyoungtech spyoungtech changed the title Memory leak.. Memory leak when calling run_script Aug 17, 2024
@spyoungtech
Copy link
Owner

Thanks for the report. I confirmed this behavior is reproducible with run_script and calls with blocking=False. It's likely related to resources for the subprocess.

@spyoungtech
Copy link
Owner

spyoungtech commented Aug 17, 2024

I believe I found the cause of this.

The creation of AutoHotkey processes uses the start method (or here in the sync API), which includes a call to atexit.register, which is an attempt to ensure that the subprocess will be cleaned up when the Python script is finished -- to avoid leaving AutoHotkey running after Python is done executing.

However, this obviously causes a reference to be held for the subprocess.Popen object, so it never gets garbage collected.

When I remove this line, the memory buildup no longer occurs.

@Apprisco
Copy link
Author

Apprisco commented Aug 17, 2024

How am I able to copy your fix? Will there be an updated version of AHK wrapper released soon? Thanks :)
This library is genuinely awesome for people who want to use AHK but not in a synchronous loop, and it's pretty damn great asides from the memory leak lol.

While I understand this is an open source project and i cannot expect a fast response, a memory leak is a pretty critical issue to deal with.

@spyoungtech
Copy link
Owner

@Apprisco I've opened #339 with a fix for this issue. As long as tests pass, it should be released within the next day, if not the next hour or so.

spyoungtech added a commit that referenced this issue Aug 17, 2024
GH-338 allow garbage collection of non-daemon processes
@spyoungtech
Copy link
Owner

This is fixed in version 1.8.0

@Apprisco
Copy link
Author

Hell yeah, thanks man!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants