flutter_cached_network_image icon indicating copy to clipboard operation
flutter_cached_network_image copied to clipboard

CacheManager: Connection closed before full header was received

Open wangxingxing123654 opened this issue 4 years ago • 22 comments

🐛 Bug Report

CacheManager: Failed to download file from https://harbor.alpha.stylewe.com/image_cache/resize/335x445/image/catalog/2015-04-01/new pic/15-4-1-15-3.JPG with error: Connection closed before full header was received

Expected behavior

Reproduction steps

Configuration

Version: 1.x

Platform:

  • [ ] :iphone: iOS
  • [ ] :robot: Android

wangxingxing123654 avatar Aug 17 '20 08:08 wangxingxing123654

but brower can open it , [pic url](https://harbor.alpha.stylewe.com/image_cache/resize/335x445/image/catalog/2015-04-01/new pic/15-4-1-15-3.JPG)

wangxingxing123654 avatar Aug 17 '20 08:08 wangxingxing123654

any solutions for this issue ?

khalid-alsaleh-dev avatar Aug 28 '20 09:08 khalid-alsaleh-dev

this is really issue. when I get over 30 images, some images are stuck and not represent. Is this issue is flutter's http issue or cache library's issue?

whatamelon avatar Sep 07 '20 01:09 whatamelon

same issue here, the fix to change request from https to HTTP is not helping

Ocelyn avatar Oct 06 '20 04:10 Ocelyn

Same here, pls fix it :)

Arley011 avatar Oct 08 '20 13:10 Arley011

I think it might be a problem that the server closes the connections when to many images are downloaded at the same time. So I guess it could be solved with queuing as proposed in #453

renefloor avatar Oct 08 '20 15:10 renefloor

@renefloor This issue is still occurring on Flutter 1.22.1, with CachedNetworkImage 2.3.3

bradsb avatar Oct 16 '20 10:10 bradsb

Which version of flutter_cache_manager is in your pubspec.lock?

renefloor avatar Oct 16 '20 10:10 renefloor

  flutter_cache_manager:
    dependency: transitive
    description:
      name: flutter_cache_manager
      url: "https://pub.dartlang.org"
    source: hosted
    version: "2.0.0"

bradsb avatar Oct 16 '20 11:10 bradsb

Are you requesting a lot of images? I guess there can be more reasons why a connection is dropped. Can you give me a small reproducible example?

renefloor avatar Oct 16 '20 11:10 renefloor

Apparently it is not really related to doing to many requests, as it is reproduced with doing just 1 request as well. I'll reopen this issue, but you'll should keep track (and thumbs up) this issue: https://github.com/flutter/flutter/issues/41573

renefloor avatar Nov 23 '20 19:11 renefloor

I was facing the same issue, when I was using ListView, you can fix this by using ScrollAwareImageProvider with CachedNetworkImageProvider passed to it as imageProvider.

Edit: This solution significantly reduces the events encountered for the issue, but do not consider this as a valid solution for the problem, it still needs to be fixed at the platform/package end.

shbmbhrdwj avatar Feb 08 '22 08:02 shbmbhrdwj

@shbmbhrdwj Could you provide an example ?

erf avatar Feb 08 '22 12:02 erf

@shbmbhrdwj example please :)

SamKlim avatar Mar 01 '22 21:03 SamKlim

@erf @SamKlim It's pretty straitforward:

You can use it like this:

Image(
    image: ScrollAwareImageProvider(
        context: _disposableBuildContext,
        imageProvider: CachedNetworkImageProvider(
           imageUrl,
        ),
    ),
)

shbmbhrdwj avatar Mar 02 '22 15:03 shbmbhrdwj

@shbmbhrdwj how would you add a Placeholder with that example and where do you get _disposableBuildContext from ?

erf avatar Mar 02 '22 16:03 erf

This week I've been investigating workarounds for the upstream Flutter issue in CachedNetworkImage. The approach I have taken is to implement my own CacheManager and FileService so I can add some simple retry logic. If the request fails with a ClientException, then we wait for 100ms or so, then retry the request up to X times.

Here is the FileService we are using right now - it could easily be extended to retry on response error codes (501, etc) but we are not concerned with that for our purposes.

/// A FileService for Flutter Cache Manager that implements
/// simple retry logic in the event that fetching fails due
/// to client errors, which typically occur with poor internet
/// connections.
///
/// Note that this service does not retry on certain HTTP error
/// codes, (400, 500, etc), but this would be trivial to extend.
class CaFileService extends FileService {
  CaFileService({
    /// Override the default HTTP client
    http.Client? httpClient,
    this.maxAttempts = 2,
  }) : _httpClient = httpClient ?? http.Client();

  /// The HTTP client to use. A Client will be automatically
  /// created if not set.
  final http.Client _httpClient;

  /// The maximum number of attempts before giving up
  /// on fetching the resource.
  final int maxAttempts;

  @override
  Future<FileServiceResponse> get(
    String url, {
    Map<String, String>? headers,

    /// The current attempt
    int attemptNumber = 1,
  }) async {
    final req = http.Request('GET', Uri.parse(url));
    if (headers != null) {
      req.headers.addAll(headers);
    }

    try {
      final httpResponse = await _httpClient.send(req);
      return HttpGetResponse(httpResponse);
    } catch (err) {
      if (attemptNumber < maxAttempts) {
        // We use an increasing delay with each attempt.
        final delay = attemptNumber * 100;
        await Future.delayed(Duration(milliseconds: delay));

        // retry the fetch
        return get(url, headers: headers, attemptNumber: attemptNumber + 1);
      } else {
        // Reached the maximum number of retries, so giving up.
        rethrow;
      }
    }
  }
}

You can use this by passing a custom CacheManager to CachedNetworkImage.

MichaelMarner avatar Jun 15 '22 02:06 MichaelMarner

Same issue, pls fix it :)

Alfaizkhan avatar Aug 23 '22 06:08 Alfaizkhan

Same issue here, for a lot of images in the same view - they are all downloading in the same time

marcellocamara avatar Sep 08 '22 16:09 marcellocamara

the same issue, I'm using cache manager, with errorBuilder some of the images on the page are rendered with the errorBuilder

aissam-amez avatar Nov 01 '22 13:11 aissam-amez

i used this code https://github.com/flutter/flutter/issues/25107#issuecomment-456667247 and added a timeout duration ,i think it fixed my problem

import 'dart:io';

class MyHttpOverrides extends HttpOverrides { @override HttpClient createHttpClient(SecurityContext? context) { return super.createHttpClient(context) ..maxConnectionsPerHost = 300 ..connectionTimeout = const Duration(minutes: 6) ..idleTimeout = const Duration(minutes: 6); } }

void main() { HttpOverrides.global = MyHttpOverrides(); runApp(MyApp()); }

aissam-amez avatar Nov 01 '22 15:11 aissam-amez

i have this problem yet

pishguy avatar Aug 21 '23 08:08 pishguy