Skip to content

Commit 1e9a8e2

Browse files
committed
Fix proxy auto-restarting after intentional stop (#49)
When stopping via mtproxymax stop or TUI, the container would restart because Docker's --restart=unless-stopped policy and the Telegram bot's auto-recovery both tried to bring it back. Fix: - Set Docker restart policy to 'no' before stopping - Create /tmp/.mtproxymax_stopped flag on intentional stop - Bot health check and auto_recover() skip recovery if flag exists - Flag is cleared on next mtproxymax start/restart
1 parent 3d2dad0 commit 1e9a8e2

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

mtproxymax.sh

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2734,8 +2734,12 @@ stop_proxy_container() {
27342734
if is_proxy_running; then
27352735
# Flush traffic counters to disk before stopping
27362736
flush_traffic_to_disk 2>/dev/null
2737+
# Prevent Docker from auto-restarting the container
2738+
docker update --restart=no "$CONTAINER_NAME" &>/dev/null || true
27372739
if docker stop --timeout 10 "$CONTAINER_NAME" 2>/dev/null; then
27382740
traffic_tracking_teardown
2741+
# Signal intentional stop — prevents bot auto-recovery from restarting
2742+
echo "$(date +%s)" > /tmp/.mtproxymax_stopped 2>/dev/null || true
27392743
log_success "Proxy stopped"
27402744
else
27412745
log_error "Failed to stop proxy"
@@ -2778,6 +2782,9 @@ _start_all_instances() {
27782782
}
27792783

27802784
start_proxy_container() {
2785+
# Clear intentional-stop flag
2786+
rm -f /tmp/.mtproxymax_stopped 2>/dev/null
2787+
27812788
if is_proxy_running; then
27822789
log_info "Proxy is already running"
27832790
return 0
@@ -3226,7 +3233,7 @@ health_check() {
32263233
}
32273234

32283235
auto_recover() {
3229-
if ! is_proxy_running; then
3236+
if ! is_proxy_running && [ ! -f /tmp/.mtproxymax_stopped ]; then
32303237
log_warn "Proxy is down, attempting auto-recovery..."
32313238
start_proxy_container
32323239
fi
@@ -4327,7 +4334,7 @@ while true; do
43274334
# Health check every 5 minutes
43284335
if [ $((_now - _last_health)) -ge 300 ]; then
43294336
_last_health=$_now
4330-
if [ "$TELEGRAM_ALERTS_ENABLED" = "true" ] && ! is_running; then
4337+
if [ "$TELEGRAM_ALERTS_ENABLED" = "true" ] && ! is_running && [ ! -f /tmp/.mtproxymax_stopped ]; then
43314338
tg_send "🔴 *Alert*: Proxy is down! Attempting auto-restart..."
43324339
"${INSTALL_DIR}/mtproxymax" start &>/dev/null
43334340
sleep 5

0 commit comments

Comments
 (0)