Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] The timeout setting will be invalid when the server does not return data #18

Closed
heafox opened this issue Apr 6, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@heafox
Copy link

heafox commented Apr 6, 2024

It's too late, let me briefly record the problem first

A 5-second timeout was set at the call location

_httpClient ??= FetchClient();
final response = await _httpClient!.send(request).timeout(_timeout);�

fetch_client.dart

The program will get stuck in this sentence of code 5m return
response = await fetch(request.url.toString(), init);

I added a cancellation callback abort1

    final abort1 = () {
      _abortCallbacks.remove(abort1);
      abortController.abort();
    };
    _abortCallbacks.add(abort1);

    final Response response;
    try {
      print({1, request.url});
      response = await fetch(request.url.toString(), init);
      print({2, request.url});

      if (response.type == 'opaqueredirect' &&
          !request.followRedirects &&
          redirectPolicy != RedirectPolicy.alwaysFollow)
        return _probeRedirect(
          request: request,
          initialResponse: response,
          init: init,
          abortController: abortController,
        );
    } catch (e) {
      throw ClientException('Failed to execute fetch: $e', request.url);
    }�
@Zekfad
Copy link
Owner

Zekfad commented Apr 6, 2024

Hi, thank you for your report, but I don't quite understand the issue you're facing.

Please provide what options do you use. Your snippet seems to be related to probe request mode?
Also what page or what is the specifics of server/response is required to reproduce the issue?

@Zekfad Zekfad added the bug Something isn't working label Apr 6, 2024
@heafox
Copy link
Author

heafox commented Apr 6, 2024

https://github.com/mylisabox/flutter_mjpeg

Mjpeg(
  isLive: true,
  stream: 'http://ruanzheng.com:9430/stream/192.168.1.2?resolution=480P',
  fit: BoxFit.fill,
  timeout: const Duration(seconds: 5),
  httpClient: FetchClient(),
);

This mjpeg did not return any data, resulting in the timeout set for the request not taking effect.

The mjpeg code has been modified to ensure that it calls the close connection interface.

  ...
  Future<void> dispose() async {
    if (_subscription != null) {
      await _subscription!.cancel();
      _subscription = null;
    }
    _httpClient.close();
  }
  ...
  void updateStream(BuildContext context, ValueNotifier<MemoryImage?> image,
      ValueNotifier<List<dynamic>?> errorState) async {
    try {
      final request = Request("GET", Uri.parse(stream));
      request.headers.addAll(headers);
      final response = await _httpClient.send(request).timeout(
          _timeout); //timeout is to prevent process to hang forever in some case

      if (response.statusCode >= 200 && response.statusCode < 300) {
       ...
      } else {
        if (context.mounted) {
          errorState.value = [
            HttpException('Stream returned ${response.statusCode} status'),
            StackTrace.current
          ];
          image.value = null;
        }
        dispose();
      }
    } catch (error, stack) {
      if (context.mounted) {
        errorState.value = [error, stack];
        image.value = null;
      }
      dispose();
    }
  }

3Q

@Zekfad
Copy link
Owner

Zekfad commented Apr 15, 2024

I've made changes that should incorporate different fix for this issue.
Could you please check current master branch?

@heafox
Copy link
Author

heafox commented Apr 25, 2024

Fixed it :)

@heafox heafox closed this as completed Apr 25, 2024
@Zekfad
Copy link
Owner

Zekfad commented May 3, 2024

@heafox I've published this version as 1.1.0 release in Dart Pub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants