nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.23k stars 1.47k forks source link

Reading zero or negative byte with asyncfile leads to different behavior in linux and windows #15150

Closed ringabout closed 2 years ago

ringabout commented 3 years ago

Related: #15148

echo "123" >> test.txt

block:
  proc saveEmpty() {.async.} =
    let
      file = open_async("test.txt", fmRead)
    discard await file.read(0)
    file.close()

  waitFor saveEmpty()

Current Output

in linux

Exception message: index out of bounds, the container is empty
Exception type: [IndexError]

in windows

Error: unhandled exception: D:\QQPCmgr\Desktop\Nim\lib\pure\asyncfile.nim(219, 20) `bytesCount > 0`  [AssertionDefect]
Error: execution of an external program failed: 'D:\QQPCmgr\Desktop\Nim\test.exe '

Expected

compile or raise assert error
Araq commented 2 years ago

Neither the assertion nor the IndexError are supposed to be caught anyway so I don't see a problem here. You shall not continue to run your program after a detected bug.

konsumlamm commented 2 years ago

Neither the assertion nor the IndexError are supposed to be caught anyway so I don't see a problem here. You shall not continue to run your program after a detected bug.

Wouldn't it be better to get an assertion failure that works the same on all platform than to get some internal error? I don't think anyone wants to catch the exception.

ringabout commented 2 years ago

Exception message: index out of bounds, the container is empty Exception type: [IndexError]

Doesn't this mean there is a bug in stdlib? I forgot to say "test.txt" is not empty.

Full error messages:

/root/prologue/hello.nim(10) hello
/root/.choosenim/toolchains/nim-#devel/lib/pure/asyncmacro.nim(262) saveEmpty
/root/.choosenim/toolchains/nim-#devel/lib/pure/asyncmacro.nim(29) saveEmptyNimAsyncContinue
/root/prologue/hello.nim(7) saveEmptyIter
/root/.choosenim/toolchains/nim-#devel/lib/pure/asyncfile.nim(292) read
/root/.choosenim/toolchains/nim-#devel/lib/pure/asyncfile.nim(276) cb
/root/.choosenim/toolchains/nim-#devel/lib/system/fatal.nim(53) sysFatal
[[reraised from:
/root/prologue/hello.nim(10) hello
/root/.choosenim/toolchains/nim-#devel/lib/pure/asyncdispatch.nim(1937) waitFor
/root/.choosenim/toolchains/nim-#devel/lib/pure/asyncfutures.nim(375) read
]]
Error: unhandled exception: index out of bounds, the container is empty
Async traceback:
  /root/prologue/hello.nim(10)                                        hello
  /root/.choosenim/toolchains/nim-#devel/lib/pure/asyncmacro.nim(262) saveEmpty
  /root/.choosenim/toolchains/nim-#devel/lib/pure/asyncmacro.nim(29)  saveEmptyNimAsyncContinue
  /root/prologue/hello.nim(7)                                         saveEmptyIter
  /root/.choosenim/toolchains/nim-#devel/lib/pure/asyncfile.nim(292)  read
  /root/.choosenim/toolchains/nim-#devel/lib/pure/asyncfile.nim(276)  cb
  /root/.choosenim/toolchains/nim-#devel/lib/system/fatal.nim(53)     sysFatal
Exception message: index out of bounds, the container is empty
Exception type: [IndexDefect]