after socket closes, complete pending requests with errors

This commit is contained in:
Josh Babb 2023-05-25 15:51:27 -05:00
parent e0e4ffe0f4
commit 73312cb920

View file

@ -62,9 +62,10 @@ class JsonRPC {
if (_requestQueue.isNotEmpty) {
// TODO iterate over the remaining requests and if they are not isComplete then complete the completer with an error
Logging.instance.log(
"JsonRPC doneHandler: queue not empty but connection closed",
level: LogLevel.Warning,
"JsonRPC doneHandler: queue not empty but connection closed, completing pending requests with errors",
level: LogLevel.Error,
);
_errorPendingRequests();
}
}
@ -91,6 +92,25 @@ class JsonRPC {
);
}
void _errorPendingRequests() {
if (_requestQueue.isNotEmpty) {
final req = _requestQueue.next;
if (!(req.isComplete)) {
req.completer.completeError('JsonRPC doneHandler: socket closed before request could complete');
_requestQueue.remove(req).then((ret) {
if (_requestQueue.isNotEmpty) {
_errorPendingRequests();
}
});
}
} else {
Logging.instance.log(
"JsonRPC _errorPendingRequests: done completing pending requests with errors",
level: LogLevel.Info,
);
}
}
Future<dynamic> request(String jsonRpcRequest) async {
// todo: handle this better?
// Do we need to check the subscription, too?