Skip to content

Share a single HttpClient for AsyncImage downloads - #157

Merged
maxkatz6 merged 1 commit into
mainfrom
fix/asyncimage-shared-httpclient
Oct 25, 2026
Merged

maxkatz6 merged 1 commit into
mainfrom
fix/asyncimage-shared-httpclient

Conversation

@maxkatz6

Copy link
Copy Markdown
Member

Fixes #32

Changes

  • AsyncImage: when IsCacheEnabled is false, each load used to create and dispose its own HttpClient, which can run out of sockets when many images load. It now reuses ImageCache's shared client. The response stream is now disposed too.
  • ImageCache: Instance was [ThreadStatic]. Because of ConfigureAwait(false) and the Android Task.Run path, every thread-pool thread ended up with its own cache, HttpClient, in-memory storage and duplicate-request tracking. It is now a single process-wide instance. CacheBase already uses concurrent collections and semaphores, so this is safe.
  • CacheBase: HttpClient is now initialized thread-safely. Downloads pass the cancellation token through and copy streams asynchronously instead of calling CopyTo synchronously.

No public API changes: everything touched is internal. The Android main-thread Task.Run workaround is unchanged.

🤖 Generated with Claude Code

- AsyncImage no longer creates and disposes an HttpClient per load when
  caching is disabled; it reuses ImageCache's client.
- ImageCache.Instance was [ThreadStatic], so every thread-pool thread got
  its own cache, HttpClient and in-memory storage. Make it a real singleton.
- Initialize CacheBase.HttpClient thread-safely.
- Honor the cancellation token and copy streams asynchronously when
  downloading into the cache.

Fixes #32

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@maxkatz6
maxkatz6 merged commit 9a614b7 into main Oct 25, 2026
3 checks passed
@maxkatz6
maxkatz6 deleted the fix/asyncimage-shared-httpclient branch October 26, 2026 02: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.

Bad usage of HttpClient

1 participant