Skip to content

Commit ba2d0bc

Browse files
committed
fix: maybe deadlock in coordinator
1 parent 7dd9b2b commit ba2d0bc

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

coordinator/Services/JudgerCoordinatorService.cs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,18 @@ public async ValueTask<bool> TryUseConnection(HttpContext ctx) {
105105
ctx.Request.Query.TryGetValue("conn", out var connId_);
106106
var connId = connId_.FirstOrDefault();
107107

108-
var connLock = await connectionLock.LockAsync();
109-
if (connections.TryGetValue(auth, out var lastConn)) {
110-
if (lastConn.ConnectionId != null && connId != null && lastConn.ConnectionId == connId) {
111-
// replace this session
112-
await lastConn.Socket.Close(System.Net.WebSockets.WebSocketCloseStatus.PolicyViolation, "Duplicate connection", CancellationToken.None);
113-
connections.Remove(auth);
114-
} else {
115-
ctx.Response.StatusCode = StatusCodes.Status409Conflict;
116-
connLock.Dispose();
117-
return false;
108+
using (await connectionLock.LockAsync()) {
109+
if (connections.TryGetValue(auth, out var lastConn)) {
110+
if (lastConn.ConnectionId != null && connId != null && lastConn.ConnectionId == connId) {
111+
// replace this session
112+
await lastConn.Socket.Close(System.Net.WebSockets.WebSocketCloseStatus.PolicyViolation, "Duplicate connection", CancellationToken.None);
113+
connections.Remove(auth);
114+
} else {
115+
ctx.Response.StatusCode = StatusCodes.Status409Conflict;
116+
return false;
117+
}
118118
}
119119
}
120-
connLock.Dispose();
121120

122121
var ws = await ctx.WebSockets.AcceptWebSocketAsync();
123122

@@ -601,16 +600,16 @@ public void Stop() {
601600
/// </summary>
602601
/// <returns>(connectedCount, runningCount)</returns>
603602
public async Task<(int, int)> GetConnectedJudgerInfo() {
604-
await connectionLock.WaitAsync();
605-
var result = connections.Aggregate((0, 0), (cnt, val) => {
606-
if (val.Value.ActiveTaskCount > 0) {
607-
return (cnt.Item1 + 1, cnt.Item2 + 1);
608-
} else {
609-
return (cnt.Item1 + 1, cnt.Item2);
610-
}
611-
});
612-
connectionLock.Release();
613-
return result;
603+
using (await connectionLock.LockAsync()) {
604+
var result = connections.Aggregate((0, 0), (cnt, val) => {
605+
if (val.Value.ActiveTaskCount > 0) {
606+
return (cnt.Item1 + 1, cnt.Item2 + 1);
607+
} else {
608+
return (cnt.Item1 + 1, cnt.Item2);
609+
}
610+
});
611+
return result;
612+
}
614613
}
615614
}
616615

0 commit comments

Comments
 (0)