Commit 4f54897
fix(ext/node): implement setKeepAlive on native TCPWrap (#34865)
## Summary
The native `TCPWrap` rewrite (`feat(ext/node): rewrite node:http with
llhttp and native TCPWrap` #33208, plus `use native LibUvStreamWrap`
#33301), which shipped in **2.7.13**, dropped the `setKeepAlive` method
from the TCP handle. As a result `socket.setKeepAlive(enable, delay)`
became a **silent no-op**:
- `uv_tcp_keepalive` in `libs/core/uv_compat/tcp.rs` was a stub that
returned `0` without touching the socket, and
- `TCPWrap` no longer exposed a `setKeepAlive` method, so net.ts's
`ReflectHas(this._handle, "setKeepAlive")` guard short-circuited.
Before the rewrite (e.g. Deno 2.5.4) the handle implemented
`setKeepAlive` and `SO_KEEPALIVE` was actually set on the socket.
## Fix
- Implement `uv_tcp_keepalive` for real: toggle `SO_KEEPALIVE`, and when
enabling, set the per-connection idle time via `TCP_KEEPIDLE`
(Linux/Android) / `TCP_KEEPALIVE` (macOS/BSD). Windows toggles
`SO_KEEPALIVE`.
- Record the requested keepalive state on the handle so a `setKeepAlive`
issued before the connect future resolves is (re)applied once the socket
exists — mirroring how `internal_nodelay` is carried and applied on
connect.
- Expose `setKeepAlive` on `TCPWrap` (`ext/node/ops/tcp_wrap.rs`);
`delay` is the idle time in seconds, matching what net.ts passes
(`~~(initialDelay / 1000)`).
## Why this matters for #34729
Found while investigating the tedious/mssql ECONNRESET-over-SSH-tunnel
regression (#34729). `tedious` enables TCP keepalive immediately after
connecting (`socket.setKeepAlive(true, KEEP_ALIVE_INITIAL_DELAY)`)
specifically to keep tunneled/long-lived connections from being reaped
by SSH/NAT/firewalls. Since the rewrite that call silently did nothing,
removing protection that worked on 2.5.4.
I could not fully reproduce the exact `socket hang up` in a sandbox
without a real SQL Server — a faithful harness (real `sshd` + `ssh -L`,
dual-stack parallel connect like tedious's `connectInParallel`, a fake
TDS server completing the full PRELOGIN/LOGIN7 handshake, plus
fragmentation/latency) showed no old-vs-new difference for the core
read/write/connect path. So this is `Refs` rather than `Closes`: a
confirmed regression in the affected code path that restores the
pre-2.7.13 behavior tedious depends on.
## Tests
- `libs/core/uv_compat/tests.rs`:
`tcp_keepalive_sets_so_keepalive_on_connected_socket` connects a real
socket, calls `uv_tcp_keepalive`, and asserts via `getsockopt` that
`SO_KEEPALIVE` is enabled and `TCP_KEEPIDLE` matches the requested delay
(and that disabling clears it).
- `tests/unit_node/net_test.ts`: `socket.setKeepAlive()` reaches the
handle and returns the socket for chaining.
Both new Rust tests pass locally.
Refs #34729
Closes denoland/divybot#474
---------
Co-authored-by: divybot <divybot@users.noreply.github.com>
Co-authored-by: Divy Srivastava <me@littledivy.com>1 parent 33b909e commit 4f54897
4 files changed
Lines changed: 393 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
554 | 554 | | |
555 | 555 | | |
556 | 556 | | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
557 | 573 | | |
558 | 574 | | |
559 | 575 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
112 | 112 | | |
113 | 113 | | |
114 | 114 | | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
115 | 121 | | |
116 | 122 | | |
117 | 123 | | |
| |||
399 | 405 | | |
400 | 406 | | |
401 | 407 | | |
| 408 | + | |
402 | 409 | | |
403 | 410 | | |
404 | 411 | | |
| |||
883 | 890 | | |
884 | 891 | | |
885 | 892 | | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
886 | 899 | | |
887 | | - | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
| 935 | + | |
| 936 | + | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
| 961 | + | |
| 962 | + | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
| 1037 | + | |
| 1038 | + | |
| 1039 | + | |
| 1040 | + | |
| 1041 | + | |
| 1042 | + | |
| 1043 | + | |
| 1044 | + | |
888 | 1045 | | |
889 | 1046 | | |
890 | | - | |
891 | | - | |
892 | | - | |
| 1047 | + | |
| 1048 | + | |
| 1049 | + | |
893 | 1050 | | |
894 | | - | |
895 | | - | |
896 | | - | |
| 1051 | + | |
| 1052 | + | |
| 1053 | + | |
| 1054 | + | |
| 1055 | + | |
| 1056 | + | |
| 1057 | + | |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + | |
| 1065 | + | |
| 1066 | + | |
| 1067 | + | |
| 1068 | + | |
| 1069 | + | |
| 1070 | + | |
| 1071 | + | |
| 1072 | + | |
| 1073 | + | |
| 1074 | + | |
| 1075 | + | |
| 1076 | + | |
| 1077 | + | |
| 1078 | + | |
| 1079 | + | |
| 1080 | + | |
| 1081 | + | |
| 1082 | + | |
| 1083 | + | |
| 1084 | + | |
897 | 1085 | | |
898 | 1086 | | |
899 | 1087 | | |
| |||
1056 | 1244 | | |
1057 | 1245 | | |
1058 | 1246 | | |
| 1247 | + | |
1059 | 1248 | | |
1060 | 1249 | | |
1061 | 1250 | | |
| |||
1108 | 1297 | | |
1109 | 1298 | | |
1110 | 1299 | | |
| 1300 | + | |
| 1301 | + | |
| 1302 | + | |
1111 | 1303 | | |
1112 | 1304 | | |
1113 | 1305 | | |
| |||
0 commit comments