MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / check_server_alive

Function check_server_alive

tests/security/conftest.py:127–181  ·  view source on GitHub ↗

Check if server is still alive after a test. Use this fixture to verify the server didn't crash. IMPORTANT: This checks if server is RESPONSIVE, not just if connection was closed. Defensive connection closures are GOOD security behavior, not crashes!

(api_server_required)

Source from the content-addressed store, hash-verified

125
126@pytest.fixture
127def check_server_alive(api_server_required):
128 """
129 Check if server is still alive after a test.
130
131 Use this fixture to verify the server didn't crash.
132
133 IMPORTANT: This checks if server is RESPONSIVE, not just if connection
134 was closed. Defensive connection closures are GOOD security behavior,
135 not crashes!
136 """
137
138 def _check(wait_time=1.0):
139 """
140 Try to establish a NEW connection and execute a simple command.
141
142 Args:
143 wait_time: Time to wait before checking (allows server to recover)
144
145 Returns:
146 True if server is alive and responsive
147 False if server crashed or is unresponsive
148 """
149 # Wait for server to recover from rate limiting / stress
150 time.sleep(wait_time)
151
152 # Try up to 3 times with increasing delays
153 for attempt in range(3):
154 try:
155 client = SerialStudioClient(timeout=5.0)
156 client.connect()
157
158 # Execute simple command
159 result = client.command("api.getCommands")
160
161 client.disconnect()
162
163 # Verify we got a valid response
164 if isinstance(result, dict) or isinstance(result, list):
165 return True
166
167 except ConnectionRefusedError:
168 # Server not accepting connections - likely crashed
169 if attempt == 2: # Last attempt
170 return False
171 time.sleep(2.0 * (attempt + 1)) # Exponential backoff
172
173 except (ConnectionError, TimeoutError, Exception) as e:
174 # Other errors might be rate limiting or temp issues
175 if attempt == 2: # Last attempt
176 return False
177 time.sleep(2.0 * (attempt + 1)) # Exponential backoff
178
179 return False
180
181 return _check
182
183
184@pytest.fixture(autouse=True)

Calls

no outgoing calls

Tested by

no test coverage detected