@amritk/nish-aarch64-linux 0.40.1 → 0.42.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.
@@ -83,10 +83,12 @@
83
83
  * `preforkUdpCount(args)` (`--nish-prefork-udp=<fd>,<N>`, before the listener
84
84
  * flag), and sets `QuicServerConfig.workerIndex` to k and `.workerCount` to N
85
85
  * so every ID it issues starts with k. A connection then stays with its
86
- * worker when the client's address or port changes. More than 255 workers
87
- * panics; where the kernel refuses the steering (off Linux, or under a
88
- * seccomp profile that refuses it) the supervisor says so on stderr and runs
89
- * one worker.
86
+ * worker when the client's address or port changes. Port 0 is a port no UDP
87
+ * or TCP socket holds when the group binds, never another reuseport group's,
88
+ * which a port-0 bind with `SO_REUSEPORT` can be given on Linux. More than
89
+ * 255 workers panics; where the kernel refuses the steering (off Linux, or
90
+ * under a seccomp profile that refuses it) the supervisor says so on stderr
91
+ * and runs one worker.
90
92
  *
91
93
  * **How many.** `NISH_WORKERS` when it is a positive whole number, else
92
94
  * `PreforkConfig.workers` when that is positive, else
@@ -454,6 +456,40 @@ export const preforkUdpCount = (args: string[]): i32 => {
454
456
  return count > 0 ? count : 0
455
457
  }
456
458
 
459
+ /**
460
+ * A port on `host` that no UDP or TCP socket holds, for a group asked for port
461
+ * 0; a negative errno when a plain UDP socket cannot bind, -98 (`EADDRINUSE`)
462
+ * after 16 ports whose TCP side was taken. The port of a plain UDP bind,
463
+ * closed again: Linux gives a port-0 bind with `SO_REUSEPORT` a port another
464
+ * reuseport socket of the same user may hold, and the two groups would become
465
+ * one, each handed the other's datagrams. Between this close and the group's
466
+ * bind another process can still take the port; a plain bind there makes the
467
+ * group's fail, and only a reuseport bind at that instant to that one port
468
+ * joins it.
469
+ */
470
+ const preforkFreePort = (host: string): i32 => {
471
+ for (let k: i32 = 0; k < 16; k++) {
472
+ const probe: i32 = udpBind(host, 0, 0)
473
+ if (probe < 0) {
474
+ return probe
475
+ }
476
+ const port: i32 = netLocalPort(probe)
477
+ // Flags 0, not SO_REUSEPORT, which would succeed beside a same-user listener.
478
+ const listener: i32 = port > 0 ? tcpListenFlags(host, port, 1, 0) : -1
479
+ if (listener >= 0) {
480
+ netClose(listener)
481
+ }
482
+ netClose(probe)
483
+ if (port <= 0) {
484
+ return port < 0 ? port : -22
485
+ }
486
+ if (listener >= 0) {
487
+ return port
488
+ }
489
+ }
490
+ return -98
491
+ }
492
+
457
493
  /**
458
494
  * The UDP group of `listenUdp`, socket i for worker i, bound before anything
459
495
  * else on the port: steered by connection ID (`udpBind` flag 4 with the count
@@ -467,9 +503,14 @@ const preforkBindUdp = (config: PreforkConfig, count: i32): i32[] => {
467
503
  const out: i32[] = []
468
504
  // Flag 1 (SO_REUSEPORT) and 4 (steer), the group's size in bits 8 to 15.
469
505
  const steered: i32 = (count << 8) | 4 | 1
470
- const first: i32 = udpBind(host, config.listenPort, count > 1 ? steered : 1)
506
+ const asked: i32 = config.listenPort === 0 ? preforkFreePort(host) : config.listenPort
507
+ if (asked < 0) {
508
+ console.error(`nish/net/prefork: cannot find a free UDP and TCP port on ${host} (${asked})`)
509
+ return out
510
+ }
511
+ const first: i32 = udpBind(host, asked, count > 1 ? steered : 1)
471
512
  if (first < 0 && count > 1) {
472
- const plain: i32 = udpBind(host, config.listenPort, 1)
513
+ const plain: i32 = udpBind(host, asked, 1)
473
514
  if (plain >= 0) {
474
515
  console.error(`nish/net/prefork: QUIC steering refused (${first}), running one worker`)
475
516
  out.push(plain)
@@ -477,16 +518,11 @@ const preforkBindUdp = (config: PreforkConfig, count: i32): i32[] => {
477
518
  }
478
519
  }
479
520
  if (first < 0) {
480
- console.error(`nish/net/prefork: cannot bind UDP ${host}:${config.listenPort} (${first})`)
521
+ console.error(`nish/net/prefork: cannot bind UDP ${host}:${asked} (${first})`)
481
522
  return out
482
523
  }
483
524
  out.push(first)
484
- const port: i32 = config.listenPort === 0 ? netLocalPort(first) : config.listenPort
485
- if (port <= 0) {
486
- console.error(`nish/net/prefork: cannot read the UDP port bound (${port})`)
487
- netClose(first)
488
- return []
489
- }
525
+ const port: i32 = asked
490
526
  for (let i: i32 = 1; i < count; i++) {
491
527
  const fd: i32 = udpBind(host, port, steered)
492
528
  if (fd < 0) {