Skip to content

fix(ai): open a fresh LLM connection after 45 s idle (#1016) - #1134

Merged
altic-dev merged 2 commits into
altic-dev:mainfrom
andrewmitchell-automations:B/llm-idle-connection-stall
Oct 15, 2026
Merged

altic-dev merged 2 commits into
altic-dev:mainfrom
andrewmitchell-automations:B/llm-idle-connection-stall

Conversation

@andrewmitchell-automations

Copy link
Copy Markdown
Contributor

Description

After about 1.5–2 minutes idle, the first AI request can stall ~6.5 s on a pooled connection that the network path already dropped. It then fails with -1005 and the retry succeeds (#1016). LLMClient used one URLSession for the whole app life, so it always tried the old connection first.

Now LLMClient replaces its URLSession when no request has run for more than 45 s. The new session has an empty connection pool, so the next request opens a new connection (one extra TLS handshake) and does not wait for the dead one to time out. Requests closer together still share a connection. The reporter saw no stalls for requests less than ~60 s apart.

  • The swap happens only when no request is in flight. The old session is closed with finishTasksAndInvalidate().
  • Idle time is wall-clock time, so time while the Mac sleeps counts too.
  • A debug log line ("Opening a fresh connection after N s idle") shows when it happens.

Type of Change

  • 🐞 Bug fix
  • ✨ New feature
  • 💥 Breaking change
  • 🧹 Chore
  • 📝 Documentation update

Related Issue or Discussion

Closes #1016

Testing

New regression tests: LLMClientIdleConnectionTests starts a local keep-alive HTTP server that counts connections.

  • testRequestAfterIdleGapOpensFreshConnection: two calls with an idle gap longer than the limit must use 2 connections. It fails without the fix (main plus only the new init parameter: 1 connection, so the old one is reused) and passes with this change.
  • testBackToBackRequestsReuseConnection: two calls in a row must still use 1 connection.

Reproduction attempt: I could not reproduce the stall on my network. A probe with the same URLSession setup sent requests to api.groq.com after idle gaps of 30–180 s, and every one finished in under 170 ms (HTTP/3, connection reused up to 150 s). So the dead connection depends on the network path (router, VPN or firewall). This PR makes the client stop reusing long-idle connections. @Conte777 offered to test, and their logs would confirm the fix on an affected network.

Checks run:

  • LLMClientIdleConnectionTests, LLMClientRequestBodyTests, ModelTemperatureSupportTests, StripThinkingTagsTests: 43 tests, 0 failures.

  • Full suite, with the same xcodebuild test command as CI: 2,345 tests, 2,281 passed, 39 skipped, 25 failed. None of the failures are in code this PR changes:

    • 9 UpdateInstallationPipelineTests fail with invalidSignature(-67050). They fail the same way on unchanged main on my Mac, probably because the local build is unsigned.
    • 15 AudioRouteRecoveryIntegrationTests and 1 AppSearchServiceTests timed out in the full run. All of them pass when run alone on this branch.
  • Tested on Intel Mac

  • Tested on Apple Silicon Mac

  • Tested on macOS version: 27.2 (26B5091g), arm64, FluidVoice 1.6.12-beta.1 (main @ 2ca292d)

  • Ran linter locally: swiftlint --strict --config .swiftlint.yml Sources Tests Package.swift (0.63.2, 0 violations)

  • Ran formatter locally: swiftformat --config .swiftformat on the changed files (0.63.1, no changes needed). I did not run it on all of Sources, because this version wants to reformat many unrelated files.

  • Ran tests locally: see above

Screenshots / Video

  • No UI/visual changes; screenshots/video are not applicable.

Regression test output, before and after:
Before (main plus the new test):

LLMClientIdleConnectionTests.swift:30: error: testRequestAfterIdleGapOpensFreshConnection : XCTAssertEqual failed: ("1") is not equal to ("2")

After:

Test Case 'LLMClientIdleConnectionTests testBackToBackRequestsReuseConnection' passed
Test Case 'LLMClientIdleConnectionTests testRequestAfterIdleGapOpensFreshConnection' passed

Notes

  • The 45 s limit is a judgment call, based on the reporter's numbers (<60 s fine, 90–120 s stalled). The cost after idle is one new TLS handshake (about 100 ms), compared with about 6.5 s now.
  • Not done in this PR: retrying -1005 without the 200 ms delay. It saves only 0.2 s.

AI use: Claude drafted the change, the regression test and this description. I reviewed every changed line, ran the tests and the lint checks listed above, and ran the Groq probe. I could not reproduce the real stall on my network and did not test on Intel.

  • A human created this PR or reviewed and approved it before submission, understands the full diff, and takes responsibility for the change.

🤖 Generated with Claude Code

After a short idle period, the first AI request could stall ~6.5 s on a
pooled connection the network path had already dropped, then fail with
-1005 before the retry succeeded. LLMClient now replaces its URLSession
when no request has run for more than 45 s, so the next request opens a
new connection instead of waiting for the dead one to time out.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-13T16:33:05.432101Z 6ef83a5 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@greptile-apps

greptile-apps Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

RetriggerConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; the previous cleanup issue is fixed and no new actionable issues were found.

Summary

LLMClient replaces its connection pool after more than 45 seconds idle, while keeping the current session for active and closely spaced requests.

  • LLM requests get a fresh connection after 45 seconds idle.

Reviews (2) · Last reviewed commit: "test: close accepted keep-alive connecti..." · Reviewed by Greptile

@github-actions

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

FluidVoice PR build ready

Download FluidVoice-PR-1134-b2c2b6fa0e75

The artifact contains the ad-hoc-signed app ZIP, Xcode archive, build manifest, and installation instructions. It expires 5 days after the build.

Install the app

  1. Extract the downloaded artifact, then extract FluidVoice-PR-1134.app.zip.

  2. Move FluidVoice fix(ai): open a fresh LLM connection after 45 s idle (#1016) #1134.app into the /Applications folder.

  3. Open Terminal and remove the download quarantine marker:

    xattr -dr com.apple.quarantine "/Applications/FluidVoice #1134.app"
    
  4. In Applications, Control-click FluidVoice fix(ai): open a fresh LLM connection after 45 s idle (#1016) #1134.app and choose Open.

  5. If macOS still blocks it, open System Settings → Privacy & Security, click Open Anyway, and confirm.

This build has its own app identity, so its permissions are separate from the release version of FluidVoice.

View workflow run

@altic-dev
altic-dev merged commit ad1d1ff into altic-dev:main Oct 15, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] First AI enhancement request after ~1.5–2 min idle hangs ~6.5 s, fails with -1005, then succeeds on retry

2 participants