XivMitmLatencyMitigator
XivMitmLatencyMitigator copied to clipboard
Cleanup script not running on exit when running the script as a systemd service
try:
at line 1588 breaks early because of https://github.com/Soreepeong/XivMitmLatencyMitigator/issues/67#issuecomment-1287817048 and not even finally:
will run for some reason
Maybe systemd is killing the process early so the script can't find it and it won't execute the rest of the code?
https://github.com/Soreepeong/XivMitmLatencyMitigator/blob/a32f36233077da9bcff51e9f6101c915fb954664/mitigate.py#L1588-L1663
While playing around with logging.info()
I noticed the while loop breaks at sock, source = listener.accept()
and everything below that and outside of the loop won't be executed. Even the finally:
at the very bottom won't be executed at all
https://github.com/Soreepeong/XivMitmLatencyMitigator/blob/a32f36233077da9bcff51e9f6101c915fb954664/mitigate.py#L1617-L1635
Why not just add the cleanup script to the ExecStopPost
option in your unit file? Might be worth the unit separating out the components too but that'll get weird.
That's what I did for now as a workaround but the problem yet remains
[Unit]
Description=XivAlexander script
[Service]
Type=simple
User=root
WorkingDirectory=/root/xivalexander
#ExecStart=/bin/bash run.sh
ExecStart=/usr/bin/python3 mitigate.py
ExecStop=-/bin/bash .cleanup.sh
ExecStop=-/bin/rm .cleanup.sh
[Install]
WantedBy=multi-user.target
Ah I follow now, should maybe just make cleanup callable as a param, or write the iptables rules to some file that can be consumed by cleanup independent of the in-memory ports and crap.
I keep the cleanup script around for manual use, so it works just fine in systemd (except I don't use systemd), mostly since using fixed ports. Could be worth it for you, just add a flag and set port
to it, env var it, or hardcode if you need. Probably a good PR to make port configurable.
Try this:
[Unit]
Description=XivAlexander script
[Service]
Type=simple
User=root
WorkingDirectory=/root
ExecStart=/bin/bash run.sh
Restart=on-failure
RestartSec=5
KillSignal=SIGINT
[Install]
WantedBy=multi-user.target
(Added KillSignal=SIGINT
line to [Service]
)
#!/bin/bash
curl https://raw.githubusercontent.com/Soreepeong/XivMitmLatencyMitigator/main/mitigate.py | exec python3
(Added exec
to python3
)
It makes it so that sigint (ctrl+c) is passed to the mitigate.py script