REST API: fix memleak by closing AsyncStreamWrapper

Previous version closed only bodyReader, while now it closes entire bodyStream.

But creation of bodyStream is now moved out of "try" block which may lead to unhandled exceptions (?)
This commit is contained in:
Bulat-Ziganshin 2022-08-25 01:38:57 +03:00 committed by benbierens
parent dd8fc32e7b
commit 452e344bf3
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
1 changed files with 6 additions and 6 deletions

View File

@ -169,7 +169,7 @@ proc initRestApi*(node: CodexNodeRef, conf: CodexConf): RestRouter =
##
trace "Handling file upload"
var bodyReader = request.getBodyReader()
let bodyReader = request.getBodyReader()
if bodyReader.isErr():
return RestApiResponse.error(Http500, bodyReader.error)
@ -177,11 +177,11 @@ proc initRestApi*(node: CodexNodeRef, conf: CodexConf): RestRouter =
# wait 1000ms before giving up
await request.handleExpect()
try:
let
bodyStream = AsyncStreamWrapper.new(reader = AsyncStreamReader(bodyReader.get))
blockSize = (blockSize.get |? BlockSize).int
let
bodyStream = AsyncStreamWrapper.new(reader = AsyncStreamReader(bodyReader.get))
blockSize = (blockSize.get |? BlockSize).int
try:
without cid =? (await node.store(bodyStream, blockSize)), error:
trace "Error uploading file", exc = error.msg
return RestApiResponse.error(Http500, error.msg)
@ -193,7 +193,7 @@ proc initRestApi*(node: CodexNodeRef, conf: CodexConf): RestRouter =
except AsyncStreamError:
return RestApiResponse.error(Http500)
finally:
await bodyReader.get.closeWait()
await bodyStream.closeImpl()
# if we got here something went wrong?
return RestApiResponse.error(Http500)