Skip to content

Commit

Permalink
fix improper yield usage (#175)
Browse files Browse the repository at this point in the history
`yield` does not work with chronos futures
  • Loading branch information
arnetheduck authored Dec 13, 2023
1 parent a8731e9 commit 3336052
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
6 changes: 2 additions & 4 deletions json_rpc/client.nim
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ proc rpcCallNode*(path: string, params: JsonNode, id: ClientId): JsonNode =
%{"jsonrpc": %"2.0", "method": %path, "params": params, "id": %id}

method call*(client: RpcClient, name: string,
params: JsonNode): Future[Response] {.
base, async, gcsafe, raises: [Defect].} =
params: JsonNode): Future[Response] {.base, async.} =
discard

method close*(client: RpcClient): Future[void] {.
base, async, gcsafe, raises: [Defect].} =
method close*(client: RpcClient): Future[void] {.base, async.} =
discard

template `or`(a: JsonNode, b: typed): JsonNode =
Expand Down
34 changes: 19 additions & 15 deletions json_rpc/servers/httpserver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,27 @@ proc processClientRpc(rpcServer: RpcHttpServer): HttpProcessCallback =
if not res.isNil:
return res

let body = await request.getBody()

let headers = HttpTable.init([("Content-Type",
let
body = await request.getBody()
headers = HttpTable.init([("Content-Type",
"application/json; charset=utf-8")])

let future = rpcServer.route(string.fromBytes(body))
yield future
if future.failed:
debug "Internal error while processing JSON-RPC call"
return await request.respond(Http503,
"Internal error while processing JSON-RPC call",
headers)
else:
var data = future.read()
let res = await request.respond(Http200, data, headers)
trace "JSON-RPC result has been sent"
return res
data =
try:
await rpcServer.route(string.fromBytes(body))
except CancelledError as exc:
raise exc
except CatchableError as exc:
debug "Internal error while processing JSON-RPC call"
return await request.respond(
Http503,
"Internal error while processing JSON-RPC call: " & exc.msg,
headers)

res = await request.respond(Http200, data, headers)

trace "JSON-RPC result has been sent"
return res
else:
return dumbResponse()

Expand Down

0 comments on commit 3336052

Please sign in to comment.