# 未处理的异常：IsolateSpawnException：无法发送消息：SendPort 已关闭

- **ID:** `flutter/isolate-send-port-closed`
- **领域:** flutter
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

尝试通过已关闭的 SendPort 发送消息，通常是因为接收隔离已终止或端口已被释放。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Flutter 3.13.0 | active | — | — |
| Dart 3.1.0 | active | — | — |

## 解决方案

1. ```
   Check if the receiving isolate is still alive before sending. Use a shared flag or a ping mechanism: if (receivePort.isClosed) { await spawnIsolate(); } then send.
   ```
2. ```
   Implement a reconnection strategy: when the error occurs, close the old SendPort, spawn a new isolate, and recreate the port pair. Example: isolate = await Isolate.spawn(entryPoint, receivePort.sendPort); sendPort = isolate.controlPort;
   ```
3. ```
   Use a dedicated communication manager that monitors isolate lifecycle and automatically re-establishes ports on termination.
   ```

## 无效尝试

- **Increase the size of the isolate's message queue.** — The error is not about queue capacity but about the port being closed; queue size does not affect closed ports. (95% 失败率)
- **Wrap the send call in a try-catch and ignore the error.** — Ignoring the error leads to silent data loss; the root cause (port closure) remains unaddressed. (80% 失败率)
