Skip to content

HttpException: Unexpected response (unsolicited response without request) not catchable #60333

@rbellens

Description

@rbellens

It happens that a http server responses in an illegal way, resulting in a HttpException: Unexpected response (unsolicited response without request). However, this error is thrown in a Stream listener and cannot be caught in client code. As a result, the program ends prematurely or otherwise behaves unexpected.

Here is some example code, to reproduce:

import 'dart:async';
import 'dart:convert';
import 'dart:io';

Future<void> main() async {
  var uri = Uri.parse(
      'https://identitytoolkit.googleapis.com/v1/recaptchaParams?alt=json');
  var client = HttpClient();

  while (true) {
    try {
      var r = await client.openUrl('GET', uri);

      // we set the content length to -1, but add empty content to trigger an unexpected response from the server
      r.contentLength = -1;
      await r.addStream(Stream.empty());

      print('send request to $uri');
      var response = await r.close();
      print('response status code: ${response.statusCode}');

      await response.transform(utf8.decoder).join();
      await Future.delayed(Duration(milliseconds: 200));
    } catch (e) {
      // the error is never caught
      print('We caught the error: $e');
    }
  }
}

Tested on Dart SDK version: 3.5.3

Metadata

Metadata

Assignees

Labels

area-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.library-iotriagedIssue has been triaged by sub team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions