about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-05-09 12:34:58 +0200
committerbptato <nincsnevem662@gmail.com>2023-05-09 12:34:58 +0200
commit87f9bd656b2a8a8d4ebd029ba6a78f1dc93558eb (patch)
treec3b7e4f3500512baef2064986cfdf5701c988e29 /src
parente0a8b27068ac8fc61a243bae685c23a14976f619 (diff)
downloadchawan-87f9bd656b2a8a8d4ebd029ba6a78f1dc93558eb.tar.gz
Improve debugging, reduce crashes
Loader no longer dies when not everything is read from the stream.
Diffstat (limited to 'src')
-rw-r--r--src/display/client.nim24
-rw-r--r--src/io/http.nim6
-rw-r--r--src/io/loader.nim14
-rw-r--r--src/main.nim7
4 files changed, 44 insertions, 7 deletions
diff --git a/src/display/client.nim b/src/display/client.nim
index 59557f36..cd2c9f5d 100644
--- a/src/display/client.nim
+++ b/src/display/client.nim
@@ -330,10 +330,19 @@ proc handleRead(client: Client, fd: int) =
     if s.len > prefix.len:
       client.console.err.write(s)
     client.console.err.flush()
+  elif fd in client.loader.connecting:
+    client.loader.onConnected(fd)
+    client.runJSJobs()
+  elif fd in client.loader.ongoing:
+    #TODO something with readablestream?
+    discard
   else:
     let container = client.fdmap[fd]
     client.pager.handleEvent(container)
 
+proc flushConsole*(client: Client) {.jsfunc.} =
+  client.handleRead(client.dispatcher.forkserver.estream.fd)
+
 proc handleError(client: Client, fd: int) =
   if client.console.tty != nil and fd == client.console.tty.getFileHandle():
     #TODO do something here...
@@ -343,6 +352,12 @@ proc handleError(client: Client, fd: int) =
     #TODO do something here...
     stderr.write("Fork server crashed :(\n")
     quit(1)
+  elif fd in client.loader.connecting:
+    #TODO handle error?
+    discard
+  elif fd in client.loader.ongoing:
+    #TODO something with readablestream?
+    discard
   else:
     if fd in client.fdmap:
       let container = client.fdmap[fd]
@@ -392,8 +407,15 @@ proc inputLoop(client: Client) =
     client.pager.showAlerts()
     client.pager.draw()
 
+func hasSelectFds(client: Client): bool =
+  return client.timeouts.len > 0 or
+    client.intervals.len > 0 or
+    client.pager.numload > 0 or
+    client.loader.connecting.len > 0 or
+    client.loader.ongoing.len > 0
+
 proc headlessLoop(client: Client) =
-  while client.timeouts.len + client.intervals.len != 0 or client.pager.numload > 0:
+  while client.hasSelectFds():
     let events = client.selector.select(-1)
     for event in events:
       if Read in event.events:
diff --git a/src/io/http.nim b/src/io/http.nim
index bd880445..60387faf 100644
--- a/src/io/http.nim
+++ b/src/io/http.nim
@@ -72,8 +72,10 @@ proc curlWriteHeader(p: cstring, size: csize_t, nitems: csize_t, userdata: point
 proc curlWriteBody(p: cstring, size: csize_t, nmemb: csize_t, userdata: pointer): csize_t {.cdecl.} =
   let handleData = cast[HandleData](userdata)
   if nmemb > 0:
-    handleData.ostream.writeData(p, int(nmemb))
-    handleData.ostream.flush()
+    try:
+      handleData.ostream.writeData(p, int(nmemb))
+    except IOError: # Broken pipe
+      return 0
   return nmemb
 
 proc applyPostBody(curl: CURL, request: Request, handleData: HandleData) =
diff --git a/src/io/loader.nim b/src/io/loader.nim
index 2cb2b295..15b8e3e2 100644
--- a/src/io/loader.nim
+++ b/src/io/loader.nim
@@ -94,6 +94,7 @@ proc onLoad(ctx: LoaderContext, stream: Stream) =
   if not ctx.config.filter.match(request.url):
     stream.swrite(-1) # error
     stream.flush()
+    stream.close()
   else:
     for k, v in ctx.config.defaultHeaders.table:
       if k notin request.headers.table:
@@ -131,8 +132,11 @@ proc acceptConnection(ctx: LoaderContext) =
 
 proc finishCurlTransfer(ctx: LoaderContext, handleData: HandleData, res: int) =
   if res != int(CURLE_OK):
-    handleData.ostream.swrite(int(res))
-    handleData.ostream.flush()
+    try:
+      handleData.ostream.swrite(int(res))
+      handleData.ostream.flush()
+    except IOError: # Broken pipe
+      discard
   discard curl_multi_remove_handle(ctx.curlm, handleData.curl)
   handleData.ostream.close()
   handleData.cleanup()
@@ -234,7 +238,11 @@ proc fetch*(loader: FileLoader, input: Request): Promise[Response] =
   let fd = int(stream.source.getFd())
   loader.registerFun(fd)
   let promise = Promise[Response]()
-  loader.connecting[fd] = ConnectData(promise: promise, request: input)
+  loader.connecting[fd] = ConnectData(
+    promise: promise,
+    request: input,
+    stream: stream
+  )
 
 proc newResponse(res: int, request: Request, stream: Stream = nil): Response =
   return Response(
diff --git a/src/main.nim b/src/main.nim
index 7786909e..a0a6b064 100644
--- a/src/main.nim
+++ b/src/main.nim
@@ -124,4 +124,9 @@ conf.nmap = constructActionTable(conf.nmap)
 conf.lemap = constructActionTable(conf.lemap)
 disp.forkserver.loadForkServerConfig(conf)
 
-newClient(conf, disp).launchClient(pages, ctype, dump)
+let c = newClient(conf, disp)
+try:
+  c.launchClient(pages, ctype, dump)
+except CatchableError:
+  c.flushConsole()
+  raise