Skip to content

Commit

Permalink
Update ev.c with workaround for failing chat server.
Browse files Browse the repository at this point in the history
2 issues:
- With poll backend, we were polling for writes even after we finished
  writing. Presents as wasting a lot of CPU.
- Fixes  strange closing behavior of chat server.
  • Loading branch information
bakpakin committed Sep 6, 2024
1 parent 5f70a85 commit f553c5d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 3 additions & 2 deletions examples/chatserver.janet
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

(defn handler
[connection]
(print "created " connection)
(print "connection: " connection)
(net/write connection "Whats your name?\n")
(def name (string/trim (string (ev/read connection 100))))
(print name " connected")
Expand All @@ -23,13 +23,14 @@
(put conmap name nil)
(:close connection))
(while (def msg (ev/read connection 100))
(broadcast name (string msg)))
(broadcast name (string msg)))
(print name " disconnected")))))

(defn main [& args]
(printf "STARTING SERVER...")
(flush)
(def my-server (net/listen "127.0.0.1" "8000"))
'(handler (net/accept my-server))
(forever
(def connection (net/accept my-server))
(ev/call handler connection)))
11 changes: 11 additions & 0 deletions src/core/ev.c
Original file line number Diff line number Diff line change
Expand Up @@ -2277,6 +2277,7 @@ void ev_callback_read(JanetFiber *fiber, JanetAsyncEvent event) {
break;
case JANET_ASYNC_EVENT_CLOSE:
janet_schedule(fiber, janet_wrap_nil());
stream->read_fiber = NULL;
janet_async_end(fiber);
break;
#ifdef JANET_WINDOWS
Expand Down Expand Up @@ -2361,6 +2362,7 @@ void ev_callback_read(JanetFiber *fiber, JanetAsyncEvent event) {
} else {
janet_schedule(fiber, janet_wrap_nil());
}
stream->read_fiber = NULL;
janet_async_end(fiber);
break;
}
Expand Down Expand Up @@ -2402,6 +2404,7 @@ void ev_callback_read(JanetFiber *fiber, JanetAsyncEvent event) {
nread = 0;
} else {
janet_cancel(fiber, janet_ev_lasterr());
stream->read_fiber = NULL;
janet_async_end(fiber);
break;
}
Expand All @@ -2411,6 +2414,7 @@ void ev_callback_read(JanetFiber *fiber, JanetAsyncEvent event) {
state->bytes_read += nread;
if (state->bytes_read == 0 && (state->mode != JANET_ASYNC_READMODE_RECVFROM)) {
janet_schedule(fiber, janet_wrap_nil());
stream->read_fiber = NULL;
janet_async_end(fiber);
break;
}
Expand All @@ -2434,6 +2438,7 @@ void ev_callback_read(JanetFiber *fiber, JanetAsyncEvent event) {
resume_val = janet_wrap_buffer(buffer);
}
janet_schedule(fiber, resume_val);
stream->read_fiber = NULL;
janet_async_end(fiber);
break;
}
Expand Down Expand Up @@ -2526,6 +2531,7 @@ void ev_callback_write(JanetFiber *fiber, JanetAsyncEvent event) {
}
case JANET_ASYNC_EVENT_CLOSE:
janet_cancel(fiber, janet_cstringv("stream closed"));
stream->write_fiber = NULL;
janet_async_end(fiber);
break;
#ifdef JANET_WINDOWS
Expand Down Expand Up @@ -2612,10 +2618,12 @@ void ev_callback_write(JanetFiber *fiber, JanetAsyncEvent event) {
#else
case JANET_ASYNC_EVENT_ERR:
janet_cancel(fiber, janet_cstringv("stream err"));
stream->write_fiber = NULL;
janet_async_end(fiber);
break;
case JANET_ASYNC_EVENT_HUP:
janet_cancel(fiber, janet_cstringv("stream hup"));
stream->write_fiber = NULL;
janet_async_end(fiber);
break;
case JANET_ASYNC_EVENT_INIT:
Expand Down Expand Up @@ -2653,13 +2661,15 @@ void ev_callback_write(JanetFiber *fiber, JanetAsyncEvent event) {
if (nwrote == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) break;
janet_cancel(fiber, janet_ev_lasterr());
stream->write_fiber = NULL;
janet_async_end(fiber);
break;
}

/* Unless using datagrams, empty message is a disconnect */
if (nwrote == 0 && !dest_abst) {
janet_cancel(fiber, janet_cstringv("disconnect"));
stream->write_fiber = NULL;
janet_async_end(fiber);
break;
}
Expand All @@ -2673,6 +2683,7 @@ void ev_callback_write(JanetFiber *fiber, JanetAsyncEvent event) {
state->start = start;
if (start >= len) {
janet_schedule(fiber, janet_wrap_nil());
stream->write_fiber = NULL;
janet_async_end(fiber);
break;
}
Expand Down

0 comments on commit f553c5d

Please sign in to comment.