Skip to content

fix: read the settings response message before disconnecting - #322

Open
njuro wants to merge 1 commit into
segmentio:mainfrom
njuro:fix/settings-response-leak
Open

fix: read the settings response message before disconnecting#322
njuro wants to merge 1 commit into
segmentio:mainfrom
njuro:fix/settings-response-leak

Conversation

@njuro

@njuro njuro commented Aug 27, 2026

Copy link
Copy Markdown

Problem

RequestFactory.settings() handles a non-200 reply as

connection.disconnect()
throw IOException("HTTP " + responseCode + ": " + connection.responseMessage)

With the OkHttp-backed OkHttpURLConnection (HTTP client v2), disconnect() closes the response and sets connected = false, and getResponseMessage() then calls connect() again. So every failed settings fetch sends the request a second time, and that second Response is never closed. OkHttp reports it a few minutes later:

WARN okhttp3.OkHttpClient : A connection to https://cdn-settings.segment.com/ was leaked. Did you forget to close a response body?

Reproduces with any non-200 from the CDN — an invalid write key, or a transient CDN error — once per Analytics instance per checkSettings().

Fix

Read responseMessage into a local before disconnect(). One request is made and closed; the exception message is unchanged.

On OkHttpURLConnection, disconnect() closes the response and marks the
connection as not connected; getResponseMessage() then reconnects, so a
failed settings fetch sent the request twice and never closed the second
response. OkHttp reported it as a leaked connection.
@njuro
njuro marked this pull request as ready for review August 27, 2026 12:44
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.

1 participant