Gcode_shell_command timing

I’m currently using gcode_shell_command and RUN_SHELL_COMMAND to execute a shell command from the gcode. However, this shell command is executed way, way before commands that come before it in the gcode file are processed by the mcu. It seems like the shell command is executed after the preceding gcode is sent to the mcu’s input queue, rather than when that gcode is (expected to be) actually processed on the mcu.

Is there some way to synchronize these somehow?

Or perhaps approximated within a second or so?

I also noticed that Klipper supports M400 which waits for current moves to finish. Could that perhaps be used to empty the mcu’s queue, or in some other way synchronize the RUN_SHELL_COMMAND execution? (I wouldn’t mind if the printer would just stop for a second while the command is executed.)