Quick Macros randomly repeating hundreds of times

I use Quick Macros all day, every day – most often for Next Tab/Previous Tab shortcuts (mapping to ⌘[ and ⌘]) – but sometimes for more involved tedious tasks.

Generally they're awesome but maybe 1 in 20 executions, the macro will get stuck in an endless loop. If it's a simple macro like the Next Tab shortcut, it's a mere annoyance, but sometimes it results in losing work or a data-destructive situation.

From the most recent instance, here's the Engine.log:

2019-08-21 17:27:23 Execute macro “Quick Macro for F3” from trigger The Hot Key ⌥F3 is pressed
2019-08-21 17:27:47 Running application query took a while (7474 us)
2019-08-21 17:27:47 Running application query took a while (7409 us)
2019-08-21 17:27:47 Running application query took a while (5174 us)
… [53 more lines like the above, with different IDs] ...
2019-08-21 17:29:52 GetProcessForPID(23152) failed with -600 for <KBMRunningApplication “Safari” <0x600001fd2910> pid=23152 bid=com.apple.Safari abht>, using cached version
2019-08-21 17:29:54 Running application query took a while (7040 us)
2019-08-21 17:29:55 More than 50 simultaneous macros - aborting everything
2019-08-21 17:29:56 Running application query took a while (3923 us)
2019-08-21 17:29:56 Running application query took a while (7495 us)
2019-08-21 17:29:56 Running application query took a while (3638 us)
… [9 more lines like the above, with different IDs] ...
2019-08-21 17:30:00 Execute macro “Check PT every hour” from trigger Every 30 minutes between 9:00-18:00 Monday-Friday
2019-08-21 17:30:00 More than 50 simultaneous macros - aborting everything
2019-08-21 17:30:02 Running application query took a while (4564 us)
2019-08-21 17:30:02 Running application query took a while (3465 us)
2019-08-21 17:30:02 Running application query took a while (3970 us)
… [56 more lines like the above, with different IDs] ...
2019-08-21 17:34:03 More than 50 simultaneous macros - aborting everything
2019-08-21 17:34:03 Running application query took a while (4634 us)
2019-08-21 17:34:06 Running application query took a while (11155 us)
2019-08-21 17:34:09 Running application query took a while (12436 us)
… [84 more lines like the above, with different IDs] ...
2019-08-21 17:35:12 Execute macro “Launchbar ALSO ON OPTION SPACE” from trigger The Hot Key ⌘Space is pressed  //At this point I've failed to kill the macro using normal means so I'm trying to get to Activity Monitor to kill KM
2019-08-21 17:35:13 Running application query took a while (18091 us)
2019-08-21 17:35:13 Running application query took a while (28752 us)
2019-08-21 17:35:13 Running application query took a while (6634 us)
2019-08-21 17:35:13 Running application query took a while (3792 us)
2019-08-21 17:35:13 Execute macro “Launchbar ALSO ON OPTION SPACE” from trigger The Hot Key ⌘Space is pressed
2019-08-21 17:35:15 Running application query took a while (6034 us)
2019-08-21 17:35:15 Running application query took a while (7928 us)
2019-08-21 17:35:17 Running application query took a while (10417 us)
… [69 more lines like the above, with different IDs] ...
…eventually I was able to kill KM from Activity Monitor…
2019-08-21 17:36:40 Engine Starting 9.0

The Running application query took a while log happens whenever I trigger my "Previous application" macro, which was part of the Quick Macro, so in a sense it provides nice context demonstrating the looping behavior, which might not have otherwise been logged.

Any idea what's going on here? And how I can safely trigger Quick Macros in the future without this loop?

Since I'm just an average user, my advice will be fairly lame.

I have caused accidental recursion myself when trying to remap keys, because I didn't realize that sending the new key could and would trigger the other key mapping macro. There is a way to turn a macro off before sending out a key.

But it's really hard to tell if the problem is recursion here without seeing the code. I'm not sure I'm smart enough to read that code anyway.

I'm puzzled why you said in the log file that you had to kill KM (you probably meant the KM Engine) to stop the macro. You should be able to just deactivate the trigger or the macro or the macro group. What is the trigger that is activating constantly?

I have mapped a key on my keyboard to the "Cancel All Macros" action so that I can stop all macros if I'm in some sort of infinite loop that's interfering with the mouse. I suppose you could do the same thing and also in that macro include an action that terminates the KM Engine. In your case that might be a good idea to reduce your suffering until the problem is solved.

No fancy code here – it's just a quick macro playing back a series of keystrokes to copy a value from one app and search in another:

[tab]
` (this key triggers a macro that switches to the last app)
⌘0
⌘C
`
⌘F
⌘V

It is very hard to understand what is going on, but basically you should pretty much never see “More than 50 simultaneous macros - aborting everything”, so why is that happening>

The “Running application” log has nothing to do with any of this - it is just the system’s largely crap API for dealing with running applications (how on earth can it take any modern CPU 7ms to respond to a simply query about a running application?)

What is the Check PT every hour macro doing that results in 50 simultaneous macros?

The Quick Macro action does not actually log when you trigger it (which is odd, I should fix that), so it is hard to see when it is being triggered.

But I don't understand how it would be “stuck in an endless loop” unless somehow you were re-triggering it by doing whatever you were doing in the quick macro and simulating the key required to trigger it (although Keyboard Maestro normally defends against that).

Hi Peter, thanks for your reply.

you should pretty much never see “More than 50 simultaneous macros - aborting everything”, so why is that happening>

That's what I'm trying to figure out :slight_smile:

The “Running application” log has nothing to do with any of this - it is just the system’s largely crap API for dealing with running applications (how on earth can it take any modern CPU 7ms to respond to a simply query about a running application?)

Yep. I'm not worried about the content of this log. It happens whenever I trigger my "last application" macro. And since my Last Application macro was triggered by the Quick Macro, this provides proof that the Quick Macro is executing repeatedly.

What is the Check PT every hour macro doing that results in 50 simultaneous macros?

Irrelevant; the Check PT macro just happened to run during the course of this situation. (The 50 simultaneous macros error happened 3 times, both before and after the Check PT macro)

But I don't understand how it would be “stuck in an endless loop” unless somehow you were re-triggering it by doing whatever you were doing in the quick macro and simulating the key required to trigger it (although Keyboard Maestro normally defends against that).

No, the quick macro is definitely not simulating the keystroke to trigger itself. I know this because ~19 out of 20 times, the quick macro works as expected. But on occasion, it goes into this manic repeat cycle. (NOTE: I realize "infinite loop" is an inaccurate description of this issue so I renamed the topic)

One possibility that wouldn't implicate Keyboard Maestro is if my keyboard itself is somehow getting stuck and firing F3 repeatedly. I'm not sure how I would test for this, though.

I have added logging of when a quick macro is triggered. I'm not sure what that will tell us, but is should at least confirm whether it is being triggered more than once.

A question would be whether you have Enable Key Repeat enabled in the action. If the key is stuck (or held) down (or the key up event is lost somehow), then that could cause the recorded sequence to repeat. And it will eventually timeout because the "is down" trigger does have a limitation on how long it will repeat for for exactly the case where the key is/appears “stuck down”.

Interesting. I didn't know what that option did, but yes, Enable Key Repeat was enabled. I have disabled it and will report back if the issue reoccurs.