🔌 fix: Isolate Code-Server HTTP Agents to Prevent Socket Pool Contamination#12311
Merged
Conversation
Prevents socket hang up after 5s on Node 19+ when code executor has file attachments. follow-redirects (axios dep) leaks `socket.destroy` as a timeout listener on TCP sockets; with Node 19+ defaulting to keepAlive: true, tainted sockets re-enter the global pool and destroy active node-fetch requests in CodeExecutor after the idle timeout. Uses dedicated http/https agents with keepAlive: false for all axios calls targeting CODE_BASEURL in crud.js and process.js. Closes #12298
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses a Node.js (19+/23+) “socket hang up after 5s” failure in the code executor when file attachments trigger axios (follow-redirects) requests to the code-server, by isolating those axios requests from the default/global HTTP agent pool.
Changes:
- Introduces dedicated
http.Agent/https.Agentinstances withkeepAlive: falsefor code-server axios calls. - Wires these dedicated agents into axios requests in both the code output processing path and code file CRUD helpers.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| api/server/services/Files/Code/process.js | Adds dedicated HTTP(S) agents and applies them to axios calls used during code output processing/session info retrieval. |
| api/server/services/Files/Code/crud.js | Adds dedicated HTTP(S) agents and applies them to axios calls for code output download streams and file uploads. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Move duplicated agent construction from crud.js and process.js into
a shared agents.js module to eliminate DRY violation
- Switch process.js from raw `require('axios')` to `createAxiosInstance()`
for proxy configuration parity with crud.js
- Fix import ordering in process.js (agent constants no longer split imports)
- Add 120s timeout to uploadCodeEnvFile (was the only code-server call
without a timeout)
51a9d6b to
1bce032
Compare
- Add crud.spec.js covering getCodeOutputDownloadStream and uploadCodeEnvFile (agent options, timeout, URL, error handling) - Add socket pool isolation tests to process.spec.js asserting keepAlive:false agents are forwarded to axios - Update process.spec.js mocks for createAxiosInstance() migration
1bce032 to
93e8b70
Compare
Relocate agents.js from api/server/services/Files/Code/ to packages/api/src/utils/code.ts per workspace conventions. Consumers now import codeServerHttpAgent/codeServerHttpsAgent from @librechat/api.
807515c to
899c1d2
Compare
mstlaur1
pushed a commit
to mstlaur1/LibreChat
that referenced
this pull request
Mar 25, 2026
…nation (danny-avila#12311) * 🔧 fix: Isolate HTTP agents for code-server axios requests Prevents socket hang up after 5s on Node 19+ when code executor has file attachments. follow-redirects (axios dep) leaks `socket.destroy` as a timeout listener on TCP sockets; with Node 19+ defaulting to keepAlive: true, tainted sockets re-enter the global pool and destroy active node-fetch requests in CodeExecutor after the idle timeout. Uses dedicated http/https agents with keepAlive: false for all axios calls targeting CODE_BASEURL in crud.js and process.js. Closes danny-avila#12298 * ♻️ refactor: Extract code-server HTTP agents to shared module - Move duplicated agent construction from crud.js and process.js into a shared agents.js module to eliminate DRY violation - Switch process.js from raw `require('axios')` to `createAxiosInstance()` for proxy configuration parity with crud.js - Fix import ordering in process.js (agent constants no longer split imports) - Add 120s timeout to uploadCodeEnvFile (was the only code-server call without a timeout) * ✅ test: Add regression tests for code-server socket isolation - Add crud.spec.js covering getCodeOutputDownloadStream and uploadCodeEnvFile (agent options, timeout, URL, error handling) - Add socket pool isolation tests to process.spec.js asserting keepAlive:false agents are forwarded to axios - Update process.spec.js mocks for createAxiosInstance() migration * ♻️ refactor: Move code-server agents to packages/api Relocate agents.js from api/server/services/Files/Code/ to packages/api/src/utils/code.ts per workspace conventions. Consumers now import codeServerHttpAgent/codeServerHttpsAgent from @librechat/api.
jcbartle
pushed a commit
to jcbartle/LibreChat
that referenced
this pull request
May 11, 2026
…nation (danny-avila#12311) * 🔧 fix: Isolate HTTP agents for code-server axios requests Prevents socket hang up after 5s on Node 19+ when code executor has file attachments. follow-redirects (axios dep) leaks `socket.destroy` as a timeout listener on TCP sockets; with Node 19+ defaulting to keepAlive: true, tainted sockets re-enter the global pool and destroy active node-fetch requests in CodeExecutor after the idle timeout. Uses dedicated http/https agents with keepAlive: false for all axios calls targeting CODE_BASEURL in crud.js and process.js. Closes danny-avila#12298 * ♻️ refactor: Extract code-server HTTP agents to shared module - Move duplicated agent construction from crud.js and process.js into a shared agents.js module to eliminate DRY violation - Switch process.js from raw `require('axios')` to `createAxiosInstance()` for proxy configuration parity with crud.js - Fix import ordering in process.js (agent constants no longer split imports) - Add 120s timeout to uploadCodeEnvFile (was the only code-server call without a timeout) * ✅ test: Add regression tests for code-server socket isolation - Add crud.spec.js covering getCodeOutputDownloadStream and uploadCodeEnvFile (agent options, timeout, URL, error handling) - Add socket pool isolation tests to process.spec.js asserting keepAlive:false agents are forwarded to axios - Update process.spec.js mocks for createAxiosInstance() migration * ♻️ refactor: Move code-server agents to packages/api Relocate agents.js from api/server/services/Files/Code/ to packages/api/src/utils/code.ts per workspace conventions. Consumers now import codeServerHttpAgent/codeServerHttpsAgent from @librechat/api.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a "socket hang up" after ~5 seconds on Node 19+ when the code executor processes file attachments, and hardens the surrounding infrastructure with a shared agent module, proxy parity, a missing upload timeout, and regression test coverage.
http.Agent/https.Agentinstances withkeepAlive: falsefor all four axios call sites targetingCODE_BASEURL(getCodeOutputDownloadStream,uploadCodeEnvFile,processCodeOutput,getSessionInfo), preventing tainted sockets from re-entering the global pool and destroying activenode-fetchrequests inCodeExecutor.agents.jsmodule, eliminating the DRY violation that would have existed acrosscrud.jsandprocess.js.process.jsfrom a rawrequire('axios')call tocreateAxiosInstance()from@librechat/api, aligning proxy configuration behavior withcrud.js.120stimeout touploadCodeEnvFile, which was the only code-server axios call without one.crud.spec.jscoveringgetCodeOutputDownloadStreamanduploadCodeEnvFilewith agent option assertions, timeout verification, URL correctness, success/error paths, andentity_idquery parameter behavior.process.spec.jswith asocket pool isolationdescribe block that asserts the exact agent instances, their types, andkeepAlive: falseare forwarded to axios forprocessCodeOutput.process-traversal.spec.jsand existingprocess.spec.jsmocks to reflect thecreateAxiosInstance()migration.Change Type
Testing
Ran the existing and new unit tests from the
apiworkspace:Covers:
crud.spec.js— agent isolation,keepAlive: false, timeout, URL,entity_idparam, success/error paths for both CRUD functionsprocess.spec.js— full existing suite (atomic claim, image/non-image processing, size limits, fallback behavior, usage counters, metadata) plus the newsocket pool isolationblock asserting agents are forwarded correctly__tests__/process-traversal.spec.js— path traversal protection, unaffected by the changesTo reproduce the underlying bug manually: run LibreChat on Node 20+ with a code executor configured, attach a file to a conversation, and trigger code execution. Without this fix, the download or session-info request will hang up after ~5 seconds. With it, requests complete cleanly.
Test Configuration:
CODE_BASEURL) configured and reachableChecklist
Closes #12298