Connection Phases
An ACP connection goes through these phases:- Creation - Connection object is instantiated with a stream
- Active - Messages can be sent and received
- Closing - Stream is ending (readable stream closed)
- Closed - Connection is terminated, no more messages possible
Creating a Connection
Connections are created by passing a stream and handler function:The handler function (
toAgent or toClient) receives the connection object and should return the Agent or Client implementation.Monitoring Connection State
The SDK provides two mechanisms for monitoring connection state:Using signal (AbortSignal)
The signal property provides an AbortSignal that aborts when the connection closes:
- Synchronous status check with
.aborted - Can be passed to other APIs (fetch, setTimeout)
- Standard Web API for cancellation
Using closed (Promise)
The closed property provides a Promise that resolves when the connection closes:
- Natural async/await syntax
- Can be combined with
Promise.race()for timeouts
Connection Closure
Connections close when the underlying readable stream ends. This can happen:- Normally - The other side closes their output stream
- Due to error - A stream error or write failure occurs
- Process termination - The subprocess exits
Detecting Closure
Both sides should monitor for connection closure:Resource Management
Proper resource management is critical for long-running connections.Cleaning Up on Close
Always clean up resources when the connection closes:Using Signals for Cancellation
Pass the connection’s signal to operations that should be cancelled on disconnect:Graceful Shutdown
For clean shutdown, ensure all pending operations complete:Handling Stream Errors
Stream errors trigger connection closure:Example: Complete Lifecycle Management
Here’s a complete example showing proper lifecycle management:Best Practices
Related
- Streams - Understanding the underlying stream API
- Error Handling - Handling connection errors