Giter Site home page Giter Site logo

Comments (11)

psagers avatar psagers commented on May 25, 2024 2

I'm experiencing similar behavior on Arch with glibc. This is Janet version 1.34.0-f92f3eb6, built from the repo. I don't see the issue on FreeBSD with an equivalent build (from the same commit).

Here's my repro setup:

(var n-req 0)

(defn handler
  [stream]
  (defer (net/close stream)
    (ev/sleep (math/random))
    (set n-req (inc n-req))
    (printf "%d: %N" n-req (net/peername stream))
    (net/write stream (string/format "%3d: Accepted\n" n-req))))

(defn main
  [&]
  (let [server (net/listen "127.0.0.1" 9000)]
    (net/accept-loop server handler)))

In another shell:

for i in $(seq 100)
do nc -d -w 2 127.0.0.1 9000 &
done

Once the accept-loop gets "stuck", the symptom I see is that some (but not all) connections are accepted and sent to the handler and some of those (but not all) result in data actually being written back to the client. The issue seems to accumulate, such that successive batches of 100 requests result in fewer and fewer successful responses.

I tried (forever (ev/call handler (net/accept server))) and that does seem to work correctly for me as well.

Updated: I see the same behavior with the official build as well.

from janet.

psagers avatar psagers commented on May 25, 2024 2

This appears to be working for me. Thanks!

from janet.

l63l avatar l63l commented on May 25, 2024 2

musl builds are also fixed. Thanks.

from janet.

bakpakin avatar bakpakin commented on May 25, 2024 1

POC fix that just make all server sockets run in level trigger mode (so not a "real" solution as it would cause a busy loop in the case with (forever (net/accept ...))

diff --git a/src/core/ev.c b/src/core/ev.c
index 335b9738..e373c3f1 100644
--- a/src/core/ev.c
+++ b/src/core/ev.c
@@ -1543,8 +1543,12 @@ static JanetTimestamp ts_now(void) {
 /* Wait for the next event */
 static void janet_register_stream(JanetStream *stream) {
     struct epoll_event ev;
-    ev.events = EPOLLET;
-    if (stream->flags & (JANET_STREAM_READABLE | JANET_STREAM_ACCEPTABLE)) ev.events |= EPOLLIN;
+    if (stream->flags & JANET_STREAM_ACCEPTABLE) {
+        ev.events = EPOLLIN; /* Do NOT edge trigger server sockets */
+    } else {
+        ev.events = EPOLLET;
+    }
+    if (stream->flags & JANET_STREAM_READABLE) ev.events |= EPOLLIN;
     if (stream->flags & JANET_STREAM_WRITABLE) ev.events |= EPOLLOUT;
     ev.data.ptr = stream;
     int status;
@@ -1695,7 +1699,9 @@ static void timestamp2timespec(struct timespec *t, JanetTimestamp ts) {
 void janet_register_stream(JanetStream *stream) {
     struct kevent kevs[2];
     int length = 0;
-    if (stream->flags & (JANET_STREAM_READABLE | JANET_STREAM_ACCEPTABLE)) {
+    if (stream->flags & JANET_STREAM_ACCEPTABLE) {
+        EV_SETx(&kevs[length++], stream->handle, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, stream);
+    } else if (stream->flags & JANET_STREAM_READABLE) {
         EV_SETx(&kevs[length++], stream->handle, EVFILT_READ, EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, stream);
     }
     if (stream->flags & JANET_STREAM_WRITABLE) {

from janet.

bakpakin avatar bakpakin commented on May 25, 2024 1

@psagers, @l63l or @pepe, let me know if this fixes the problem (especially the musl builds that I can't easily verify) and we can close this out.

from janet.

pepe avatar pepe commented on May 25, 2024

Hello. I am using musl build on Alpine, and I have yet to notice the behavior you are describing. I will try your reproduction code and report back. Thanks for reporting.

from janet.

pepe avatar pepe commented on May 25, 2024

I have tried it with the latest Janet from the master branch on Alpine Linux with musl at the time of this writing and see no issues like the one you are describing. We will need more information about your setup.

from janet.

l63l avatar l63l commented on May 25, 2024

hm, thanks for testing. I tested on void-linux with host musl-libc (1.1.24), alpine 3.17 vm (musl 1.2.3) and debian bullseye with musl-gcc. Now I fetched alpine 3.19.2 and build janet from master and could reproduce it, too.

You need to keep the "server snippet" running, start the "client snippet", kill the "client snippet" and the second invocation will get stuck.

from janet.

pepe avatar pepe commented on May 25, 2024

Oh. I did not get the process right. I am trying it again.

from janet.

pepe avatar pepe commented on May 25, 2024

Yep. I can confirm that the problem manifests in this case on Alpine.

from janet.

bakpakin avatar bakpakin commented on May 25, 2024

So took another look at this now that I have repro that works on my system, and the root cause is in how we setup epoll/kqueue in edge trigger mode.

This works well for most cases, but for net/accept-loop, this can cause connections to be missed in these micro benchmarks where we make many connections back to back.

The fix is conceptual fairly simple, but I would like to make server sockets use edge trigger mode unless we call net/accept-loop, in which they switch to level trigger mode and make sure we fix kqueue too, which seems to have the same wrong logic here.

from janet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.