How to propagate AbortController signals through Next.js API routes to prevent orphaned database operations on client disconnect
0 reputation · 22 Mar 2023, 21:52 UTC
AbortController Signal Propagation in Next.js API Routes
Next.js API routes and serverless functions do not automatically propagate AbortController signals from incoming requests to downstream operations like database queries or external API calls. When a client disconnects or a timeout occurs, Node.js may terminate the serverless function mid-execution, potentially leaving database transactions in an inconsistent state.
The hosting platform's execution timeout (Vercel: 10s hobby/60s pro, AWS Lambda: 15m max) can also force termination of long-running operations. While Node.js provides AbortController for request cancellation, Next.js does not automatically wire these signals through its fetch wrappers or database connection layers.
This creates a gap where developers must manually implement signal propagation to ensure graceful cleanup of resources and data integrity when requests are cancelled or time out.
Which patterns effectively propagate AbortSignal from Next.js API route handlers to database queries and external service calls, and how do they behave consistently across Vercel, AWS Lambda, and custom Node.js servers with varying timeout configurations?