@@ -220,15 +220,49 @@ async def startup(self, sockets=None):
220
220
if self .should_exit :
221
221
return
222
222
223
+ # Custom implementation that doesn't rely on config.server_class
223
224
if sockets is not None :
224
225
self .servers = []
225
226
for socket in sockets :
226
- server = await self .config .server_class (config = self .config , server = self )
227
+ # Use TCPServer directly instead of relying on config.server_class
228
+ try :
229
+ # Try the newer location first
230
+ from uvicorn .server import TCPServer
231
+ server = TCPServer (config = self .config )
232
+ except (ImportError , AttributeError ):
233
+ try :
234
+ # Try the older location
235
+ from uvicorn .protocols .http .h11_impl import TCPServer
236
+ server = TCPServer (config = self .config )
237
+ except (ImportError , AttributeError ):
238
+ # Last resort - use a simple server implementation
239
+ logger .warning ("Could not import TCPServer - using simple server implementation" )
240
+ from uvicorn .protocols .http .auto import AutoHTTPProtocol
241
+ server = uvicorn .Server (config = self .config )
242
+
243
+ server .server = self # Set the server reference
227
244
await server .start ()
228
245
self .servers .append (server )
229
246
else :
230
- self .servers = [await self .config .server_class (config = self .config , server = self )]
231
- await self .servers [0 ].start ()
247
+ # Use TCPServer directly instead of relying on config.server_class
248
+ try :
249
+ # Try the newer location first
250
+ from uvicorn .server import TCPServer
251
+ server = TCPServer (config = self .config )
252
+ except (ImportError , AttributeError ):
253
+ try :
254
+ # Try the older location
255
+ from uvicorn .protocols .http .h11_impl import TCPServer
256
+ server = TCPServer (config = self .config )
257
+ except (ImportError , AttributeError ):
258
+ # Last resort - use a simple server implementation
259
+ logger .warning ("Could not import TCPServer - using simple server implementation" )
260
+ from uvicorn .protocols .http .auto import AutoHTTPProtocol
261
+ server = uvicorn .Server (config = self .config )
262
+
263
+ server .server = self # Set the server reference
264
+ await server .start ()
265
+ self .servers = [server ]
232
266
233
267
if self .lifespan is not None :
234
268
try :
@@ -240,6 +274,18 @@ async def startup(self, sockets=None):
240
274
self .lifespan = NoopLifespan (self .config .app )
241
275
logger .warning ("Replaced failed lifespan with NoopLifespan" )
242
276
277
+ async def main_loop (self ):
278
+ """Custom main loop implementation with error handling."""
279
+ try :
280
+ # Use asyncio.sleep to keep the server running
281
+ while not self .should_exit :
282
+ await asyncio .sleep (0.1 )
283
+ except Exception as e :
284
+ logger .error (f"Error in main loop: { str (e )} " )
285
+ logger .debug (f"Main loop error details: { traceback .format_exc ()} " )
286
+ # Set should_exit to True to initiate shutdown
287
+ self .should_exit = True
288
+
243
289
async def shutdown (self , sockets = None ):
244
290
"""Override the shutdown method to add error handling for lifespan shutdown."""
245
291
if self .servers :
0 commit comments