about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-06-07 13:15:17 +0200
committerbptato <nincsnevem662@gmail.com>2023-06-07 13:19:02 +0200
commitd78298ae3369374bf7bf31592513d112c75a9601 (patch)
treee8daf184adb0b09d48d53bf123f23a5d2a7b8d86
parentc15d04041b1b8660000a1517b9d4bfc06f2cb490 (diff)
downloadchawan-d78298ae3369374bf7bf31592513d112c75a9601.tar.gz
Clean up stream error handling
* sread now raises EOFError if readData returns too few bytes
* buffer handleError ignores unregistered fds like client
* client handleError ignores unregistered fds like buffer
-rw-r--r--src/buffer/buffer.nim12
-rw-r--r--src/display/client.nim2
-rw-r--r--src/ips/serialize.nim7
3 files changed, 13 insertions, 8 deletions
diff --git a/src/buffer/buffer.nim b/src/buffer/buffer.nim
index dc41ba6c..77772532 100644
--- a/src/buffer/buffer.nim
+++ b/src/buffer/buffer.nim
@@ -75,7 +75,6 @@ type
   Buffer* = ref object
     rfd: int # file descriptor of command pipe
     fd: int # file descriptor of buffer source
-    oldfd: int # fd after being unregistered
     alive: bool
     readbufsize: int
     contenttype: string
@@ -663,7 +662,7 @@ proc finishLoad(buffer: Buffer): EmptyPromise =
     p = EmptyPromise()
     p.resolve()
   buffer.selector.unregister(buffer.fd)
-  buffer.oldfd = buffer.fd
+  buffer.loader.unregistered.add(buffer.fd)
   buffer.fd = -1
   buffer.istream.close()
   return p
@@ -1133,8 +1132,8 @@ proc handleRead(buffer: Buffer, fd: int) =
   elif fd in buffer.loader.ongoing:
     #TODO something with readablestream?
     discard
-  elif buffer.fd == -1 and buffer.oldfd == fd:
-    discard #TODO hack
+  elif fd in buffer.loader.unregistered:
+    discard # ignore
   else: assert false
 
 proc handleError(buffer: Buffer, fd: int, err: OSErrorCode) =
@@ -1149,8 +1148,8 @@ proc handleError(buffer: Buffer, fd: int, err: OSErrorCode) =
   elif fd in buffer.loader.ongoing:
     #TODO something with readablestream?
     discard
-  elif buffer.fd == -1 and fd == buffer.oldfd:
-    discard #TODO hack
+  elif fd in buffer.loader.unregistered:
+    discard # ignore
   else:
     assert false, $fd & ": " & $err
 
@@ -1169,6 +1168,7 @@ proc runBuffer(buffer: Buffer, rfd: int) =
         assert buffer.window != nil
         assert buffer.window.timeouts.runTimeoutFd(event.fd)
         buffer.window.runJSJobs()
+    buffer.loader.unregistered.setLen(0)
   buffer.pstream.close()
   buffer.loader.quit()
   quit(0)
diff --git a/src/display/client.nim b/src/display/client.nim
index 456ca502..98b8d7b7 100644
--- a/src/display/client.nim
+++ b/src/display/client.nim
@@ -295,6 +295,8 @@ proc handleRead(client: Client, fd: int) =
   elif fd in client.loader.ongoing:
     #TODO something with readablestream?
     discard
+  elif fd in client.loader.unregistered:
+    discard # ignore
   else:
     let container = client.fdmap[fd]
     client.pager.handleEvent(container)
diff --git a/src/ips/serialize.nim b/src/ips/serialize.nim
index c2a6f5a6..beab3a5e 100644
--- a/src/ips/serialize.nim
+++ b/src/ips/serialize.nim
@@ -80,7 +80,8 @@ proc swrite*(stream: Stream, n: SomeNumber) =
   stream.write(n)
 
 proc sread*(stream: Stream, n: var SomeNumber) =
-  stream.read(n)
+  if stream.readData(addr n, sizeof(n)) < sizeof(n):
+    raise newException(EOFError, "eof")
 
 func slen*(n: SomeNumber): int =
   return sizeof(n)
@@ -122,7 +123,9 @@ proc sread*(stream: Stream, s: var string) =
   var len: int
   stream.sread(len)
   if len > 0:
-    stream.readStr(len, s)
+    s = newString(len)
+    if stream.readData(addr s[0], len) < len:
+      raise newException(EOFError, "eof")
   else:
     s = ""