Try booting into Safe Mode to see if you have the same issue when shutting down. If the issue is not present when booted into Safe Mode, then maybe you have an issue with some third party software. Make sure all of your third party software is up to date and compatible with Sonoma.
Disconnect all external devices in case one of them is causing a problem.
Try running the Apple Diagnostics to see if any hardware issues are detected.
If this is an Intel Mac, then you can try an SMC Reset and a PRAM Reset (hold the PRAM Reset for two chimes if possible....not always possible). These two resets do not apply if you are using an Apple Silicon Mac.
I don't know if this will do any good, but you could also try a DFU firmware "Revive" which will reset the system firmware. A "Revive" is not supposed to harm any data, but you should first make sure to have a good backup just to be safe.