jens.luecke commited on
Commit
0f27fd6
·
1 Parent(s): 36e65eb

Fix 502 error on reload by improving preview app port management - added port availability checking and proper process termination with retry logic

Browse files
Files changed (1) hide show
  1. app.py +29 -1
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import os
 
2
  import subprocess
3
  import time
4
  from pathlib import Path
@@ -23,6 +24,17 @@ def get_preview_url():
23
  PREVIEW_URL = get_preview_url()
24
 
25
 
 
 
 
 
 
 
 
 
 
 
 
26
  def find_app_py_in_sandbox():
27
  """Find app.py file in sandbox folder and its subfolders."""
28
  sandbox_path = Path("sandbox")
@@ -65,7 +77,12 @@ def stop_preview_app():
65
  print("✅ Preview app stopped gracefully.")
66
  except subprocess.TimeoutExpired:
67
  preview_process.kill()
68
- print("⚠️ Preview app force-killed after timeout.")
 
 
 
 
 
69
  except Exception as e:
70
  print(f"❌ Error stopping preview app: {e}")
71
  finally:
@@ -84,6 +101,17 @@ def start_preview_app():
84
  # Stop any existing process before starting a new one
85
  stop_preview_app()
86
 
 
 
 
 
 
 
 
 
 
 
 
87
  app_file = find_app_py_in_sandbox()
88
  if not app_file:
89
  return False, "No `app.py` found in the `sandbox` directory."
 
1
  import os
2
+ import socket
3
  import subprocess
4
  import time
5
  from pathlib import Path
 
24
  PREVIEW_URL = get_preview_url()
25
 
26
 
27
+ def is_port_available(port, host="0.0.0.0"):
28
+ """Check if a port is available for binding."""
29
+ try:
30
+ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
31
+ sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
32
+ sock.bind((host, port))
33
+ return True
34
+ except OSError:
35
+ return False
36
+
37
+
38
  def find_app_py_in_sandbox():
39
  """Find app.py file in sandbox folder and its subfolders."""
40
  sandbox_path = Path("sandbox")
 
77
  print("✅ Preview app stopped gracefully.")
78
  except subprocess.TimeoutExpired:
79
  preview_process.kill()
80
+ # Wait a bit longer for the kill to take effect
81
+ try:
82
+ preview_process.wait(timeout=2)
83
+ print("⚠️ Preview app force-killed after timeout.")
84
+ except subprocess.TimeoutExpired:
85
+ print("⚠️ Preview app may still be running after force-kill attempt.")
86
  except Exception as e:
87
  print(f"❌ Error stopping preview app: {e}")
88
  finally:
 
101
  # Stop any existing process before starting a new one
102
  stop_preview_app()
103
 
104
+ # Wait for the port to become available (up to 5 seconds)
105
+ for i in range(10): # 10 attempts * 0.5 seconds = 5 seconds max
106
+ if is_port_available(PREVIEW_PORT):
107
+ print(f"✅ Port {PREVIEW_PORT} is available")
108
+ break
109
+ print(f"⏳ Port {PREVIEW_PORT} still busy, waiting... (attempt {i+1}/10)")
110
+ time.sleep(0.5)
111
+ else:
112
+ print(f"❌ Port {PREVIEW_PORT} is still not available after 5 seconds")
113
+ return False, f"Port {PREVIEW_PORT} is not available"
114
+
115
  app_file = find_app_py_in_sandbox()
116
  if not app_file:
117
  return False, "No `app.py` found in the `sandbox` directory."