Fixes#21792
## Problem
When using SNI (multiple TLS certificates with serverName), Bun would crash with a segmentation fault when:
1. Creating a server with multiple TLS certificates
2. Stopping the server
3. Creating a new server (with single or multiple certs)
4. Making requests to the new server
The crash occurred because route handlers registered for SNI domains persisted in uWS even after the server was destroyed. When the new server reused the same domain names, requests would trigger the old route handlers which had dangling pointers to the freed server.
## Root Cause
When setting up SNI, Bun calls:
- `app.addServerNameWithOptions(servername, ssl_opts)` - adds SSL context
- `app.domain(servername)` - switches to that domain's router
- `setRoutes()` - registers routes with UserRoute pointers
The UserRoute structures contain a pointer to the server (`server: *ThisServer`). When the server is destroyed, these pointers become dangling, but uWS still has them registered in the per-domain routers.
## Solution
Before destroying the app in `deinit()`:
1. Switch to each SNI domain using `app.domain()`
2. Clear routes for that domain using `app.clearRoutes()`
3. Remove the SNI server name using `app.removeServerName()`
4. Clear routes for the default domain
5. Then destroy the app
This ensures all route handlers are cleared and SNI contexts are properly removed before the server is freed.
## Testing
Verified the fix resolves the crash with:
- Stop/restart servers with SNI configurations
- Switching between single and dual certificate configurations
- Rapid switching between different TLS configurations
- Complex object patterns similar to Elysia's setup
🤖 Generated with Claude Code
Co-Authored-By: Claude <noreply@anthropic.com>