Tool to diagnose why/where a long script is stuck?

Get help with using AutoHotkey and its commands and hotkeys
freespacing
Posts: 97
Joined: 28 Sep 2016, 11:14
Contact:

Tool to diagnose why/where a long script is stuck?

09 Apr 2019, 10:24

I have a very long script (1,800 lines and growing) that gets called via various entry points. There are no permanent states such as defined hotkeys or CapsLock, and usually the script exits okay.

But quite often, the script fails to exit. The reason I know this is that:
  • With #SingleInstance force, the compiler often warns that it cannot write the file (because the exe is already running)
  • With #SingleInstance off, I see multiple icons in the tray.
It's not always (the script gets called a lot and only gets stuck a few times), and I can't tell what is triggering this.

Is there a way to troubleshoot this, for instance a tool that might introspect one of the old icons of the running script and tell me what line it is stuck on?
User avatar
jeeswg
Posts: 6395
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Tool to diagnose why/where a long script is stuck?

09 Apr 2019, 10:48

Some of those symptoms sound reminiscent of trying to reopen an admin script, as a non-admin script.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
iseahound
Posts: 408
Joined: 13 Aug 2016, 21:04
GitHub: iseahound

Re: Tool to diagnose why/where a long script is stuck?

09 Apr 2019, 11:12

You need to rewrite your code. Refactor and refactor. The fact that you have issues has nothing to do with any lack of diagnostic tools. But rather you need to step back, create a new file, and copy and paste the bits that work, look for patterns and abstract them into functions, then turn your functions into objects.
freespacing
Posts: 97
Joined: 28 Sep 2016, 11:14
Contact:

Re: Tool to diagnose why/where a long script is stuck?

09 Apr 2019, 11:34

iseahound wrote:
09 Apr 2019, 11:12
You need to rewrite your code. Refactor and refactor. The fact that you have issues has nothing to do with any lack of diagnostic tools. But rather you need to step back, create a new file, and copy and paste the bits that work, look for patterns and abstract them into functions, then turn your functions into objects.
I see that I may not have explained the problem well.

The code is already tightly compartmentalized. It's not spaghetti. Everything gets off-loaded to functions when possible. No single function is longer than 50 lines. Etc etc.

The problem is that if at any give time, let's say 12:55pm, if I notice that an instance of the script is still running, I have no idea (i) when that happened, and (ii) at what precise point in the script we are stuck. If I knew where this script is hanging, I could fix it.
swagfag
Posts: 2519
Joined: 11 Jan 2017, 17:59

Re: Tool to diagnose why/where a long script is stuck?

09 Apr 2019, 11:38

its spaghetti. it can hang on blocking calls, file, resource locking etc. it could be a badly implemented onexit routine. it could be the admin thing like jeswg mentioned. the fact that u got no clue at all as to what might be causing it is more concerning to me. its spaghetti, bon apetit
freespacing
Posts: 97
Joined: 28 Sep 2016, 11:14
Contact:

Re: Tool to diagnose why/where a long script is stuck?

09 Apr 2019, 13:03

swagfag wrote:
09 Apr 2019, 11:38
the fact that u got no clue at all as to what might be causing it is more concerning to me. its spaghetti, bon apetit
Then I saw you wrote

Code: Select all

str := RegExReplace(str, "([0-9])\.([0-9])", "$1ZZZZ$2")
where I would have done

Code: Select all

str := RegExReplace(str, "[0-9]\K\.(?=[0-9])", "ZZZZ")
and I yawned.
User avatar
jeeswg
Posts: 6395
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Tool to diagnose why/where a long script is stuck?

09 Apr 2019, 13:05

- I think some of the editors have debugging functionality.
- You could also use ListLines.
- To clarify my earlier point, if you run a script as admin, and then try to run the same script again, but this time normally (not as admin), it won't be able to close the other script.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
freespacing
Posts: 97
Joined: 28 Sep 2016, 11:14
Contact:

Re: Tool to diagnose why/where a long script is stuck?

