I've been writing a simple shutdown utility in Rust to gracefully close all open apps before running a final command and exiting: https://github.com/dastarruer/gtkshutdown. This final command is provided by the user.
However, this final command just... doesn't run after the program finishes. Or, rather, sometimes it does, and sometimes it doesn't? I'd really like to finish this project and move on with my life, but I just can't figure out what the issue might be.
To test this behavior, you can run RUST_LOG=trace cargo run -- --post-cmd "touch /tmp/gtkshutdown", and then run ls /tmp/gtkshutdown to check that the post command ran. Logs are written to $XDG_STATE_HOME/gtkshutdown by default.
Edit: If you find a solution and would like to just submit it as a PR, you are more than welcome to do that instead.
I don't know enough about gtk, does the timer callback block the main event loop, or can it be run asynchronously (i.e. from a separate time keeping thread)?
I see you closed the main window before running the post command, maybe there's a race condition here, e.g. your app sometimes exited before the shell command is run?
try reorder these two steps:
This doesn't fix the issue sadly... I believe closing the window does not close the process; any code ordered after it will still execute.
another shot in the dark: when you closing open apps, do you filter out your own process id? could it be that your program kills itself before it had a chance to run the post command?
Yep, there’s a line somewhere to filter out any process matching gtkshutdown’s pid.
I see you forked the process at startup by default, does the same problem occur if you use --no-fork?
If you use --no-fork, then the terminal running the program will be killed; thus gtkshutdown will also terminate, which is not wanted behavior. Adding & disown is the same as omitting the --no-fork flag, so the same issue persists.