Fix socket server close.

This commit is contained in:
bhartnett 2024-10-04 16:39:58 +08:00
parent 53e9d98dcf
commit d4d970581f
No known key found for this signature in database
GPG Key ID: 076F2830DA6BD535
2 changed files with 8 additions and 3 deletions

View File

@ -80,6 +80,7 @@ method callBatch*(client: RpcSocketClient,
proc processData(client: RpcSocketClient) {.async: (raises: []).} =
while true:
var localException: ref JsonRpcError
var stopping = false
while true:
try:
var value = await client.transport.readLine(defaultMaxRequestLength)
@ -99,6 +100,7 @@ proc processData(client: RpcSocketClient) {.async: (raises: []).} =
break
except CancelledError as exc:
localException = newException(JsonRpcError, exc.msg)
stopping = true
await client.transport.closeWait()
break
@ -108,6 +110,9 @@ proc processData(client: RpcSocketClient) {.async: (raises: []).} =
if client.batchFut.isNil.not and not client.batchFut.completed():
client.batchFut.fail(localException)
if stopping:
break
# async loop reconnection and waiting
try:
info "Reconnect to server", address=`$`(client.address)
@ -136,5 +141,5 @@ method isConnected*(client: RpcSocketClient): bool =
method close*(client: RpcSocketClient) {.async.} =
await client.loop.cancelAndWait()
if not client.transport.isNil:
client.transport.close()
await client.transport.closeWait()
client.transport = nil

View File

@ -52,8 +52,8 @@ suite "Socket Server/Client RPC":
test "Client close and isConnected":
check client.isConnected() == true
# Is socket server close broken?
# waitFor client.close()
# check client.isConnected() == false
waitFor client.close()
check client.isConnected() == false
srv.stop()
waitFor srv.closeWait()