2
0
mirror of https://github.com/esiur/esiur-dart.git synced 2025-05-06 12:02:57 +00:00

dead lock avoidance

This commit is contained in:
Ahmed Zamil 2024-07-16 09:44:18 +03:00
parent 675b94e909
commit 37643008aa
2 changed files with 28 additions and 18 deletions

View File

@ -25,6 +25,7 @@ SOFTWARE.
import 'dart:async'; import 'dart:async';
import 'package:collection/collection.dart'; import 'package:collection/collection.dart';
import 'package:esiur/src/Net/IIP/DistributedResourceAttachRequestInfo.dart';
import 'package:esiur/src/Security/Membership/AuthorizationRequest.dart'; import 'package:esiur/src/Security/Membership/AuthorizationRequest.dart';
import 'package:web_socket_channel/status.dart'; import 'package:web_socket_channel/status.dart';
import '../../Misc/Global.dart'; import '../../Misc/Global.dart';
@ -152,8 +153,8 @@ class DistributedConnection extends NetworkConnection with IStore {
KeyList<int, WeakReference<DistributedResource>> _suspendedResources = KeyList<int, WeakReference<DistributedResource>> _suspendedResources =
new KeyList<int, WeakReference<DistributedResource>>(); new KeyList<int, WeakReference<DistributedResource>>();
KeyList<int, AsyncReply<DistributedResource>> _resourceRequests = KeyList<int, DistributedResourceAttachRequestInfo> _resourceRequests =
new KeyList<int, AsyncReply<DistributedResource>>(); new KeyList<int, DistributedResourceAttachRequestInfo>();
KeyList<Guid, AsyncReply<TypeTemplate?>> _templateRequests = KeyList<Guid, AsyncReply<TypeTemplate?>> _templateRequests =
new KeyList<Guid, AsyncReply<TypeTemplate?>>(); new KeyList<Guid, AsyncReply<TypeTemplate?>>();
@ -374,7 +375,7 @@ class DistributedConnection extends NetworkConnection with IStore {
_resourceRequests.values.forEach((x) { _resourceRequests.values.forEach((x) {
try { try {
x.triggerError( x.reply.triggerError(
AsyncException(ErrorType.Management, 0, "Connection closed")); AsyncException(ErrorType.Management, 0, "Connection closed"));
} catch (ex) {} } catch (ex) {}
}); });
@ -407,7 +408,6 @@ class DistributedConnection extends NetworkConnection with IStore {
// @TODO: implement this // @TODO: implement this
// if (ready) // if (ready)
// _server.membership?.Logout(session); // _server.membership?.Logout(session);
} else if (autoReconnect && !_invalidCredentials) { } else if (autoReconnect && !_invalidCredentials) {
Future.delayed(Duration(seconds: reconnectInterval), reconnect); Future.delayed(Duration(seconds: reconnectInterval), reconnect);
} else { } else {
@ -1729,12 +1729,12 @@ class DistributedConnection extends NetworkConnection with IStore {
try { try {
var sendDetach = false; var sendDetach = false;
if (_attachedResources.containsKey(instanceId)){ if (_attachedResources.containsKey(instanceId)) {
_attachedResources.remove(instanceId); _attachedResources.remove(instanceId);
sendDetach = true; sendDetach = true;
} }
if (_suspendedResources.containsKey(instanceId)){ if (_suspendedResources.containsKey(instanceId)) {
_suspendedResources.remove(instanceId); _suspendedResources.remove(instanceId);
sendDetach = true; sendDetach = true;
} }
@ -1745,7 +1745,6 @@ class DistributedConnection extends NetworkConnection with IStore {
.done(); .done();
return null; return null;
} catch (ex) { } catch (ex) {
return null; return null;
} }
@ -1825,9 +1824,7 @@ class DistributedConnection extends NetworkConnection with IStore {
} }
} }
void iipEventResourceReassigned(int resourceId, int newResourceId) { void iipEventResourceReassigned(int resourceId, int newResourceId) {}
}
void iipEventResourceDestroyed(int resourceId) { void iipEventResourceDestroyed(int resourceId) {
var r = _attachedResources[resourceId]?.target; var r = _attachedResources[resourceId]?.target;
@ -1842,7 +1839,6 @@ class DistributedConnection extends NetworkConnection with IStore {
// @TODO: handle this mess // @TODO: handle this mess
_neededResources.remove(resourceId); _neededResources.remove(resourceId);
} }
} }
// @TODO: Check for deadlocks // @TODO: Check for deadlocks
@ -3161,28 +3157,33 @@ class DistributedConnection extends NetworkConnection with IStore {
resource = _neededResources[id]; resource = _neededResources[id];
var request = _resourceRequests[id]; var requestInfo = _resourceRequests[id];
//print("fetch $id"); //print("fetch $id");
if (request != null) { if (requestInfo != null) {
if (resource != null && (requestSequence?.contains(id) ?? false)) if (resource != null && (requestSequence?.contains(id) ?? false)) {
return AsyncReply<DistributedResource>.ready(resource); return AsyncReply<DistributedResource>.ready(resource);
return request; } else if (resource != null && requestInfo.requestSequence.contains(id)) {
return AsyncReply<DistributedResource>.ready(resource);
} else {
return requestInfo.reply;
}
} else if (resource != null && !resource.distributedResourceSuspended) { } else if (resource != null && !resource.distributedResourceSuspended) {
// @REVIEW: this should never happen // @REVIEW: this should never happen
print("DCON: Resource not moved to attached."); print("DCON: Resource not moved to attached.");
return new AsyncReply<DistributedResource>.ready(resource); return new AsyncReply<DistributedResource>.ready(resource);
} }
var reply = new AsyncReply<DistributedResource>();
_resourceRequests.add(id, reply);
//print("AttachResource sent ${id}"); //print("AttachResource sent ${id}");
var newSequence = var newSequence =
requestSequence != null ? List<int>.from(requestSequence) : <int>[]; requestSequence != null ? List<int>.from(requestSequence) : <int>[];
var reply = new AsyncReply<DistributedResource>();
_resourceRequests.add(
id, DistributedResourceAttachRequestInfo(reply, newSequence));
newSequence.add(id); newSequence.add(id);
(_sendRequest(IIPPacketAction.AttachResource)..addUint32(id)).done() (_sendRequest(IIPPacketAction.AttachResource)..addUint32(id)).done()

View File

@ -0,0 +1,9 @@
import '../../Core/AsyncReply.dart';
import 'DistributedResource.dart';
class DistributedResourceAttachRequestInfo {
List<int> requestSequence;
AsyncReply<DistributedResource> reply;
DistributedResourceAttachRequestInfo(this.reply, this.requestSequence) {}
}