@amritk/nish-aarch64-linux 0.44.0 → 0.46.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/INSTALL.md +3 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +18 -16
  4. package/package.json +1 -1
  5. package/runtime/core/numbers.mjs +3 -1
  6. package/runtime/core/strings.mjs +90 -38
  7. package/runtime/core.d.ts +8 -0
  8. package/runtime/core.mjs +16 -0
  9. package/runtime/nish.d.ts +88 -5
  10. package/runtime/nish.h +10 -0
  11. package/runtime/nish.mjs +5 -0
  12. package/runtime/runtime-host.c +15 -0
  13. package/runtime/runtime-net.c +223 -4
  14. package/runtime/runtime-os.c +15 -1
  15. package/runtime/runtime-uring.c +291 -31
  16. package/runtime/sqlite/nish-sqlite.c +301 -128
  17. package/std/README.md +15 -15
  18. package/std/array.ts +10 -2
  19. package/std/bytes.ts +239 -26
  20. package/std/cli-args.ts +1072 -606
  21. package/std/cli-flags.ts +180 -98
  22. package/std/cli.ts +21 -13
  23. package/std/collections.ts +245 -127
  24. package/std/compress/checksum.ts +32 -37
  25. package/std/compress/deflate.ts +1102 -542
  26. package/std/compress/gzip.ts +290 -145
  27. package/std/compress/zlib.ts +57 -21
  28. package/std/crypto/aes.ts +153 -75
  29. package/std/crypto/base64url.ts +84 -61
  30. package/std/crypto/bcrypt-batch.ts +164 -90
  31. package/std/crypto/bcrypt.ts +185 -107
  32. package/std/crypto/chacha20poly1305.ts +84 -68
  33. package/std/crypto/ct.ts +9 -7
  34. package/std/crypto/ed25519.ts +64 -27
  35. package/std/crypto/hkdf.ts +105 -63
  36. package/std/crypto/hmac.ts +95 -89
  37. package/std/crypto/jwt.ts +248 -126
  38. package/std/crypto/p256.ts +758 -444
  39. package/std/crypto/p384.ts +20 -13
  40. package/std/crypto/pki-roots.ts +95 -38
  41. package/std/crypto/pki.ts +164 -60
  42. package/std/crypto/rsa.ts +266 -223
  43. package/std/crypto/scram.ts +3 -4
  44. package/std/crypto/sha1.ts +107 -65
  45. package/std/crypto/sha256.ts +502 -422
  46. package/std/crypto/sha512.ts +204 -135
  47. package/std/crypto/x509-random.ts +9 -3
  48. package/std/crypto/x509.ts +331 -171
  49. package/std/json-equal.ts +35 -19
  50. package/std/json-format.ts +215 -108
  51. package/std/json-number.ts +126 -72
  52. package/std/json-pointer.ts +42 -13
  53. package/std/json-tokenizer.ts +498 -307
  54. package/std/json-typed.ts +115 -36
  55. package/std/json-value.ts +341 -152
  56. package/std/json.ts +300 -158
  57. package/std/net/buffer-pool.ts +17 -7
  58. package/std/net/dns-config.ts +427 -236
  59. package/std/net/dns-message.ts +114 -47
  60. package/std/net/dns-resolver.ts +736 -237
  61. package/std/net/dns.ts +205 -52
  62. package/std/net/fetch.ts +320 -184
  63. package/std/net/grpc.ts +948 -0
  64. package/std/net/handler.ts +1315 -222
  65. package/std/net/hpack.ts +395 -180
  66. package/std/net/http-compress.ts +502 -65
  67. package/std/net/http-fields.ts +85 -49
  68. package/std/net/http-server.ts +1764 -602
  69. package/std/net/http1-client-codec.ts +372 -161
  70. package/std/net/http1-codec.ts +665 -331
  71. package/std/net/http1-server.ts +660 -278
  72. package/std/net/http2-cleartext.ts +165 -95
  73. package/std/net/http2-frame.ts +232 -89
  74. package/std/net/http2-tls.ts +42 -12
  75. package/std/net/http2.ts +327 -101
  76. package/std/net/http3-frame.ts +68 -33
  77. package/std/net/http3-server.ts +647 -192
  78. package/std/net/http3.ts +667 -189
  79. package/std/net/oidc.ts +63 -24
  80. package/std/net/outbound.ts +207 -142
  81. package/std/net/postgres-async.ts +1165 -443
  82. package/std/net/postgres-carrier.ts +575 -372
  83. package/std/net/postgres.ts +1459 -559
  84. package/std/net/prefork.ts +254 -98
  85. package/std/net/protobuf.ts +1017 -0
  86. package/std/net/qpack.ts +143 -92
  87. package/std/net/quic-conn-ack.ts +50 -18
  88. package/std/net/quic-conn-cid.ts +66 -26
  89. package/std/net/quic-conn-params.ts +13 -7
  90. package/std/net/quic-datagram.ts +90 -27
  91. package/std/net/quic-frame.ts +225 -124
  92. package/std/net/quic-listener.ts +197 -89
  93. package/std/net/quic-packet.ts +289 -163
  94. package/std/net/quic-recovery.ts +279 -128
  95. package/std/net/quic-stream.ts +466 -167
  96. package/std/net/quic.ts +568 -257
  97. package/std/net/router.ts +450 -99
  98. package/std/net/serve.ts +651 -232
  99. package/std/net/static.ts +1458 -0
  100. package/std/net/tls/client.ts +140 -45
  101. package/std/net/tls/codec.ts +510 -260
  102. package/std/net/tls/record-client.ts +268 -76
  103. package/std/net/tls/record-server.ts +236 -79
  104. package/std/net/tls/record.ts +173 -47
  105. package/std/net/tls/schedule.ts +58 -39
  106. package/std/net/tls/ticket.ts +295 -103
  107. package/std/net/tls/verify.ts +1 -1
  108. package/std/net/tls-tcp.ts +218 -75
  109. package/std/net/tls.ts +260 -152
  110. package/std/net/upstream.ts +283 -86
  111. package/std/net/url.ts +277 -102
  112. package/std/net/websocket.ts +370 -186
  113. package/std/net/webtransport.ts +755 -436
  114. package/std/regex.ts +1659 -829
  115. package/std/sqlite.ts +171 -0
  116. package/std/sync/ring.ts +320 -156
  117. package/std/sync/shm.ts +53 -6
  118. package/std/text.ts +205 -124
  119. package/std/threads.ts +22 -2
@@ -76,6 +76,8 @@ typedef int nish_net_unused;
76
76
  #if defined(__linux__)
77
77
  #include <linux/filter.h>
78
78
  #include <netinet/udp.h>
79
+ #include <signal.h>
80
+ #include <stdio.h>
79
81
  #include <sys/epoll.h>
80
82
  #include <time.h>
81
83
  /* glibc before 2.29 names neither; the numbers are the kernel's ABI. */
@@ -292,6 +294,8 @@ int32_t nish_uring_open(struct nish_uring_set **out, int32_t listener, const str
292
294
  int32_t nish_uring_fd(const struct nish_uring_set *s);
293
295
  int32_t nish_uring_sleep_ms(const struct nish_uring_set *s, int32_t timeout_ms);
294
296
  int32_t nish_uring_wait(struct nish_uring_set *s, int32_t timeout_ms, struct nish_uring_event *ev, int32_t cap);
297
+ int32_t nish_uring_wait_direct(struct nish_uring_set *s, int32_t watch_fd, int32_t min_complete, int32_t timeout_ms);
298
+ int32_t nish_uring_poll_events(struct nish_uring_set *s, int32_t take);
295
299
  int32_t nish_uring_send(struct nish_uring_set *s, int32_t ring, int32_t slot, const uint8_t *p, int32_t n);
296
300
  int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot);
297
301
  int32_t nish_uring_shutdown(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t how);
@@ -299,6 +303,13 @@ int64_t nish_uring_queued(const struct nish_uring_set *s, int32_t ring, int32_t
299
303
  int32_t nish_uring_full(const struct nish_uring_set *s);
300
304
  int32_t nish_uring_hold(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t on);
301
305
  void nish_uring_destroy(struct nish_uring_set *s);
306
+ /* runtime-uring.c's counters; the dump below prints six of them. */
307
+ struct nish_uring_stats {
308
+ int64_t rings, live, accepting, slots, taskrun_reentries, overflow_drains, nobufs_rearms, send_requeues;
309
+ int64_t accept_errors, accept_pauses, bound_violations, max_accepting_rings, direct_waits, sends;
310
+ int64_t pool_new, pool_held, pool_cached;
311
+ };
312
+ void nish_uring_stats(const struct nish_uring_set *s, struct nish_uring_stats *st);
302
313
 
303
314
  #define NR_BASE (1 << 20)
304
315
  #define NR_RINGS 64
@@ -321,6 +332,7 @@ void nish_uring_destroy(struct nish_uring_set *s);
321
332
  the list of sources that are ready, kept in the order they became so. */
322
333
  struct nr_vent {
323
334
  int32_t loop, events, token;
335
+ uint32_t seen; /* the `nr_epoch` of the batch that last reported it */
324
336
  uint8_t listed, is_listener;
325
337
  struct nr_vent *prev, *next;
326
338
  };
@@ -590,9 +602,52 @@ static void nr_pump(struct nr_set *S) {
590
602
  S->npend = keep;
591
603
  }
592
604
 
605
+ /* The stats dump. With NISH_URING_STATS set to a value other than "" or
606
+ "0" when the first ring listener opens, SIGUSR1 asks for one line per
607
+ open set on stderr, printed by the next pump of the loop, and closing a
608
+ ring listener prints its set's line before the set goes. Without it no
609
+ handler is installed and SIGUSR1 keeps its default action. The handler
610
+ stays installed once set; with no set open, a signal prints nothing.
611
+ signalFd's pipe (runtime-host.c) handles only SIGINT and SIGTERM, so it
612
+ never claims SIGUSR1. */
613
+ static int nr_stats_on = -1; /* -1 until the first open reads the knob */
614
+ static volatile sig_atomic_t nr_stats_asked;
615
+
616
+ static void nr_stats_signal(int sig) {
617
+ (void)sig;
618
+ nr_stats_asked = 1;
619
+ }
620
+
621
+ static void nr_stats_setup(void) {
622
+ if (nr_stats_on >= 0) return;
623
+ const char *v = getenv("NISH_URING_STATS");
624
+ nr_stats_on = v && v[0] && !(v[0] == '0' && !v[1]);
625
+ if (!nr_stats_on) return;
626
+ struct sigaction sa;
627
+ memset(&sa, 0, sizeof sa);
628
+ sa.sa_handler = nr_stats_signal;
629
+ sa.sa_flags = SA_RESTART;
630
+ sigemptyset(&sa.sa_mask);
631
+ if (sigaction(SIGUSR1, &sa, NULL) != 0) nr_stats_on = 0;
632
+ }
633
+
634
+ static void nr_stats_print(const struct nr_set *S, const char *why) {
635
+ struct nish_uring_stats st;
636
+ nish_uring_stats(S->u, &st);
637
+ dprintf(2,
638
+ "uring-stats %s port=%d sends=%lld pool_new=%lld pool_held=%lld pool_cached=%lld live=%lld slots=%lld\n",
639
+ why, (int)S->port, (long long)st.sends, (long long)st.pool_new, (long long)st.pool_held,
640
+ (long long)st.pool_cached, (long long)st.live, (long long)st.slots);
641
+ }
642
+
593
643
  static void nr_pump_all(void) {
594
644
  for (int32_t i = 0; i < nr_nsets; i++)
595
645
  if (!nr_sets[i]->dead) nr_pump(nr_sets[i]);
646
+ if (nr_stats_asked) {
647
+ nr_stats_asked = 0;
648
+ for (int32_t i = 0; i < nr_nsets; i++)
649
+ if (!nr_sets[i]->dead) nr_stats_print(nr_sets[i], "signal");
650
+ }
596
651
  }
597
652
 
598
653
  static struct nr_set *nr_listener(int32_t fd) {
@@ -650,6 +705,7 @@ static int32_t nr_open(int32_t fd, int32_t port) {
650
705
  /* Only an open set decides for rings: a probe that passed before a set
651
706
  that ran short leaves the state at 0 (NURING-26). */
652
707
  nr_state = 1;
708
+ nr_stats_setup();
653
709
  S->listener = fd;
654
710
  S->port = port;
655
711
  S->slots = o.slots;
@@ -663,6 +719,7 @@ static int32_t nr_open(int32_t fd, int32_t port) {
663
719
  /* Closes a ring listener: the set and every slot in it, then the descriptor.
664
720
  The record stays, dead, so its handles answer -9. */
665
721
  static int32_t nr_close_listener(struct nr_set *S) {
722
+ if (nr_stats_on > 0) nr_stats_print(S, "close");
666
723
  nr_unregister(&S->v);
667
724
  for (int ri = 0; ri < NR_RINGS; ri++) {
668
725
  if (!S->conns[ri]) continue;
@@ -878,17 +935,23 @@ static int32_t nr_ctl(int32_t loop, int op, struct nr_vent *v, struct nr_set *S,
878
935
  /* Writes the loop's ready virtual sources into `out`, at most `max`, in the
879
936
  order they became ready. One reported and still ready goes to the tail,
880
937
  so a busy source does not starve the rest. */
881
- static int nr_collect(int32_t loop, int32_t *out, int max) {
938
+ static uint32_t nr_epoch;
939
+
940
+ static int nr_collect_from(int32_t loop, int32_t *out, int max, int skip_seen) {
882
941
  int n = 0;
883
942
  struct nr_vent *again = NULL, *again_tail = NULL;
884
943
  for (struct nr_vent *v = nr_head, *next; v && n < max; v = next) {
885
944
  next = v->next;
886
945
  if (v->loop != loop) continue;
946
+ /* A batch's second wait passes over what its first already reported,
947
+ which stays listed for the next call. */
948
+ if (skip_seen && v->seen == nr_epoch) continue;
887
949
  int32_t bits = nr_bits(v) & (v->events | 4);
888
950
  nr_unlist(v);
889
951
  if (bits == 0) continue;
890
952
  out[2 * n] = v->token;
891
953
  out[2 * n + 1] = bits;
954
+ v->seen = nr_epoch;
892
955
  n++;
893
956
  v->next = NULL;
894
957
  if (again_tail) again_tail->next = v; else again = v;
@@ -901,6 +964,8 @@ static int nr_collect(int32_t loop, int32_t *out, int max) {
901
964
  return n;
902
965
  }
903
966
 
967
+ static int nr_collect(int32_t loop, int32_t *out, int max) { return nr_collect_from(loop, out, max, 0); }
968
+
904
969
  static int64_t nr_now_ms(void) {
905
970
  struct timespec t;
906
971
  clock_gettime(CLOCK_MONOTONIC, &t);
@@ -910,6 +975,72 @@ static int64_t nr_now_ms(void) {
910
975
  /* Whose turn a full wait with room for one gives: 1 the epoll set's. */
911
976
  static int nr_turn;
912
977
 
978
+ /* The ordinary descriptors each loop watches, as `pollAdd` and `pollModify`
979
+ left them, so a wait can tell a loop that holds nothing but ring slots and
980
+ `signalFd()` from one that must sleep in epoll_wait. A close without
981
+ `pollRemove` may leave an entry behind, which only keeps that loop on
982
+ epoll; past NR_FDS entries no loop waits on its ring directly. */
983
+ #define NR_FDS 64
984
+ static struct {
985
+ int32_t loop, fd, events;
986
+ } nr_fds[NR_FDS];
987
+ static int32_t nr_nfds, nr_fds_spilled;
988
+
989
+ static void nr_fd_note(int32_t loop, int op, int32_t fd, int32_t events) {
990
+ int32_t at = -1;
991
+ for (int32_t i = 0; i < nr_nfds; i++)
992
+ if (nr_fds[i].loop == loop && nr_fds[i].fd == fd) at = i;
993
+ if (op == EPOLL_CTL_DEL) {
994
+ if (at >= 0) nr_fds[at] = nr_fds[--nr_nfds];
995
+ return;
996
+ }
997
+ if (at < 0) {
998
+ if (nr_nfds == NR_FDS) {
999
+ nr_fds_spilled = 1;
1000
+ return;
1001
+ }
1002
+ at = nr_nfds++;
1003
+ nr_fds[at].loop = loop;
1004
+ nr_fds[at].fd = fd;
1005
+ }
1006
+ nr_fds[at].events = events;
1007
+ }
1008
+
1009
+ /* `fd` is closed or new: it watches nothing and is in no loop. */
1010
+ static void nr_fd_forget(int32_t fd) {
1011
+ for (int32_t i = 0; i < nr_nfds;)
1012
+ if (nr_fds[i].loop == fd || nr_fds[i].fd == fd) nr_fds[i] = nr_fds[--nr_nfds];
1013
+ else i++;
1014
+ }
1015
+
1016
+ /* The set a wait on `loop` may sleep on directly, in io_uring_enter rather
1017
+ than epoll_wait, and in `*watch` the one descriptor its ring must watch
1018
+ for it, or -1: there is one live set, no other loop holds a virtual
1019
+ source, and the loop's only ordinary descriptor, if any, is `signalFd()`
1020
+ watched for reading. NULL otherwise, and the wait uses epoll. Once
1021
+ `netClose` or `fileClose` has closed the signal descriptor its number may
1022
+ name a new socket, which the ring's poll, still armed on the old pipe,
1023
+ would never report; `nish_signal_fd_made` answers -1 from then on, so no
1024
+ loop with an ordinary descriptor waits directly again. */
1025
+
1026
+ static struct nr_set *nr_direct(int32_t loop, int32_t *watch) {
1027
+ if (nr_fds_spilled || nr_loops_spilled || nr_nloops != 1 || nr_loops[0] != loop) return NULL;
1028
+ struct nr_set *D = NULL;
1029
+ for (int32_t i = 0; i < nr_nsets; i++) {
1030
+ if (nr_sets[i]->dead) continue;
1031
+ if (D) return NULL;
1032
+ D = nr_sets[i];
1033
+ }
1034
+ *watch = -1;
1035
+ for (int32_t i = 0; i < nr_nfds; i++) {
1036
+ if (nr_fds[i].loop != loop) continue;
1037
+ if (*watch >= 0 || nr_fds[i].fd != nish_signal_fd_made() || nr_fds[i].events != 1)
1038
+ return NULL;
1039
+ *watch = nr_fds[i].fd;
1040
+ }
1041
+ return D;
1042
+ }
1043
+
913
1044
  /* `pollWait` for a loop that may hold virtual sources: pump every set, take
914
1045
  what is ready, and sleep on the loop's epoll set, which holds each set's
915
1046
  descriptor, no longer than the sets allow (NURING-8). A wake by the bound,
@@ -933,8 +1064,24 @@ static int32_t nr_poll_wait(int32_t loop, int32_t *out, int max, int32_t timeout
933
1064
  int room = max - n;
934
1065
  if (room == 0) room = max > 1 ? max / 2 : (nr_turn ^= 1);
935
1066
  struct epoll_event e[NR_EVENTS];
936
- int k = room > 0 ? epoll_wait(loop, e, room, t) : 0;
937
- int woke = 0;
1067
+ int woke = 0, k = -2, watch;
1068
+ /* A loop of ring slots and at most `signalFd()` sleeps on its ring, and
1069
+ reads its epoll set only once the ring's poll says the signal
1070
+ descriptor is ready; that read takes the place of NURING-18's share. */
1071
+ struct nr_set *D = room > 0 ? nr_direct(loop, &watch) : NULL;
1072
+ if (D) {
1073
+ int rc = nish_uring_wait_direct(D->u, watch, 1, t);
1074
+ if (rc != -EOPNOTSUPP) {
1075
+ if (rc < 0) {
1076
+ errno = -rc;
1077
+ return nish_net_fail();
1078
+ }
1079
+ nr_pump(D);
1080
+ woke = 1;
1081
+ k = watch >= 0 && nish_uring_poll_events(D->u, 1) ? epoll_wait(loop, e, room, 0) : 0;
1082
+ }
1083
+ }
1084
+ if (k == -2) k = room > 0 ? epoll_wait(loop, e, room, t) : 0;
938
1085
  /* io_uring's task work interrupts the wait with no signal behind it, so
939
1086
  EINTR is a wake to pump for, not an answer of 0 before the timeout. */
940
1087
  if (k < 0 && errno != EINTR) return nish_net_fail();
@@ -967,6 +1114,51 @@ static int32_t nr_poll_wait(int32_t loop, int32_t *out, int max, int32_t timeout
967
1114
  }
968
1115
  #endif
969
1116
 
1117
+ #if defined(__linux__)
1118
+ /* A batch's second wait: `n` pairs are in `out`, and it waits on the one live
1119
+ set's ring, up to `max_wait_ms`, until `min` are ready or `max` fill it. It
1120
+ reads no epoll set, so an ordinary descriptor that becomes ready meanwhile
1121
+ waits for the next call, at most `max_wait_ms`; on a direct loop the
1122
+ ring's poll on `signalFd()` ends the batch as soon as it answers, and the
1123
+ next call reports it. */
1124
+ static int nr_batch(int32_t loop, int32_t *out, int n, int max, int32_t min, int32_t max_wait_ms) {
1125
+ struct nr_set *S = NULL;
1126
+ for (int32_t i = 0; i < nr_nsets; i++) {
1127
+ if (nr_sets[i]->dead) continue;
1128
+ if (S) return n; /* a batch waits on one ring set, never on several */
1129
+ S = nr_sets[i];
1130
+ }
1131
+ if (!S) return n;
1132
+ /* A loop no virtual source has joined has nothing the ring can add, so it
1133
+ answers as `pollWait` did rather than sleep out `max_wait_ms`. */
1134
+ int held = nr_loops_spilled;
1135
+ for (int32_t i = 0; i < nr_nloops; i++)
1136
+ if (nr_loops[i] == loop) held = 1;
1137
+ if (!held) return n;
1138
+ int32_t watch = -1;
1139
+ int direct = nr_direct(loop, &watch) == S;
1140
+ int64_t end = nr_now_ms() + max_wait_ms;
1141
+ while (n < min && n < max) {
1142
+ int64_t left = end - nr_now_ms();
1143
+ if (left <= 0) break;
1144
+ int rc = direct ? nish_uring_wait_direct(S->u, watch, min - n, (int32_t)left) : -EOPNOTSUPP;
1145
+ if (rc == -EOPNOTSUPP) {
1146
+ struct nish_uring_event ev[NR_EVENTS];
1147
+ int k = nish_uring_wait(S->u, (int32_t)left, ev, NR_EVENTS);
1148
+ if (k < 0) break;
1149
+ for (int i = 0; i < k; i++) nr_dispatch(S, &ev[i]);
1150
+ } else if (rc < 0) {
1151
+ break;
1152
+ }
1153
+ nr_pump(S);
1154
+ n += nr_collect_from(loop, out + 2 * n, max - n, 1);
1155
+ if (direct && watch >= 0 && nish_uring_poll_events(S->u, 0)) break;
1156
+ }
1157
+ return n;
1158
+ }
1159
+ #endif
1160
+
1161
+
970
1162
  /* `netRingState()`: whether `tcpListenFlags` flag 8 gets rings. 0 before a
971
1163
  listener has asked, 1 when it got them, or the negative errno that refused
972
1164
  them, after which every listener in the process is a descriptor one: -1
@@ -1339,6 +1531,8 @@ int32_t nish_net_close(int32_t fd) {
1339
1531
  struct nr_set *R = nr_nsets > 0 ? nr_listener(fd) : NULL;
1340
1532
  if (R) return nr_close_listener(R);
1341
1533
  if (nr_nsets > 0) nr_forget_loop(fd);
1534
+ nr_fd_forget(fd);
1535
+ nish_signal_fd_closing(fd);
1342
1536
  #endif
1343
1537
  return close(fd) != 0 ? nish_net_fail() : 0;
1344
1538
  }
@@ -1574,6 +1768,7 @@ int32_t nish_poll_create(void) {
1574
1768
  nish_net_raise_nofile();
1575
1769
  #if defined(__linux__)
1576
1770
  int fd = epoll_create1(EPOLL_CLOEXEC);
1771
+ if (fd >= 0) nr_fd_forget(fd);
1577
1772
  #else
1578
1773
  int fd = kqueue();
1579
1774
  if (fd >= 0) fcntl(fd, F_SETFD, FD_CLOEXEC);
@@ -1619,7 +1814,9 @@ static int32_t nish_poll_ctl(int32_t loop, int op, int32_t fd, int32_t events, i
1619
1814
  e.events = ((events & 1) ? EPOLLIN : 0) | ((events & 2) ? EPOLLOUT : 0);
1620
1815
  /* The high word is 0 for a descriptor, and 1 for a ring set's own. */
1621
1816
  e.data.u64 = (uint32_t)token;
1622
- return epoll_ctl(loop, op, fd, &e) != 0 ? nish_net_fail() : 0;
1817
+ if (epoll_ctl(loop, op, fd, &e) != 0) return nish_net_fail();
1818
+ nr_fd_note(loop, op, fd, events);
1819
+ return 0;
1623
1820
  #else
1624
1821
  int r, w;
1625
1822
  if (op == NISH_POLL_ADD) {
@@ -1709,6 +1906,28 @@ int32_t nish_poll_wait(int32_t loop, nish_array *ready, int32_t timeout_ms) {
1709
1906
  #endif
1710
1907
  }
1711
1908
 
1909
+ /* `pollWaitBatch(loop, ready, timeoutMs, minEvents, maxWaitMs)`: wait as
1910
+ `pollWait` does for the first event, then, on a loop with one ring set,
1911
+ up to `maxWaitMs` more until `minEvents` are ready (nr_batch). */
1912
+ int32_t nish_poll_wait_batch(int32_t loop, nish_array *ready, int32_t timeout_ms, int32_t min_events,
1913
+ int32_t max_wait_ms) {
1914
+ #if defined(__linux__)
1915
+ uint64_t room = ready->len / 2;
1916
+ if (nr_nsets > 0 && min_events > 1 && max_wait_ms > 0 && room > 0) {
1917
+ int max = room < NISH_POLL_CHUNK ? (int)room : NISH_POLL_CHUNK;
1918
+ int32_t *out = (int32_t *)ready->data;
1919
+ nr_epoch++;
1920
+ int n = nr_poll_wait(loop, out, max, timeout_ms);
1921
+ if (n <= 0 || n >= min_events || n >= max) return n;
1922
+ return nr_batch(loop, out, n, max, min_events, max_wait_ms);
1923
+ }
1924
+ #else
1925
+ (void)min_events;
1926
+ (void)max_wait_ms;
1927
+ #endif
1928
+ return nish_poll_wait(loop, ready, timeout_ms);
1929
+ }
1930
+
1712
1931
  /* The Unix-domain address `path` names into `s`, its length into `n`, and 1
1713
1932
  or 0 for whether it is abstract; or the failure, before any socket exists.
1714
1933
  An empty path or one with a NUL inside is -22, and so is a bare `@`. A path
@@ -324,9 +324,23 @@ int64_t nish_file_size(int32_t fd) {
324
324
  return fstat(fd, &st) == 0 ? (int64_t)st.st_size : nish_fs_err(errno);
325
325
  }
326
326
 
327
+ #if !defined(__wasi__) && !defined(__wasm__)
328
+ /* Weak, so that runtime.c, runtime-text.c and runtime-os.c still link as a set
329
+ of their own: runtime-host.c defines it, and is linked wherever `signalFd()`
330
+ can have made the pipe this hook watches for. */
331
+ extern void nish_signal_fd_closing(int32_t fd) __attribute__((weak));
332
+ #endif
333
+
327
334
  /* `fileClose(fd)`: never retried on `EINTR`, because Linux has released the
328
335
  number by then and a retry could close a descriptor opened since. */
329
- int32_t nish_file_close(int32_t fd) { return fd < 0 ? -9 : close(fd) == 0 ? 0 : nish_fs_err(errno); }
336
+ int32_t nish_file_close(int32_t fd) {
337
+ if (fd < 0) return -9;
338
+ #if !defined(__wasi__) && !defined(__wasm__)
339
+ /* The signal pipe's number is about to be free for reuse (NURING-49). */
340
+ if (nish_signal_fd_closing) nish_signal_fd_closing(fd);
341
+ #endif
342
+ return close(fd) == 0 ? 0 : nish_fs_err(errno);
343
+ }
330
344
 
331
345
  /* ---- Files and directories with an explicit mode (docs/security/runtime.md,
332
346
  RT-24 to RT-26): `writeFileSyncMode`, `createFileExclusiveSync` and