The fact that the keyboard also becomes unresponsive is an important detail. If Terminal and other apps are not accepting keyboard input, then killall Dock or killall Finder obviously would not help at that point because you can not enter the command.
If Terminal is still usable the next time it happens, I would try killall Dock first, since Stacks are part of the Dock. If Finder itself appears to be what is s hung, killall
Finder is another option. Neither is guaranteed to fix this particular freeze, but they are reasonable things to try before logging out.
If you can not use Terminal, try Command–Option–Escape. If the Force Quit window comes up and responds, select Finder and choose Relaunch. (Apple Support)
If that doesn't respond either and the Mac won't restart normally, Apple recommends holding the Touch ID/power button until the Mac shuts down. (Apple Support)
Since this started immediately after upgrading to macOS 27 and you've been able to reproduce it repeatedly, I'd make sure you're on the latest available macOS 27 update and add the keyboard behaviour to your existing Apple Feedback report. I'd specifically mention that the freeze starts while moving the pointer through a Stack and that, once it happens, keyboard input stops working in multiple apps, including Terminal.
Use the Product Feedback - and make it known to Apple regarding this ongoing issue
If it keeps happening, Safe Mode would be a reasonable next diagnostic step. On your M1 Pro, shut down the Mac, hold the power button until Loading startup options appears, select your startup disk, hold Shift, and choose Continue in Safe Mode. Apple says Safe Mode can help determine whether software that loads during startup is contributing to a problem. (Apple Support)
The main thing I'd try to establish during the next freeze is whether the Stack/Dock alone is stuck or the whole Mac has stopped responding. That will tell you whether restarting the Dock/Finder is worth pursuing or whether you're dealing with something broader.
Only as a diagnosis tool download the Application Etrecheck ( External Link ) directly from the Developer.
This is a Diagnostic Tool that makes no changes to the computer.
It makes a coherent and readable inventory of both the Hardware and Software used on the computer
The application is free or paid for added features.
The Report will Not Reveal Any Personal Information.
Post back the Full Report - Share Report >> Copy , then paste >>>> using the Additional Text Icon <<<<