Skip to content

Commit

Permalink
fix(http2_adapter):request by http2 without network once,the same dom…
Browse files Browse the repository at this point in the history
…ain request will never success. (#1896)

### New Pull Request Checklist

- [x] I have read the
[Documentation](https://pub.dev/documentation/dio/latest/)
- [x] I have searched for a similar pull request in the
[project](https://github.com/cfug/dio/pulls) and found none
- [x] I have updated this branch with the latest `main` branch to avoid
conflicts (via merge from master or rebase)
- [x] I have added the required tests to prove the fix/feature I'm
adding
- [ ] I have updated the documentation (if necessary)
- [x] I have run the tests without failures
- [x] I have updated the `CHANGELOG.md` in the corresponding package

### Additional context and info (if any)

`_ConnectionManager._connect` may throw SocketException when call `await
initFuture`,if it occurs, even if the network is restored, subsequent
requests with the same domain will still obtain the cached initFuture,
and it will still fail

---------

Co-authored-by: Alex Li <[email protected]>
  • Loading branch information
sunhapper and AlexV525 authored Jul 14, 2023
1 parent 4a93745 commit 063486e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion plugins/http2_adapter/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Unreleased

*None.*
- Fix cached `initFuture` not remove when throw exception.

## 2.3.0

Expand Down
7 changes: 6 additions & 1 deletion plugins/http2_adapter/lib/src/connection_manager_imp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ class _ConnectionManager implements ConnectionManager {
if (initFuture == null) {
_connectFutures[domain] = initFuture = _connect(options);
}
transportState = await initFuture;
try {
transportState = await initFuture;
} catch (e) {
_connectFutures.remove(domain);
rethrow;
}
if (_forceClosed) {
transportState.dispose();
} else {
Expand Down
27 changes: 27 additions & 0 deletions plugins/http2_adapter/test/http2_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,31 @@ void main() {
final res = await dio.post('post', data: 'TEST');
expect(res.data.toString(), contains('TEST'));
});

test('request without network and restore', () async {
bool needProxy = true;
final dio = Dio()
..options.baseUrl = 'https://httpbun.com/'
..httpClientAdapter = Http2Adapter(ConnectionManager(
idleTimeout: Duration(milliseconds: 10),
onClientCreate: (uri, settings) {
if (needProxy) {
// first request use bad proxy to simulate network error
settings.proxy = Uri.parse('http://localhost:1234');
needProxy = false;
} else {
// remove proxy to restore network
settings.proxy = null;
}
},
));
try {
// will throw SocketException
await dio.post('post', data: 'TEST');
} on DioException {
// ignore
}
final res = await dio.post('post', data: 'TEST');
expect(res.data.toString(), contains('TEST'));
});
}

0 comments on commit 063486e

Please sign in to comment.