[wip] defensively copy stream data, even when it almost certainly shouldn't be necessary

This commit is contained in:
Michael Bradley, Jr 2021-12-14 13:13:23 -06:00
parent 47bc7f2359
commit f96f120d96
No known key found for this signature in database
GPG Key ID: D0307DBCF21A9A58
1 changed files with 15 additions and 11 deletions

View File

@ -29,15 +29,17 @@ proc readFromStreamWriteToFile(rfd: int, destPath: string)
proc pred(data: openarray[byte]): tuple[consumed: int, done: bool] proc pred(data: openarray[byte]): tuple[consumed: int, done: bool]
{.gcsafe, raises: [Defect].} = {.gcsafe, raises: [Defect].} =
debug "task pred got data", byteCount=data.len let dataCopy = @data
if data.len > 0: debug "task pred got data", byteCount=dataCopy.len
if dataCopy.len > 0:
try: try:
discard destFile.writeBytes(data, 0, data.len) discard destFile.writeBytes(dataCopy, 0, dataCopy.len)
except Exception as e: except Exception as e:
debug "exception raised when task wrote to file", error=e.msg error "exception raised when task wrote to file", error=e.msg
(data.len, data.len < 4096) (dataCopy.len, dataCopy.len < 4096)
else: else:
(0, true) (0, true)
@ -73,17 +75,19 @@ proc main() {.async.} =
proc pred(data: openarray[byte]): tuple[consumed: int, done: bool] proc pred(data: openarray[byte]): tuple[consumed: int, done: bool]
{.gcsafe, raises: [Defect].} = {.gcsafe, raises: [Defect].} =
debug "http server pred got data", byteCount=data.len let dataCopy = @data
if data.len > 0: debug "http server pred got data", byteCount=dataCopy.len
if dataCopy.len > 0:
try: try:
# discard waitFor writer.write(@data, data.len) # discard waitFor writer.write(dataCopy, dataCopy.len)
discard writer.write(@data, data.len) discard writer.write(dataCopy, dataCopy.len)
except Exception as e: except Exception as e:
debug "exception raised when http server wrote to task", error "exception raised when http server wrote to task",
error=e.msg, stacktrace=getStackTrace(e) error=e.msg, stacktrace=getStackTrace(e)
(data.len, false) (dataCopy.len, false)
else: else:
(0, true) (0, true)