Skip to content

type error when specifiying a connectionTimeout for HttpClient #44895

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

Closed
miDeb opened this issue Feb 9, 2021 · 5 comments
Closed

type error when specifiying a connectionTimeout for HttpClient #44895

miDeb opened this issue Feb 9, 2021 · 5 comments
Assignees
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-io type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@miDeb
Copy link
Contributor

miDeb commented Feb 9, 2021

Minimal code example:

import 'dart:io';

void main() async {
  final client = HttpClient();
  client.connectionTimeout = Duration();
  await client.openUrl(
    'get',
    Uri.parse(
      'https://example.com/',
    ),
  );
}

results in:

Unhandled exception:
type '() => Null' is not a subtype of type '(() => FutureOr<SecureSocket>)?' of 'onTimeout'
#0      Future.timeout (dart:async/future_impl.dart)
#1      _ConnectionTarget.connect.<anonymous closure> (dart:_http/http_impl.dart:2222:37)
<asynchronous suspension>
#2      _ConnectionTarget.connect.<anonymous closure> (dart:_http/http_impl.dart)
<asynchronous suspension>

OS: linux
dart: Dart SDK version: 2.13.0-0.0.dev (dev) (Thu Feb 4 08:08:19 2021 -0800) on "linux_x64"

@miDeb miDeb mentioned this issue Feb 9, 2021
6 tasks
@mit-mit
Copy link
Member

mit-mit commented Feb 10, 2021

cc @lrhn

@miDeb
Copy link
Contributor Author

miDeb commented Feb 10, 2021

I believe the problem is the following:

Future socketFuture = task.socket; // type of task.socket: Future<Socket>
// ...
// but socketFuture is used as a Future<Socket?>, if there is a timeout it returns null:
socketFuture = socketFuture.timeout(connectionTimeout, onTimeout: () {
    _socketTasks.remove(task);
   ask.cancel();
   return null;
});

What's the proper way to convert a Future<T> to a Future<T?> ?

@lrhn
Copy link
Member

lrhn commented Feb 10, 2021

Currently there is no easy way to just introduce nullability in the type of a future.
I'd write this as:

socketFuture = socketFuture.then<Socket?>((x) => x).timeout(....);

We could perhaps introduce a cast extension method on futures, so you could just write:

socketFuture = socketFuture.cast<Socket?>().timeout(...);

but we can't restrict it to up-casts, so it's going to be doing dynamic casts of all events, even when not necessary.
The alternative would be a Future<T?> orNull(); extension method on Future<T>.

I'll fix the dart:io library to not make that type error.

@lrhn lrhn added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-io type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Feb 10, 2021
@lrhn lrhn self-assigned this Feb 10, 2021
dart-bot pushed a commit that referenced this issue Feb 17, 2021
This would have been caught if the static type had not
been made `Future<dynamic>`.

Fixes #44895

BUG= http://dartbug.com/44895
TEST= standalone/io/regress_44895

Change-Id: I237c552fdb42943b395352a7232b34ab5488ac6b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/184261
Reviewed-by: Nate Bosch <[email protected]>
Commit-Queue: Lasse R.H. Nielsen <[email protected]>
@miDeb
Copy link
Contributor Author

miDeb commented Feb 17, 2021

It is currently unclear when/how a null safe version of dio will be released because the mantainer is inactive, but this bug will likely block a release. Is it possible to uplift this fix (if that is necessary)?
EDIT: Oh I see, that has already happended :)

@mit-mit
Copy link
Member

mit-mit commented Feb 22, 2021

This issue was cherry picked in #44989, and is available in Dart's beta channel in version 2.12.0-259.14.beta and later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-io type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

3 participants