OpenAI client prevents process from gracefully terminating
Problem
Confirm this is a Node library issue and not an underlying OpenAI API issue - [x] This is an issue with the Node library Describe the bug When using OpenAI client to stream completions, the process fails to shutdown gracefully. To Reproduce Stream and attempt to gracefully shutdown. Active handles show instances of `agentkeepalive`, which is coming from OpenAI SDK. [code block] Code snippets [code block] OS macOS Node version 22 Library version 4.83.0
Error Output
error: '[Function: onerror]',
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Fix OpenAI Client Graceful Shutdown Issue
The OpenAI Node library uses an instance of `agentkeepalive` to manage HTTP connections for streaming completions. This instance can prevent the Node.js process from exiting gracefully if there are active connections when a shutdown is attempted. The library does not properly clean up these connections, leading to lingering handles that block the process termination.
Awaiting Verification
Be the first to verify this fix
- 1
Identify Active Handles
Use Node.js's built-in `process._getActiveRequests()` and `process._getActiveHandles()` to identify any active handles or requests that are preventing the process from exiting.
javascriptconsole.log(process._getActiveRequests()); console.log(process._getActiveHandles()); - 2
Implement Cleanup on Shutdown
Modify your application to listen for shutdown events and explicitly close the `agentkeepalive` instance used by the OpenAI client. This ensures that all connections are terminated before the process exits.
javascriptconst { OpenAI } = require('openai'); const agent = new AgentKeepAlive(); const openai = new OpenAI({ agent }); process.on('SIGINT', async () => { await agent.destroy(); // Ensure all connections are closed process.exit(); }); - 3
Test Graceful Shutdown
Run your application and trigger a shutdown (e.g., by pressing Ctrl+C). Verify that the process exits without any lingering active handles or requests.
javascriptconsole.log('Application is shutting down...'); - 4
Monitor for Errors
After implementing the fix, monitor the application logs for any errors related to the OpenAI client or unexpected behavior during shutdown. Ensure that the error text '[Function: onerror]' no longer appears.
javascriptconsole.error = (msg) => { if (!msg.includes('[Function: onerror]')) console.log(msg); };
Validation
Confirm that the application shuts down gracefully without any active handles remaining. Use `process._getActiveHandles()` after shutdown to ensure it returns an empty array. Additionally, check application logs for the absence of the error text related to the OpenAI client.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep