Comments (11)
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.
This appears to be working for me. Thanks!
from janet.
musl builds are also fixed. Thanks.
from janet.
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.
@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.
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.
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.
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.
Oh. I did not get the process right. I am trying it again.
from janet.
Yep. I can confirm that the problem manifests in this case on Alpine.
from janet.
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)
- Proposal: Terse Chained Indices HOT 19
- Reimplement `slurp` with `os/open`. HOT 28
- A way to read standard input in the background without `file/read` in a thread. HOT 4
- `each` macro improper behavior? HOT 8
- `if-let` breaks tail call optimization. HOT 6
- false branch of `if-let` doesn't report the correct call stack information. HOT 4
- `ev/select` should not resume a dead task. HOT 4
- `try` and `defer` break tail call optimization. HOT 4
- `(= @"update" @"update")` is `false`. HOT 3
- `ev/select` still revives fibers.... HOT 2
- option to unbundle docstrings and potential consequences HOT 8
- splicing into structs/tables HOT 2
- Disasm/asm doesn't round-trip for function with unused argument HOT 1
- Consider making `module/paths` a dynamic variable HOT 6
- Can it run on iOS? Thank you. HOT 3
- `os/strftime` doesn't respect environment variables. HOT 3
- Re-integrate jpm into janet HOT 43
- recent change kinda breaks lexical scope with multiple modules HOT 2
- How to switch off spork/sh/copy message? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from janet.