09 Apr 2019, 13:08

jeeswg wrote:
09 Apr 2019, 13:05
I think some of the editors have debugging functionality.
You could also use ListLines.
Thank you very much for those ideas, @jeeswg
I will look into them.
Interested in exploring anything that provides some introspecting ability.
iseahound
Posts: 408
Joined: 13 Aug 2016, 21:04
GitHub: iseahound

Re: Tool to diagnose why/where a long script is stuck?

09 Apr 2019, 15:10

Post code? We can't help you otherwise.

I suggest using ListLines to see the last line of executed code, and Tooltip % A_ThisFunc on every function. You could also make a debug log, use FileAppend, % A_ThisFunc at the beginning of every function.

Try double clicking the AHK icon and looking though the last key presses, etc.
swagfag
Posts: 2519
Joined: 11 Jan 2017, 17:59

Re: Tool to diagnose why/where a long script is stuck?

09 Apr 2019, 16:08

ohohohohoh /bow mr regexgodx lol
freespacing
Posts: 97
Joined: 28 Sep 2016, 11:14
Contact:

Re: Tool to diagnose why/where a long script is stuck?

10 Apr 2019, 10:37

@swagfag Thank you for your compliment, I appreciate it.

@Everyone Else Thank you for your helpful suggestions. An update is in order.

I've realized that I needed to take a step back to inform myself about debugging facilities in AHK, something I'd never studied.

This has been extremely interesting, but some of the information is unclear to me. For anyone else also starting with this topic I'll summarize my findings so far and relevant links, and also hope that some of you may be able to advise as to what I'm missing in the tools (sections in bold italic).

Debugging Tools

These are the three main things I've read about in the AHK debugging tools world:
  • 1. "Main Window". When an AHK script is running (the dot-ahk version, not a dot-exe compiled version) you can right-click the script's icon in the tray and select 'Open', which brings up the "Main Window". This is supposed to give you access to variables, last executed lines and so forth — which apparently are also available via the ListVar facility some of you mentioned. This sounds great, but the problem is that when I tried to use this on a hanging script, the script immediately terminated.

    2. Debugging within the Autohotkey Studio editor. This video demonstrates a very nice watch window within AHK studio. This looks fantastic, but in the menu (all the options are accessible via Alt + M) I haven't found a way to attach the debugger to a running script.

    3. Debugging via the SciTE4AutoHotkey editor. This thread presents an updated version of the editor with more debugging features. Under Tools, there is an extremely promising feature to "debug a currently running script". When I try this, I am able to select my script, but sadly SciTE then hangs with this message: Waiting for SciTE to connect... Nothing more.

    This is the status of my research into debugging facilities.

    Would be hugely grateful for any insights and input from this point forward.
freespacing
Posts: 97
Joined: 28 Sep 2016, 11:14
Contact:

Re: Tool to diagnose why/where a long script is stuck?

11 Apr 2019, 05:13

Just found one place where the script was sometimes hung: ClipWait

When restoring the clipboard, there were places where I had a ClipWait, but the clipboard was initially empty, so it just stayed there.

This is telling me to use something like ClipWait, 0.5 most of the time as a safety feature.

I imagine this is a common problem, and this makes me wonder: do some of you know threads that collect "common Gotchas" like that? I know some of you have put together some outstanding collections of bookmarked threads.
swagfag
Posts: 2519
Joined: 11 Jan 2017, 17:59

Re: Tool to diagnose why/where a long script is stuck?

11 Apr 2019, 05:16

doubt an infinite loop fits the definition of a common gotcha
User avatar
jeeswg
Posts: 6395
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Tool to diagnose why/where a long script is stuck?

17 Apr 2019, 12:14

Re. gotchas. Although there's not much content there.
The Unofficial Known Issues and Gotchas List - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=2047
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask For Help”

Who is online

Users browsing this forum: Albireo, Flipeador, gummby8, Odlanir, swagfag, vsub and 171 guests