-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_immediate_shutdown.py
More file actions
126 lines (106 loc) · 4.39 KB
/
test_immediate_shutdown.py
File metadata and controls
126 lines (106 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python3
"""
Test immediate shutdown behavior (no graceful cleanup)
"""
import subprocess
import time
import signal
import os
import sys
def test_immediate_shutdown():
"""Test that server shuts down IMMEDIATELY when Ctrl+C is pressed"""
print("💀 Testing IMMEDIATE Shutdown (No Graceful Cleanup)")
print("=" * 60)
print("🚀 Starting server in background...")
# Start server process
try:
server_process = subprocess.Popen(
[sys.executable, "openrouter_server.py"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
print(f"📊 Server PID: {server_process.pid}")
# Wait a bit for server to start
print("⏳ Waiting 3 seconds for server to initialize...")
time.sleep(3)
# Check if server is running
if server_process.poll() is None:
print("✅ Server is running")
else:
print("❌ Server failed to start")
stdout, stderr = server_process.communicate()
print(f"STDOUT: {stdout}")
print(f"STDERR: {stderr}")
return False
# Send SIGINT (Ctrl+C equivalent)
print("💀 Sending SIGINT (Ctrl+C) for IMMEDIATE shutdown...")
shutdown_start = time.time()
server_process.send_signal(signal.SIGINT)
# Wait for process to terminate with very short timeout
try:
server_process.wait(timeout=3) # Only 3 seconds max
shutdown_time = time.time() - shutdown_start
print(f"✅ Server shut down IMMEDIATELY in {shutdown_time:.2f} seconds")
if shutdown_time < 1:
print("🎉 PERFECT: Instant shutdown (< 1 second)")
return True
elif shutdown_time < 3:
print("👍 GOOD: Very fast shutdown (< 3 seconds)")
return True
else:
print("⚠️ ACCEPTABLE: Quick shutdown (< 3 seconds)")
return True
except subprocess.TimeoutExpired:
print("❌ FAILURE: Server did not shut down within 3 seconds")
print("💀 This means it's still trying graceful cleanup")
print("🔨 Force killing server...")
server_process.kill()
server_process.wait()
return False
except Exception as e:
print(f"❌ Error testing immediate shutdown: {e}")
return False
def show_usage_instructions():
"""Show how to use the immediate shutdown"""
print("\n💀 IMMEDIATE SHUTDOWN Instructions")
print("=" * 50)
print("The server now uses IMMEDIATE shutdown (no graceful cleanup)")
print()
print("✅ BENEFITS:")
print(" - Ctrl+C responds instantly (< 1 second)")
print(" - No hanging on web scraping operations")
print(" - No waiting for Chrome to close gracefully")
print(" - Process exits immediately")
print()
print("⚠️ TRADE-OFFS:")
print(" - Chrome processes may remain open")
print(" - No cleanup of temporary files")
print(" - Connections may not close gracefully")
print()
print("🔧 MANUAL CLEANUP (if needed):")
print(" - Close Chrome manually if it stays open")
print(" - Kill any remaining Chrome processes:")
print(" taskkill /f /im chrome.exe")
print()
print("💡 USAGE:")
print(" 1. Start server: uv run python openrouter_server.py")
print(" 2. Press Ctrl+C anytime - exits IMMEDIATELY")
print(" 3. No waiting, no hanging, just instant exit")
def main():
print("💀 IMMEDIATE Shutdown Test")
print("This tests that Ctrl+C exits instantly with no graceful cleanup")
print("=" * 70)
# Automated test
print("🤖 Running immediate shutdown test...")
success = test_immediate_shutdown()
if success:
print("\n🎉 SUCCESS: Server shuts down IMMEDIATELY!")
print("✅ Ctrl+C now works instantly - no more hanging!")
else:
print("\n❌ Issues detected with immediate shutdown")
print("💡 The server may still be trying graceful cleanup")
# Usage instructions
show_usage_instructions()
if __name__ == "__main__":
main()