Skip to content

Commit 50e7f54

Browse files
authored
Ignore StateError when closing sockets, and attempt multiple times. (#270)
1 parent 2f1ac14 commit 50e7f54

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

neat_cache/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## v2.0.5
2+
* Ignore undocumented errors from `Socket.close`.
3+
* Add additional calls to `Socket.close` after all streams have consumed.
4+
15
## v2.0.4
26
* Reconnect when a redis command fails.
37

neat_cache/lib/src/providers/resp.dart

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class RespClient {
145145
/// [1]: https://redis.io/topics/protocol
146146
Future<Object?> command(List<Object> args) async {
147147
if (_closing) {
148-
throw RedisConnectionException._('redis connection is closed');
148+
throw RedisConnectionException._('redis connection is closing');
149149
}
150150

151151
final out = BytesBuilder(copy: false);
@@ -215,13 +215,39 @@ class RespClient {
215215
}
216216
}
217217

218+
// Ensure that we also attempt to close the [Socket] after all pending
219+
// commands have been sent.
220+
//
221+
// This may be necessary, as it appears that [Socket] from 'dart:io' may
222+
// throw [StateError], if it's bound to stream when [close] is called.
223+
//
224+
// See: https://github.com/dart-lang/sdk/issues/41707
225+
_pendingStream = _pendingStream.then((_) async {
226+
try {
227+
await _output.close();
228+
} catch (e) {
229+
// ignore
230+
}
231+
});
232+
218233
if (!force) {
219234
scheduleMicrotask(() async {
220-
await _output.close().catchError((_) {/* ignore */});
235+
try {
236+
await _output.close();
237+
} catch (e) {
238+
// ignore
239+
}
221240
});
222241
await _closed.future;
223242
} else {
224-
await _output.close().catchError((_) {/* ignore */});
243+
try {
244+
// TODO(https://github.com/dart-lang/sdk/issues/61743): Sadly, this can throw, it can even throw synchroniously,
245+
// implying [Socket] from 'dart:io' fails to implement
246+
// [StreamSink] as specified.
247+
await _output.close();
248+
} catch (e) {
249+
// ignore
250+
}
225251

226252
// Resolve all outstanding requests
227253
final pending = _pending.toList(growable: false);

neat_cache/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: neat_cache
2-
version: 2.0.4
2+
version: 2.0.5
33
description: >-
44
A neat cache abstraction for wrapping in-memory or redis caches.
55
homepage: https://github.com/google/dart-neats/tree/master/neat_cache

0 commit comments

Comments
 (0)