nish 0.44.0 → 0.45.1
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.
- package/README.md +1 -1
- package/docs/AI.md +18 -16
- package/package.json +5 -5
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +8 -0
- package/runtime/core.mjs +16 -0
- package/runtime/nish.d.ts +47 -5
- package/runtime/nish.mjs +4 -0
- package/std/README.md +3 -3
- package/std/array.ts +10 -2
- package/std/bytes.ts +239 -26
- package/std/cli-args.ts +1072 -606
- package/std/cli-flags.ts +180 -98
- package/std/cli.ts +21 -13
- package/std/collections.ts +245 -127
- package/std/compress/checksum.ts +32 -37
- package/std/compress/deflate.ts +1102 -542
- package/std/compress/gzip.ts +290 -145
- package/std/compress/zlib.ts +57 -21
- package/std/crypto/aes.ts +153 -75
- package/std/crypto/base64url.ts +84 -61
- package/std/crypto/bcrypt-batch.ts +164 -90
- package/std/crypto/bcrypt.ts +185 -107
- package/std/crypto/chacha20poly1305.ts +84 -68
- package/std/crypto/ct.ts +9 -7
- package/std/crypto/ed25519.ts +64 -27
- package/std/crypto/hkdf.ts +105 -63
- package/std/crypto/hmac.ts +95 -89
- package/std/crypto/jwt.ts +248 -126
- package/std/crypto/p256.ts +758 -444
- package/std/crypto/p384.ts +20 -13
- package/std/crypto/pki-roots.ts +95 -38
- package/std/crypto/pki.ts +164 -60
- package/std/crypto/rsa.ts +266 -223
- package/std/crypto/scram.ts +3 -4
- package/std/crypto/sha1.ts +107 -65
- package/std/crypto/sha256.ts +502 -422
- package/std/crypto/sha512.ts +204 -135
- package/std/crypto/x509-random.ts +9 -3
- package/std/crypto/x509.ts +331 -171
- package/std/json-equal.ts +35 -19
- package/std/json-format.ts +215 -108
- package/std/json-number.ts +126 -72
- package/std/json-pointer.ts +42 -13
- package/std/json-tokenizer.ts +498 -307
- package/std/json-typed.ts +115 -36
- package/std/json-value.ts +341 -152
- package/std/json.ts +300 -158
- package/std/net/buffer-pool.ts +17 -7
- package/std/net/dns-config.ts +427 -236
- package/std/net/dns-message.ts +114 -47
- package/std/net/dns-resolver.ts +736 -237
- package/std/net/dns.ts +205 -52
- package/std/net/fetch.ts +320 -184
- package/std/net/grpc.ts +948 -0
- package/std/net/handler.ts +1275 -217
- package/std/net/hpack.ts +395 -180
- package/std/net/http-compress.ts +502 -65
- package/std/net/http-fields.ts +85 -49
- package/std/net/http-server.ts +1764 -602
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +660 -278
- package/std/net/http2-cleartext.ts +165 -95
- package/std/net/http2-frame.ts +232 -89
- package/std/net/http2-tls.ts +42 -12
- package/std/net/http2.ts +327 -101
- package/std/net/http3-frame.ts +68 -33
- package/std/net/http3-server.ts +647 -192
- package/std/net/http3.ts +667 -189
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1165 -443
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1459 -559
- package/std/net/prefork.ts +254 -98
- package/std/net/protobuf.ts +1017 -0
- package/std/net/qpack.ts +143 -92
- package/std/net/quic-conn-ack.ts +50 -18
- package/std/net/quic-conn-cid.ts +66 -26
- package/std/net/quic-conn-params.ts +13 -7
- package/std/net/quic-datagram.ts +90 -27
- package/std/net/quic-frame.ts +225 -124
- package/std/net/quic-listener.ts +197 -89
- package/std/net/quic-packet.ts +289 -163
- package/std/net/quic-recovery.ts +279 -128
- package/std/net/quic-stream.ts +466 -167
- package/std/net/quic.ts +568 -257
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +651 -232
- package/std/net/static.ts +1458 -0
- package/std/net/tls/client.ts +140 -45
- package/std/net/tls/codec.ts +510 -260
- package/std/net/tls/record-client.ts +268 -76
- package/std/net/tls/record-server.ts +236 -79
- package/std/net/tls/record.ts +173 -47
- package/std/net/tls/schedule.ts +58 -39
- package/std/net/tls/ticket.ts +295 -103
- package/std/net/tls/verify.ts +1 -1
- package/std/net/tls-tcp.ts +218 -75
- package/std/net/tls.ts +260 -152
- package/std/net/upstream.ts +283 -86
- package/std/net/url.ts +277 -102
- package/std/net/websocket.ts +370 -186
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +205 -124
- package/std/threads.ts +22 -2
package/std/net/prefork.ts
CHANGED
|
@@ -290,10 +290,11 @@ export const preforkWorkerCount = (requested: i32, env: string | null): i32 => {
|
|
|
290
290
|
* process not started by one)
|
|
291
291
|
*/
|
|
292
292
|
export const preforkIndex = (args: string[]): i32 => {
|
|
293
|
-
|
|
293
|
+
const n: i32 = toI32(args.length)
|
|
294
|
+
if (n < 2) {
|
|
294
295
|
return -1
|
|
295
296
|
}
|
|
296
|
-
const last: string = args[
|
|
297
|
+
const last: string = args[n - 1]
|
|
297
298
|
if (!last.startsWith(PREFORK_WORKER_FLAG)) {
|
|
298
299
|
return -1
|
|
299
300
|
}
|
|
@@ -310,34 +311,44 @@ export const preforkIndex = (args: string[]): i32 => {
|
|
|
310
311
|
* supervisor appended, so a worker reads the same ones the supervisor did
|
|
311
312
|
*/
|
|
312
313
|
export const preforkArgs = (args: string[]): string[] => {
|
|
313
|
-
|
|
314
|
+
// Unsigned: the count only falls while a flag is found below it, so it
|
|
315
|
+
// never passes 0, and a wrapped `keep - 1` would only fail the range test.
|
|
316
|
+
const n: u32 = toU32(args.length)
|
|
317
|
+
let keep: u32 = n
|
|
314
318
|
if (preforkIndex(args) >= 0) {
|
|
315
|
-
keep =
|
|
316
|
-
if (
|
|
319
|
+
keep = n - 1
|
|
320
|
+
if (preforkArgIs(args, keep - 1, PREFORK_CHANNELS_FLAG)) {
|
|
317
321
|
keep = keep - 1
|
|
318
322
|
}
|
|
319
|
-
if (
|
|
323
|
+
if (preforkArgIs(args, keep - 1, PREFORK_LISTENER_FLAG)) {
|
|
320
324
|
keep = keep - 1
|
|
321
325
|
}
|
|
322
|
-
if (
|
|
326
|
+
if (preforkArgIs(args, keep - 1, PREFORK_UDP_FLAG)) {
|
|
323
327
|
keep = keep - 1
|
|
324
328
|
}
|
|
325
329
|
}
|
|
326
330
|
const out: string[] = []
|
|
327
|
-
for (let i:
|
|
328
|
-
|
|
329
|
-
out.push(args[i])
|
|
330
|
-
}
|
|
331
|
+
for (let i: u32 = 0; i < keep && i < toU32(args.length); i++) {
|
|
332
|
+
out.push(args[i])
|
|
331
333
|
}
|
|
332
334
|
return out
|
|
333
335
|
}
|
|
334
336
|
|
|
337
|
+
/**
|
|
338
|
+
* Whether `args[at]` is a flag the supervisor appended, starting with
|
|
339
|
+
* `flag`. Index 0 is the program, which is never one, so it answers `false`
|
|
340
|
+
* there as for an index outside `args`.
|
|
341
|
+
*/
|
|
342
|
+
const preforkArgIs = (args: string[], at: u32, flag: string): boolean =>
|
|
343
|
+
at > 0 && at < toU32(args.length) && args[at].startsWith(flag)
|
|
344
|
+
|
|
335
345
|
/** Field `at` of the channels flag in a worker's `args`, or -1. */
|
|
336
346
|
const preforkChannel = (args: string[], at: i32): i32 => {
|
|
337
|
-
|
|
347
|
+
const n: i32 = toI32(args.length)
|
|
348
|
+
if (preforkIndex(args) < 0 || n < 3) {
|
|
338
349
|
return -1
|
|
339
350
|
}
|
|
340
|
-
const flag: string = args[
|
|
351
|
+
const flag: string = args[n - 2]
|
|
341
352
|
if (!flag.startsWith(PREFORK_CHANNELS_FLAG)) {
|
|
342
353
|
return -1
|
|
343
354
|
}
|
|
@@ -347,9 +358,9 @@ const preforkChannel = (args: string[], at: i32): i32 => {
|
|
|
347
358
|
if (comma < 0) {
|
|
348
359
|
return -1
|
|
349
360
|
}
|
|
350
|
-
rest = rest.substring(comma + 1)
|
|
361
|
+
rest = rest.substring(toI64(comma) + 1)
|
|
351
362
|
}
|
|
352
|
-
const comma
|
|
363
|
+
const comma = rest.indexOf(",")
|
|
353
364
|
const field: string = comma < 0 ? rest : rest.substring(0, comma)
|
|
354
365
|
return preforkDigits(field) ? parseInt(field) : -1
|
|
355
366
|
}
|
|
@@ -393,8 +404,11 @@ export const preforkListener = (args: string[]): i32 => {
|
|
|
393
404
|
if (preforkIndex(args) < 0) {
|
|
394
405
|
return -1
|
|
395
406
|
}
|
|
396
|
-
|
|
397
|
-
|
|
407
|
+
// From the second-last argument back to the fourth-last, stopping before
|
|
408
|
+
// the program at 0; unsigned, so a short list wraps `at` out of range.
|
|
409
|
+
const n: u32 = toU32(args.length)
|
|
410
|
+
for (let at: u32 = n - 2; at + 3 >= n && at > 0 && at < toU32(args.length); at--) {
|
|
411
|
+
const flag: string = args[at]
|
|
398
412
|
if (flag.startsWith(PREFORK_LISTENER_FLAG)) {
|
|
399
413
|
const digits: string = flag.substring(PREFORK_LISTENER_FLAG.length)
|
|
400
414
|
return preforkDigits(digits) ? parseInt(digits) : -1
|
|
@@ -414,13 +428,18 @@ const preforkUdpField = (args: string[], at: i32): i32 => {
|
|
|
414
428
|
if (preforkIndex(args) < 0) {
|
|
415
429
|
return -1
|
|
416
430
|
}
|
|
417
|
-
|
|
418
|
-
|
|
431
|
+
// As in `preforkListener`, one argument further back.
|
|
432
|
+
const n: u32 = toU32(args.length)
|
|
433
|
+
for (let i: u32 = n - 2; i + 4 >= n && i > 0 && i < toU32(args.length); i--) {
|
|
434
|
+
const flag: string = args[i]
|
|
419
435
|
if (flag.startsWith(PREFORK_UDP_FLAG)) {
|
|
420
436
|
const value: string = flag.substring(PREFORK_UDP_FLAG.length)
|
|
421
437
|
const comma: i32 = toI32(value.indexOf(","))
|
|
422
|
-
|
|
423
|
-
|
|
438
|
+
if (comma < 0 || comma > toI32(value.length)) {
|
|
439
|
+
return -1
|
|
440
|
+
}
|
|
441
|
+
const fd: string = value.substring(0, comma)
|
|
442
|
+
const size: string = value.substring(toI64(comma) + 1)
|
|
424
443
|
if (!preforkDigits(fd) || !preforkDigits(size)) {
|
|
425
444
|
return -1
|
|
426
445
|
}
|
|
@@ -540,6 +559,18 @@ const preforkBindUdp = (config: PreforkConfig, count: i32): i32[] => {
|
|
|
540
559
|
/** Milliseconds on the monotonic clock. */
|
|
541
560
|
const preforkNowMs = (): i64 => monotonicNanos() / 1000000
|
|
542
561
|
|
|
562
|
+
// The clock in milliseconds lies within 2^63 / 10^6 of 0, about 9.2 * 10^12,
|
|
563
|
+
// so a sum or difference of two such times, or of one and an i32, is far
|
|
564
|
+
// inside the i64 range. The 64-bit unsigned arithmetic below wraps rather
|
|
565
|
+
// than panics, and on values that never pass the range its result, read
|
|
566
|
+
// back as an i64, is the exact one.
|
|
567
|
+
|
|
568
|
+
/** `now` plus `ms` milliseconds. */
|
|
569
|
+
const preforkAfter = (now: i64, ms: i32): i64 => toI64(toU64(now) + toU64(toI64(ms)))
|
|
570
|
+
|
|
571
|
+
/** The milliseconds from `then` to `now`. */
|
|
572
|
+
const preforkSince = (now: i64, then: i64): i64 => toI64(toU64(now) - toU64(then))
|
|
573
|
+
|
|
543
574
|
/** The signals the supervisor stops its workers with. */
|
|
544
575
|
const PREFORK_SIGTERM: i32 = 15
|
|
545
576
|
const PREFORK_SIGKILL: i32 = 9
|
|
@@ -609,7 +640,7 @@ class PreforkSupervisor {
|
|
|
609
640
|
* @internal Read by `preforkSupervise` to see when, with `restart` off,
|
|
610
641
|
* every worker has exited.
|
|
611
642
|
*/
|
|
612
|
-
running:
|
|
643
|
+
running: u32 = 0
|
|
613
644
|
/** The readiness loop each supervisor channel is added to. */
|
|
614
645
|
private loop: i32
|
|
615
646
|
/** `config.serviceWorker`: with it at 0 or above, the workers are one group. */
|
|
@@ -646,7 +677,16 @@ class PreforkSupervisor {
|
|
|
646
677
|
this.listeners = []
|
|
647
678
|
this.args = preforkArgs(args)
|
|
648
679
|
const self: string | null = platform === "linux" ? realpathSync("/proc/self/exe") : null
|
|
649
|
-
|
|
680
|
+
// With no arguments there is no program to name, and `spawn` refuses the
|
|
681
|
+
// empty one, so the first start fails as `PREFORK_SPAWN_FAILED`.
|
|
682
|
+
const own: string[] = this.args
|
|
683
|
+
let program: string = ""
|
|
684
|
+
if (self !== null) {
|
|
685
|
+
program = self
|
|
686
|
+
} else if (own.length > 0) {
|
|
687
|
+
program = own[0]
|
|
688
|
+
}
|
|
689
|
+
this.program = program
|
|
650
690
|
this.slots = []
|
|
651
691
|
// No end yet; typed so an f64 build still pushes an i32.
|
|
652
692
|
const none: i32 = -1
|
|
@@ -662,15 +702,44 @@ class PreforkSupervisor {
|
|
|
662
702
|
* `pairs`; false when a socketpair, the loop or the spawn refused it.
|
|
663
703
|
*/
|
|
664
704
|
private start(index: i32): boolean {
|
|
705
|
+
// Every caller passes a slot's index, and the per-worker arrays are made
|
|
706
|
+
// one entry per slot, so this refuses nothing; it is what lets the
|
|
707
|
+
// prover see each lookup below is in range.
|
|
708
|
+
const slots: PreforkSlot[] = this.slots
|
|
709
|
+
const toService: i32[] = this.toService
|
|
710
|
+
const udp: i32[] = this.udp
|
|
711
|
+
const listeners: i32[] = this.listeners
|
|
712
|
+
if (index < 0 || index >= toI32(slots.length) || index >= toI32(toService.length)) {
|
|
713
|
+
return false
|
|
714
|
+
}
|
|
715
|
+
if (
|
|
716
|
+
(udp.length > 0 && index >= toI32(udp.length)) ||
|
|
717
|
+
(listeners.length > 0 && index >= toI32(listeners.length))
|
|
718
|
+
) {
|
|
719
|
+
return false
|
|
720
|
+
}
|
|
721
|
+
// Read before any call, while the test above still covers each index.
|
|
722
|
+
// The UDP and listener descriptors are lists of none or one, so an empty
|
|
723
|
+
// array passes nothing on rather than a stand-in descriptor.
|
|
724
|
+
const slot: PreforkSlot = slots[index]
|
|
725
|
+
const serviceEnd: i32 = toService[index]
|
|
726
|
+
const udpFd: i32[] = []
|
|
727
|
+
if (index < toI32(udp.length)) {
|
|
728
|
+
udpFd.push(udp[index])
|
|
729
|
+
}
|
|
730
|
+
const listenerFd: i32[] = []
|
|
731
|
+
if (index < toI32(listeners.length)) {
|
|
732
|
+
listenerFd.push(listeners[index])
|
|
733
|
+
}
|
|
665
734
|
const fds: i32[] = []
|
|
666
735
|
let own: i32 = -1
|
|
667
736
|
if (this.config.supervisorChannels) {
|
|
668
737
|
const pair: i32[] = [-1, -1]
|
|
669
|
-
if (unixSocketPair(this.config.channelKind, pair) !== 0) {
|
|
738
|
+
if (unixSocketPair(this.config.channelKind, pair) !== 0 || pair.length < 2) {
|
|
670
739
|
return false
|
|
671
740
|
}
|
|
672
741
|
// On the loop before the worker starts, so a refusal is a failed start.
|
|
673
|
-
if (pollAdd(this.loop, pair[0], 1, index + 1) < 0) {
|
|
742
|
+
if (pollAdd(this.loop, pair[0], 1, toI32(toI64(index) + 1)) < 0) {
|
|
674
743
|
netClose(pair[0])
|
|
675
744
|
netClose(pair[1])
|
|
676
745
|
return false
|
|
@@ -681,16 +750,16 @@ class PreforkSupervisor {
|
|
|
681
750
|
// Entry i of `fds` is the child's descriptor 3 + i.
|
|
682
751
|
const fields: string[] = [own >= 0 ? "3" : "-1"]
|
|
683
752
|
if (this.service >= 0 && index !== this.service) {
|
|
684
|
-
fds.push(
|
|
685
|
-
fields.push(`${
|
|
753
|
+
fds.push(serviceEnd)
|
|
754
|
+
fields.push(`${toU32(fds.length) + 2}`)
|
|
686
755
|
} else if (this.service >= 0) {
|
|
687
756
|
fields.push("-1")
|
|
688
|
-
for (let i: i32 = 0; i < toI32(this.
|
|
757
|
+
for (let i: i32 = 0; i < toI32(this.fromWorkers.length); i++) {
|
|
689
758
|
if (i === index) {
|
|
690
759
|
fields.push("-1")
|
|
691
760
|
} else {
|
|
692
761
|
fds.push(this.fromWorkers[i])
|
|
693
|
-
fields.push(`${
|
|
762
|
+
fields.push(`${toU32(fds.length) + 2}`)
|
|
694
763
|
}
|
|
695
764
|
}
|
|
696
765
|
}
|
|
@@ -699,20 +768,20 @@ class PreforkSupervisor {
|
|
|
699
768
|
argv.push(this.args[i])
|
|
700
769
|
}
|
|
701
770
|
const channels: boolean = fds.length > 0
|
|
702
|
-
|
|
703
|
-
fds.push(
|
|
704
|
-
argv.push(`${PREFORK_UDP_FLAG}${
|
|
771
|
+
for (const fd of udpFd) {
|
|
772
|
+
fds.push(fd)
|
|
773
|
+
argv.push(`${PREFORK_UDP_FLAG}${toU32(fds.length) + 2},${udp.length}`)
|
|
705
774
|
}
|
|
706
|
-
|
|
707
|
-
fds.push(
|
|
708
|
-
argv.push(`${PREFORK_LISTENER_FLAG}${
|
|
775
|
+
for (const fd of listenerFd) {
|
|
776
|
+
fds.push(fd)
|
|
777
|
+
argv.push(`${PREFORK_LISTENER_FLAG}${toU32(fds.length) + 2}`)
|
|
709
778
|
}
|
|
710
779
|
if (channels) {
|
|
711
780
|
argv.push(`${PREFORK_CHANNELS_FLAG}${fields.join(",")}`)
|
|
712
781
|
}
|
|
713
782
|
argv.push(`${PREFORK_WORKER_FLAG}${index}`)
|
|
714
783
|
const pid: i32 = fds.length > 0 ? spawnWithFds(argv, fds) : spawn(argv)
|
|
715
|
-
if (own >= 0) {
|
|
784
|
+
if (own >= 0 && fds.length > 0) {
|
|
716
785
|
netClose(fds[0])
|
|
717
786
|
}
|
|
718
787
|
if (pid <= 0) {
|
|
@@ -721,7 +790,6 @@ class PreforkSupervisor {
|
|
|
721
790
|
}
|
|
722
791
|
return false
|
|
723
792
|
}
|
|
724
|
-
const slot: PreforkSlot = this.slots[index]
|
|
725
793
|
if (own >= 0) {
|
|
726
794
|
slot.channel = own
|
|
727
795
|
}
|
|
@@ -747,18 +815,21 @@ class PreforkSupervisor {
|
|
|
747
815
|
* a wait that starts at `backoffMs` and doubles up to `backoffCapMs`.
|
|
748
816
|
*/
|
|
749
817
|
private backoff(slot: PreforkSlot, now: i64, failed: boolean): void {
|
|
750
|
-
if (failed || now
|
|
818
|
+
if (failed || preforkSince(now, slot.at) < toI64(this.config.quickExitMs)) {
|
|
751
819
|
const cap: i32 = this.config.backoffCapMs
|
|
752
820
|
if (slot.delay === 0) {
|
|
753
821
|
slot.delay = this.config.backoffMs
|
|
754
822
|
} else {
|
|
755
823
|
// Compared before doubling, so a cap near the i32 limit cannot overflow.
|
|
756
|
-
|
|
824
|
+
// In 64 bits, which the prover can see holds any doubled i32; the
|
|
825
|
+
// test above keeps the result within `cap`.
|
|
826
|
+
const delay: i32 = slot.delay
|
|
827
|
+
slot.delay = delay > cap / 2 ? cap : toI32(toI64(delay) * 2)
|
|
757
828
|
}
|
|
758
829
|
} else {
|
|
759
830
|
slot.delay = 0
|
|
760
831
|
}
|
|
761
|
-
slot.at = now
|
|
832
|
+
slot.at = preforkAfter(now, slot.delay)
|
|
762
833
|
}
|
|
763
834
|
|
|
764
835
|
/**
|
|
@@ -773,14 +844,21 @@ class PreforkSupervisor {
|
|
|
773
844
|
reap(stopping: boolean): boolean {
|
|
774
845
|
while (this.running > 0) {
|
|
775
846
|
const done: i32[] = waitPid(-1, 1)
|
|
776
|
-
|
|
847
|
+
// `waitPid` answers two entries; an answer with none is a failure.
|
|
848
|
+
let pid: i32 = 0
|
|
849
|
+
if (done.length > 0) {
|
|
850
|
+
pid = done[0]
|
|
851
|
+
} else {
|
|
852
|
+
return false
|
|
853
|
+
}
|
|
854
|
+
if (pid === -4) {
|
|
777
855
|
continue
|
|
778
856
|
}
|
|
779
|
-
if (
|
|
780
|
-
return
|
|
857
|
+
if (pid <= 0) {
|
|
858
|
+
return pid === 0
|
|
781
859
|
}
|
|
782
|
-
const index: i32 = this.find(
|
|
783
|
-
if (index < 0) {
|
|
860
|
+
const index: i32 = this.find(pid)
|
|
861
|
+
if (index < 0 || index >= toI32(this.slots.length)) {
|
|
784
862
|
continue
|
|
785
863
|
}
|
|
786
864
|
const slot: PreforkSlot = this.slots[index]
|
|
@@ -820,7 +898,7 @@ class PreforkSupervisor {
|
|
|
820
898
|
/** Stop the group so it starts again whole: SIGTERM now, SIGKILL after `graceMs`. */
|
|
821
899
|
private cascade(now: i64): void {
|
|
822
900
|
this.cascading = true
|
|
823
|
-
this.cascadeAt = now
|
|
901
|
+
this.cascadeAt = preforkAfter(now, this.config.graceMs)
|
|
824
902
|
this.signal(PREFORK_SIGTERM)
|
|
825
903
|
}
|
|
826
904
|
|
|
@@ -835,21 +913,34 @@ class PreforkSupervisor {
|
|
|
835
913
|
}
|
|
836
914
|
const now: i64 = preforkNowMs()
|
|
837
915
|
if (now >= this.cascadeAt) {
|
|
838
|
-
this.cascadeAt = now
|
|
916
|
+
this.cascadeAt = preforkAfter(now, this.config.graceMs)
|
|
839
917
|
this.signal(PREFORK_SIGKILL)
|
|
840
918
|
}
|
|
841
919
|
}
|
|
842
920
|
|
|
843
921
|
/** A pair from each worker to the service worker; false when one failed. */
|
|
844
922
|
private pairs(): boolean {
|
|
923
|
+
// Made afresh by push, one entry per slot: a failure leaves the ends made
|
|
924
|
+
// so far in place for `closePairs`, and `start` refuses a worker past them.
|
|
925
|
+
const toService: i32[] = []
|
|
926
|
+
const fromWorkers: i32[] = []
|
|
927
|
+
this.toService = toService
|
|
928
|
+
this.fromWorkers = fromWorkers
|
|
929
|
+
// No end; typed so an f64 build still pushes an i32.
|
|
930
|
+
const none: i32 = -1
|
|
845
931
|
for (let i: i32 = 0; i < toI32(this.slots.length); i++) {
|
|
846
|
-
if (i
|
|
932
|
+
if (i === this.service) {
|
|
933
|
+
toService.push(none)
|
|
934
|
+
fromWorkers.push(none)
|
|
935
|
+
} else {
|
|
847
936
|
const pair: i32[] = [-1, -1]
|
|
848
|
-
if (unixSocketPair(this.config.channelKind, pair) !== 0) {
|
|
937
|
+
if (unixSocketPair(this.config.channelKind, pair) !== 0 || pair.length < 2) {
|
|
849
938
|
return false
|
|
850
939
|
}
|
|
851
|
-
|
|
852
|
-
|
|
940
|
+
const toEnd: i32 = pair[0]
|
|
941
|
+
const fromEnd: i32 = pair[1]
|
|
942
|
+
toService.push(toEnd)
|
|
943
|
+
fromWorkers.push(fromEnd)
|
|
853
944
|
}
|
|
854
945
|
}
|
|
855
946
|
return true
|
|
@@ -857,11 +948,13 @@ class PreforkSupervisor {
|
|
|
857
948
|
|
|
858
949
|
/** Close every end `pairs` made: the workers hold their copies now. */
|
|
859
950
|
private closePairs(): void {
|
|
860
|
-
for (let i: i32 = 0; i < toI32(this.
|
|
951
|
+
for (let i: i32 = 0; i < toI32(this.toService.length); i++) {
|
|
861
952
|
if (this.toService[i] >= 0) {
|
|
862
953
|
netClose(this.toService[i])
|
|
863
954
|
this.toService[i] = -1
|
|
864
955
|
}
|
|
956
|
+
}
|
|
957
|
+
for (let i: i32 = 0; i < toI32(this.fromWorkers.length); i++) {
|
|
865
958
|
if (this.fromWorkers[i] >= 0) {
|
|
866
959
|
netClose(this.fromWorkers[i])
|
|
867
960
|
this.fromWorkers[i] = -1
|
|
@@ -906,7 +999,9 @@ class PreforkSupervisor {
|
|
|
906
999
|
return false
|
|
907
1000
|
}
|
|
908
1001
|
console.error(`nish/net/prefork: cannot restart the workers at worker ${failed}`)
|
|
909
|
-
|
|
1002
|
+
if (failed < toI32(this.slots.length)) {
|
|
1003
|
+
this.backoff(this.slots[failed], now, true)
|
|
1004
|
+
}
|
|
910
1005
|
this.cascade(now)
|
|
911
1006
|
return true
|
|
912
1007
|
}
|
|
@@ -1100,42 +1195,35 @@ export const preforkWith = (args: string[], config: PreforkConfig): i32 => {
|
|
|
1100
1195
|
}
|
|
1101
1196
|
|
|
1102
1197
|
/**
|
|
1103
|
-
* `preforkWith
|
|
1104
|
-
*
|
|
1105
|
-
*
|
|
1106
|
-
* error. The callback reads what is waiting, since an end left readable wakes
|
|
1107
|
-
* the loop again at once, and may write back, but never closes `fd`: the
|
|
1108
|
-
* supervisor closes it after a hang-up, when the worker is reaped and when it
|
|
1109
|
-
* stops, and a restarted worker comes with a new one.
|
|
1110
|
-
*
|
|
1111
|
-
* Panics, besides, when `serviceWorker` is not below the worker count or the
|
|
1112
|
-
* count is above 64 with a service worker (63 with `listenPort`), the most
|
|
1113
|
-
* `spawnWithFds` hands one child; when `pin`, on Linux, asks for more workers
|
|
1114
|
-
* than `availableParallelism()`; when `listenUdp` asks for more than 255
|
|
1115
|
-
* workers (62 with a service worker); and, through `preforkInvalid`, on
|
|
1116
|
-
* seqpacket channels off Linux. A listener or UDP socket that cannot bind,
|
|
1117
|
-
* or a refused steering that leaves one worker beside a `serviceWorker` past
|
|
1118
|
-
* 0, answers `PREFORK_SPAWN_FAILED` before any worker starts.
|
|
1198
|
+
* `preforkWith` for a caller built without `--allow-panics`: a config that
|
|
1199
|
+
* breaks `preforkSupervise`'s contract is answered as `Err`, naming the field
|
|
1200
|
+
* and the limit it passed, before any worker starts, rather than a panic.
|
|
1119
1201
|
*
|
|
1120
1202
|
* @param args the process's arguments, `process.argv`
|
|
1121
|
-
* @param config the supervisor's settings,
|
|
1122
|
-
*
|
|
1123
|
-
*
|
|
1124
|
-
*
|
|
1125
|
-
*
|
|
1126
|
-
* `fd` wakes it, with `pollWait`'s `events`
|
|
1127
|
-
* @returns in a worker, its index at once; in the supervisor, once it is done,
|
|
1128
|
-
* `PREFORK_STOPPED`, `PREFORK_WORKERS_EXITED` or `PREFORK_SPAWN_FAILED`
|
|
1203
|
+
* @param config the supervisor's settings, checked in the supervisor and
|
|
1204
|
+
* each worker alike as `preforkWith` checks them
|
|
1205
|
+
* @returns `Ok` with what `preforkWith` answers (a worker's index, or the
|
|
1206
|
+
* supervisor's `PREFORK_STOPPED`, `PREFORK_WORKERS_EXITED` or
|
|
1207
|
+
* `PREFORK_SPAWN_FAILED`), or `Err` with why the config was refused
|
|
1129
1208
|
*/
|
|
1130
|
-
export const
|
|
1209
|
+
export const tryPreforkWith = (args: string[], config: PreforkConfig): Result<i32, string> => {
|
|
1210
|
+
const buf: u8[] = new Array<u8>(512)
|
|
1211
|
+
return preforkRun(args, config, buf, preforkDiscard)
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1214
|
+
/**
|
|
1215
|
+
* `preforkSupervise`, answering a config that breaks its contract as `Err`
|
|
1216
|
+
* with the reason, before any worker starts, where `preforkSupervise` panics.
|
|
1217
|
+
*/
|
|
1218
|
+
const preforkRun = <S>(
|
|
1131
1219
|
args: string[],
|
|
1132
1220
|
config: PreforkConfig,
|
|
1133
1221
|
state: S,
|
|
1134
1222
|
onChannel: (state: S, index: i32, fd: i32, events: i32) => void
|
|
1135
|
-
): i32 => {
|
|
1223
|
+
): Result<i32, string> => {
|
|
1136
1224
|
const invalid: string = preforkInvalid(config)
|
|
1137
1225
|
if (invalid !== "") {
|
|
1138
|
-
|
|
1226
|
+
return Err(invalid)
|
|
1139
1227
|
}
|
|
1140
1228
|
const pin: boolean = config.pin && platform === "linux"
|
|
1141
1229
|
const index: i32 = preforkIndex(args)
|
|
@@ -1144,21 +1232,26 @@ export const preforkSupervise = <S>(
|
|
|
1144
1232
|
if (pinned < 0) {
|
|
1145
1233
|
console.error(`nish/net/prefork: worker ${index} runs unpinned (${pinned})`)
|
|
1146
1234
|
}
|
|
1147
|
-
return index
|
|
1235
|
+
return Ok(index)
|
|
1148
1236
|
}
|
|
1149
1237
|
let count: i32 = preforkWorkerCount(config.workers, getenv("NISH_WORKERS"))
|
|
1150
1238
|
if (config.listenUdp && count > PREFORK_UDP_MAX) {
|
|
1151
|
-
|
|
1239
|
+
return Err(`listenUdp with ${count} workers, above ${PREFORK_UDP_MAX}`)
|
|
1152
1240
|
}
|
|
1153
1241
|
// A held listener is one more descriptor for every worker, the service one
|
|
1154
1242
|
// too, and a held UDP socket one more again.
|
|
1155
|
-
|
|
1243
|
+
let fdMax: i32 = 64
|
|
1244
|
+
if (config.listenPort >= 0 && config.listenUdp) {
|
|
1245
|
+
fdMax = 62
|
|
1246
|
+
} else if (config.listenPort >= 0 || config.listenUdp) {
|
|
1247
|
+
fdMax = 63
|
|
1248
|
+
}
|
|
1156
1249
|
if (config.serviceWorker >= count || (config.serviceWorker >= 0 && count > fdMax)) {
|
|
1157
|
-
|
|
1250
|
+
return Err(`serviceWorker not below ${count} workers, or above ${fdMax} workers`)
|
|
1158
1251
|
}
|
|
1159
1252
|
const cpus: i32 = pin ? availableParallelism() : 0
|
|
1160
1253
|
if (pin && count > cpus) {
|
|
1161
|
-
|
|
1254
|
+
return Err(`pin with ${count} workers on ${cpus} CPUs`)
|
|
1162
1255
|
}
|
|
1163
1256
|
if (config.pin && !pin) {
|
|
1164
1257
|
console.error("nish/net/prefork: pinning needs Linux; the workers run unpinned")
|
|
@@ -1172,14 +1265,14 @@ export const preforkSupervise = <S>(
|
|
|
1172
1265
|
if (loop >= 0) {
|
|
1173
1266
|
netClose(loop)
|
|
1174
1267
|
}
|
|
1175
|
-
return PREFORK_SPAWN_FAILED
|
|
1268
|
+
return Ok(PREFORK_SPAWN_FAILED)
|
|
1176
1269
|
}
|
|
1177
1270
|
// The UDP group first: a refused steering runs one worker, so it decides
|
|
1178
1271
|
// the count everything after it is made for.
|
|
1179
1272
|
const udp: i32[] = config.listenUdp ? preforkBindUdp(config, count) : []
|
|
1180
1273
|
if (config.listenUdp && udp.length === 0) {
|
|
1181
1274
|
netClose(loop)
|
|
1182
|
-
return PREFORK_SPAWN_FAILED
|
|
1275
|
+
return Ok(PREFORK_SPAWN_FAILED)
|
|
1183
1276
|
}
|
|
1184
1277
|
if (config.listenUdp) {
|
|
1185
1278
|
count = toI32(udp.length)
|
|
@@ -1191,15 +1284,22 @@ export const preforkSupervise = <S>(
|
|
|
1191
1284
|
netClose(udp[i])
|
|
1192
1285
|
}
|
|
1193
1286
|
netClose(loop)
|
|
1194
|
-
return PREFORK_SPAWN_FAILED
|
|
1287
|
+
return Ok(PREFORK_SPAWN_FAILED)
|
|
1195
1288
|
}
|
|
1196
1289
|
const supervisor = new PreforkSupervisor(config, args, count, loop, udp)
|
|
1197
1290
|
if (config.listenPort >= 0 && !supervisor.listen(pin)) {
|
|
1198
1291
|
netClose(loop)
|
|
1199
|
-
return PREFORK_SPAWN_FAILED
|
|
1292
|
+
return Ok(PREFORK_SPAWN_FAILED)
|
|
1200
1293
|
}
|
|
1201
1294
|
// Token 0 is the signal pipe, token i + 1 worker i's channel.
|
|
1202
|
-
|
|
1295
|
+
// Pushed, not sized, so the length is never a product the prover must check.
|
|
1296
|
+
const ready: i32[] = []
|
|
1297
|
+
ready.push(0)
|
|
1298
|
+
ready.push(0)
|
|
1299
|
+
for (let i: i32 = 0; i < count; i++) {
|
|
1300
|
+
ready.push(0)
|
|
1301
|
+
ready.push(0)
|
|
1302
|
+
}
|
|
1203
1303
|
let result: i32 = PREFORK_STOPPED
|
|
1204
1304
|
if (!supervisor.restart(true)) {
|
|
1205
1305
|
console.error(`nish/net/prefork: cannot start ${supervisor.program}`)
|
|
@@ -1209,16 +1309,31 @@ export const preforkSupervise = <S>(
|
|
|
1209
1309
|
while (running) {
|
|
1210
1310
|
const n: i32 = pollWait(loop, ready, config.tickMs)
|
|
1211
1311
|
for (let k: i32 = 0; k < n; k++) {
|
|
1212
|
-
|
|
1213
|
-
|
|
1312
|
+
// Pair k is token and events; `pollWait` answers no more pairs than
|
|
1313
|
+
// `ready` holds, so the test only lets the prover see it.
|
|
1314
|
+
const at: u32 = toU32(k) * 2
|
|
1315
|
+
const next: u32 = at + 1
|
|
1316
|
+
if (at >= toU32(ready.length)) {
|
|
1317
|
+
break
|
|
1318
|
+
}
|
|
1319
|
+
const token: i32 = ready[at]
|
|
1320
|
+
if (next >= toU32(ready.length)) {
|
|
1321
|
+
break
|
|
1322
|
+
}
|
|
1323
|
+
const events: i32 = ready[next]
|
|
1214
1324
|
if (token === 0) {
|
|
1215
1325
|
readSignal(signals)
|
|
1216
1326
|
running = false
|
|
1217
|
-
} else if (
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1327
|
+
} else if (token > 0 && token <= toI32(supervisor.slots.length)) {
|
|
1328
|
+
// Worker i's channel carries token i + 1; a token past the slots is
|
|
1329
|
+
// none of the supervisor's, so it is skipped.
|
|
1330
|
+
const worker: i32 = token - 1
|
|
1331
|
+
const slot: PreforkSlot = supervisor.slots[worker]
|
|
1332
|
+
if (slot.channel >= 0) {
|
|
1333
|
+
onChannel(state, worker, slot.channel, events)
|
|
1334
|
+
if ((events & 4) !== 0) {
|
|
1335
|
+
supervisor.closeChannel(slot)
|
|
1336
|
+
}
|
|
1222
1337
|
}
|
|
1223
1338
|
}
|
|
1224
1339
|
}
|
|
@@ -1236,7 +1351,48 @@ export const preforkSupervise = <S>(
|
|
|
1236
1351
|
supervisor.stop(loop, signals, ready)
|
|
1237
1352
|
supervisor.closeListeners()
|
|
1238
1353
|
netClose(loop)
|
|
1239
|
-
return result
|
|
1354
|
+
return Ok(result)
|
|
1355
|
+
}
|
|
1356
|
+
|
|
1357
|
+
/**
|
|
1358
|
+
* `preforkWith`, calling `onChannel(state, index, fd, events)` in the
|
|
1359
|
+
* supervisor whenever worker `index`'s supervisor channel `fd` is readable or
|
|
1360
|
+
* hung up: `events` is `pollWait`'s, 1 readable and 4 added on a hang-up or
|
|
1361
|
+
* error. The callback reads what is waiting, since an end left readable wakes
|
|
1362
|
+
* the loop again at once, and may write back, but never closes `fd`: the
|
|
1363
|
+
* supervisor closes it after a hang-up, when the worker is reaped and when it
|
|
1364
|
+
* stops, and a restarted worker comes with a new one.
|
|
1365
|
+
*
|
|
1366
|
+
* Panics, besides, when `serviceWorker` is not below the worker count or the
|
|
1367
|
+
* count is above 64 with a service worker (63 with `listenPort`), the most
|
|
1368
|
+
* `spawnWithFds` hands one child; when `pin`, on Linux, asks for more workers
|
|
1369
|
+
* than `availableParallelism()`; when `listenUdp` asks for more than 255
|
|
1370
|
+
* workers (62 with a service worker); and, through `preforkInvalid`, on
|
|
1371
|
+
* seqpacket channels off Linux. A listener or UDP socket that cannot bind,
|
|
1372
|
+
* or a refused steering that leaves one worker beside a `serviceWorker` past
|
|
1373
|
+
* 0, answers `PREFORK_SPAWN_FAILED` before any worker starts.
|
|
1374
|
+
*
|
|
1375
|
+
* @param args the process's arguments, `process.argv`
|
|
1376
|
+
* @param config the supervisor's settings, every field checked (a bad one
|
|
1377
|
+
* panics) in the supervisor and each worker alike; past that check a
|
|
1378
|
+
* worker reads only `pin`
|
|
1379
|
+
* @param state the program's own value, passed to every `onChannel` call
|
|
1380
|
+
* @param onChannel what the supervisor runs when worker `index`'s channel
|
|
1381
|
+
* `fd` wakes it, with `pollWait`'s `events`
|
|
1382
|
+
* @returns in a worker, its index at once; in the supervisor, once it is done,
|
|
1383
|
+
* `PREFORK_STOPPED`, `PREFORK_WORKERS_EXITED` or `PREFORK_SPAWN_FAILED`
|
|
1384
|
+
*/
|
|
1385
|
+
export const preforkSupervise = <S>(
|
|
1386
|
+
args: string[],
|
|
1387
|
+
config: PreforkConfig,
|
|
1388
|
+
state: S,
|
|
1389
|
+
onChannel: (state: S, index: i32, fd: i32, events: i32) => void
|
|
1390
|
+
): i32 => {
|
|
1391
|
+
const ran = preforkRun(args, config, state, onChannel)
|
|
1392
|
+
if (ran.isErr()) {
|
|
1393
|
+
panic(`nish/net/prefork: ${ran.error}`)
|
|
1394
|
+
}
|
|
1395
|
+
return ran.value
|
|
1240
1396
|
}
|
|
1241
1397
|
|
|
1242
1398
|
/**
|