janet-lang / circlet

HTTP server library for janet
Other
81 stars 10 forks source link

Using os module functions causes root fiber to panic #17

Open eko234 opened 2 years ago

eko234 commented 2 years ago

Hi, I'm trying to write a little web app on circlet and I'm trying to spawn some processes like this one when I receive a request to certain endpoint.

(defn getls
  "gets ls from bin dir" []
  (def procc (os/spawn ["ls"] :p {:in :pipe :out :pipe :err :pipe}))
  (pp :waiting_for_proc)
  (pp (:wait procc))
  [(:read (procc :out) :all)
   (:read (procc :err) :all)])

the first time I get the request nothing happens, but when I try to request again I got an error that says: current fiber is already waiting for event

could you explain me why this happens? is this a bug or maybe I'm not familiar enough with how things work in Janet or with Circlet

eko234 commented 2 years ago

this is the code of the application for reference.

(import circlet)

(defn getls
  "gets ls from bin dir" []
  (def procc (os/spawn ["ls"] :p {:in :pipe :out :pipe :err :pipe}))
  (pp :waiting_for_proc)
  (pp (:wait procc))
  [(:read (procc :out) :all)
   (:read (procc :err) :all)])

(defn app
  [req]
  (try
   (do
     (def channel (ev/thread-chan 1))
     (ev/thread (fiber/new (fn [] (ev/give channel (getls)))) :t)
     (def result (ev/take channel))
     {:status 200
      :headers {"Content-Type" "text/html"}
      :body result})
   ([err] 
    (print :WE_GOT_AN_ERROR)
    (print err)
    {:status 500
      :headers {"Content-Type" "text/html"}
      :body :OOPS})))

(defn main "entry point" [& args]
  (circlet/server app 8087))
eko234 commented 2 years ago

on further debugging, i've found that the problem occurs when I read the streams

eko234 commented 2 years ago

looks like whenever I try to sync threads, read from other processes something errors out or gets stuck...

eko234 commented 2 years ago

it must be something with the way circlet handles threads/fibers, doing the same in the joy framework works well, anyhow I would really want to use circlet and understand why this happens.

harryvederci commented 2 years ago

@eko234 I'm running into similar issues, I think. Have you managed to find a workaround?

eko234 commented 2 years ago

@eko234 I'm running into similar issues, I think. Have you managed to find a workaround?

Hi fren, you should enter the chat at gitter, backpakin answered me there about it (If I remember correctly), Circlet indeed has some issues with the way it handles threads and thus using fibers or threads directly may lead to these kind of issues, I think in a future this will be fixed (and if I remember well, it has to do with the development of libuv bindings for janet) and I really hope so, but on the other hand there are other libraries you can use for now, I have used both spork for implementing a barebones rpc task manager and the joy framework which says in it description to be "maximalist", but that works pretty well and doesn't even get in my way for it has indeed a very simple and composable api, I use it in production btw.

ChanderG commented 1 year ago

I am facing something similar when I try to use spork's http/request from a circlet handler. Anyway, I have switched to using the the same lib's http/server and that works correctly.

pepe commented 1 year ago

I would say circlet is not updated for latest Janet. Please do use spork.