Files
bun.sh/src
Claude Bot 0671c086b3 Fix SNI crash caused by dangling route pointers
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>
2025-10-22 20:52:23 +00:00
..
2025-10-21 19:22:55 -07:00
2025-09-09 20:41:10 -07:00
2025-10-16 21:52:22 -04:00
2025-07-21 13:26:47 -07:00
2025-10-04 02:17:55 -07:00
2025-07-29 19:35:46 -07:00
2025-09-30 05:26:32 -07:00
2025-07-21 13:26:47 -07:00
2025-07-21 13:26:47 -07:00
2025-07-21 13:26:47 -07:00
2025-09-09 20:41:10 -07:00
2025-10-06 05:39:22 -07:00
2025-09-11 23:29:53 -07:00
2025-07-21 13:26:47 -07:00
2025-10-20 20:52:35 -07:00
2025-10-03 17:13:22 -07:00
2025-07-21 13:26:47 -07:00
2025-09-09 20:41:10 -07:00
2025-07-21 13:26:47 -07:00
2025-10-11 08:23:25 -07:00
2025-10-21 19:22:55 -07:00
2025-07-21 13:26:47 -07:00
2025-07-21 13:26:47 -07:00
2025-07-21 13:26:47 -07:00