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

Ignore StateError when closing sockets, and attempt multiple times. #270

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions neat_cache/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v2.0.5
* Ignore undocumented errors from `Socket.close`.
* Add additional calls to `Socket.close` after all streams have consumed.

## v2.0.4
* Reconnect when a redis command fails.

Expand Down
32 changes: 29 additions & 3 deletions neat_cache/lib/src/providers/resp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class RespClient {
/// [1]: https://redis.io/topics/protocol
Future<Object?> command(List<Object> args) async {
if (_closing) {
throw RedisConnectionException._('redis connection is closed');
throw RedisConnectionException._('redis connection is closing');
}

final out = BytesBuilder(copy: false);
Expand Down Expand Up @@ -215,13 +215,39 @@ class RespClient {
}
}

// Ensure that we also attempt to close the [Socket] after all pending
// commands have been sent.
//
// This may be necessary, as it appears that [Socket] from 'dart:io' may
// throw [StateError], if it's bound to stream when [close] is called.
//
// See: https://github.com/dart-lang/sdk/issues/41707
_pendingStream = _pendingStream.then((_) async {
try {
await _output.close();
} catch (e) {
// ignore
}
});

if (!force) {
scheduleMicrotask(() async {
await _output.close().catchError((_) {/* ignore */});
try {
await _output.close();
} catch (e) {
// ignore
}
});
await _closed.future;
} else {
await _output.close().catchError((_) {/* ignore */});
try {
// NOTE: Sadly, this can throw, it can even throw synchroniously,
// implying [Socket] from 'dart:io' fails to implement
// [StreamSink] as specified.
Comment on lines +244 to +246
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// NOTE: Sadly, this can throw, it can even throw synchroniously,
// implying [Socket] from 'dart:io' fails to implement
// [StreamSink] as specified.
// NOTE: Sadly, this can throw, it can even throw synchroniously,
// implying [Socket] from 'dart:io' fails to implement
// [StreamSink] as specified.

If this is true we should add a issue number here.

await _output.close();
} catch (e) {
// ignore
}

// Resolve all outstanding requests
final pending = _pending.toList(growable: false);
Expand Down
2 changes: 1 addition & 1 deletion neat_cache/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: neat_cache
version: 2.0.4
version: 2.0.5
description: >-
A neat cache abstraction for wrapping in-memory or redis caches.
homepage: https://github.com/google/dart-neats/tree/master/neat_cache
Expand Down
Loading