Commit f96897e
committed
feat(http): let another thread cancel a request in flight
There was no way to abandon a request that had been sent. A caller that stopped
caring (a user pressing Esc, say) could only leave the call running on a thread
and wait out readTimeoutMs, with the connection held open and the server still
producing the response.
send and send_stream take an optional std::stop_token. While a token is
attached, every wait on the socket is made in slices of at most 50 ms with a
check for a stop request between them, and bio_recv waits for the socket before
reading, so a handshake or the second half of a TLS record is covered too. When
the token is stopped the call returns within about one slice, and the connection
is dropped rather than pooled, so the server sees it end. A close_notify goes
out only when the stop comes after the handshake has finished.
HttpResponse::cancelled says why the call ended, without matching text. Before a
status line has arrived the response is statusCode 0 with statusText and
bodyError "Cancelled"; after it, statusCode is the server's and bodyError is
"cancelled". A cancelled request is not resent on a fresh connection by the
stale-connection retry, and a redirect is not followed.
proxy_tunnel and proxy_connect take the token as a trailing default argument
and hand it to the socket or the TLS session they open, so the exchange with
an HTTP, https or SOCKS5 proxy is covered as well.
Without a token (stop_possible() is false) every wait is the single poll it was,
and bio_recv does not poll first.
Not covered: getaddrinfo, a write blocked because the peer is not reading, and
connect on openkal, where connect completes synchronously. download_to_file is
unchanged.
Tests in test_cancel run against the in-process TLS server and a listener that
accepts and says nothing: stopped while waiting for the response head, while a
stream waits for its next chunk, and during the handshake, each with a 30 s read
timeout so only the token can have ended it; a token already stopped sends
nothing; a token never stopped leaves a 300 ms timeout alone; a cancelled
connection is not reused; a cancelled request on a pooled connection is not
retried; and 20 cancelled requests leave the descriptor count where it was.
Not run on macOS or Windows; the change adds no platform-specific code beyond
the poll the sockets already use.1 parent f461940 commit f96897e
8 files changed
Lines changed: 570 additions & 30 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
3 | 21 | | |
4 | 22 | | |
5 | 23 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
49 | 67 | | |
50 | 68 | | |
51 | 69 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
61 | 66 | | |
62 | 67 | | |
63 | 68 | | |
| |||
880 | 885 | | |
881 | 886 | | |
882 | 887 | | |
883 | | - | |
884 | | - | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
885 | 894 | | |
886 | 895 | | |
887 | 896 | | |
888 | 897 | | |
889 | 898 | | |
890 | | - | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
891 | 903 | | |
892 | 904 | | |
893 | 905 | | |
| |||
901 | 913 | | |
902 | 914 | | |
903 | 915 | | |
904 | | - | |
| 916 | + | |
905 | 917 | | |
906 | 918 | | |
907 | 919 | | |
908 | 920 | | |
909 | 921 | | |
| 922 | + | |
910 | 923 | | |
911 | 924 | | |
912 | 925 | | |
| |||
921 | 934 | | |
922 | 935 | | |
923 | 936 | | |
| 937 | + | |
924 | 938 | | |
925 | 939 | | |
926 | 940 | | |
| |||
932 | 946 | | |
933 | 947 | | |
934 | 948 | | |
935 | | - | |
| 949 | + | |
936 | 950 | | |
937 | 951 | | |
938 | 952 | | |
| |||
959 | 973 | | |
960 | 974 | | |
961 | 975 | | |
| 976 | + | |
962 | 977 | | |
963 | 978 | | |
| 979 | + | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
964 | 988 | | |
965 | 989 | | |
966 | 990 | | |
| |||
999 | 1023 | | |
1000 | 1024 | | |
1001 | 1025 | | |
1002 | | - | |
| 1026 | + | |
| 1027 | + | |
1003 | 1028 | | |
1004 | 1029 | | |
1005 | 1030 | | |
1006 | | - | |
| 1031 | + | |
1007 | 1032 | | |
1008 | 1033 | | |
1009 | 1034 | | |
| |||
1028 | 1053 | | |
1029 | 1054 | | |
1030 | 1055 | | |
1031 | | - | |
| 1056 | + | |
| 1057 | + | |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + | |
1032 | 1065 | | |
| 1066 | + | |
1033 | 1067 | | |
1034 | 1068 | | |
1035 | 1069 | | |
| |||
1041 | 1075 | | |
1042 | 1076 | | |
1043 | 1077 | | |
| 1078 | + | |
| 1079 | + | |
| 1080 | + | |
1044 | 1081 | | |
1045 | | - | |
| 1082 | + | |
1046 | 1083 | | |
1047 | 1084 | | |
1048 | 1085 | | |
1049 | 1086 | | |
1050 | 1087 | | |
1051 | | - | |
1052 | | - | |
| 1088 | + | |
1053 | 1089 | | |
1054 | 1090 | | |
1055 | 1091 | | |
| |||
1065 | 1101 | | |
1066 | 1102 | | |
1067 | 1103 | | |
1068 | | - | |
1069 | | - | |
| 1104 | + | |
1070 | 1105 | | |
1071 | 1106 | | |
1072 | 1107 | | |
| |||
1076 | 1111 | | |
1077 | 1112 | | |
1078 | 1113 | | |
1079 | | - | |
1080 | | - | |
| 1114 | + | |
1081 | 1115 | | |
1082 | | - | |
1083 | | - | |
| 1116 | + | |
1084 | 1117 | | |
1085 | 1118 | | |
1086 | 1119 | | |
1087 | 1120 | | |
1088 | 1121 | | |
1089 | 1122 | | |
1090 | | - | |
| 1123 | + | |
1091 | 1124 | | |
1092 | 1125 | | |
1093 | 1126 | | |
| |||
1103 | 1136 | | |
1104 | 1137 | | |
1105 | 1138 | | |
| 1139 | + | |
| 1140 | + | |
| 1141 | + | |
| 1142 | + | |
1106 | 1143 | | |
1107 | 1144 | | |
1108 | 1145 | | |
1109 | 1146 | | |
1110 | 1147 | | |
1111 | 1148 | | |
1112 | 1149 | | |
| 1150 | + | |
| 1151 | + | |
| 1152 | + | |
| 1153 | + | |
1113 | 1154 | | |
1114 | 1155 | | |
1115 | 1156 | | |
1116 | 1157 | | |
1117 | | - | |
| 1158 | + | |
| 1159 | + | |
| 1160 | + | |
1118 | 1161 | | |
1119 | 1162 | | |
1120 | 1163 | | |
| |||
1128 | 1171 | | |
1129 | 1172 | | |
1130 | 1173 | | |
1131 | | - | |
| 1174 | + | |
1132 | 1175 | | |
1133 | 1176 | | |
1134 | 1177 | | |
1135 | 1178 | | |
1136 | 1179 | | |
| 1180 | + | |
1137 | 1181 | | |
1138 | 1182 | | |
1139 | 1183 | | |
| |||
1157 | 1201 | | |
1158 | 1202 | | |
1159 | 1203 | | |
1160 | | - | |
| 1204 | + | |
1161 | 1205 | | |
1162 | 1206 | | |
1163 | | - | |
| 1207 | + | |
1164 | 1208 | | |
1165 | 1209 | | |
1166 | 1210 | | |
| |||
1181 | 1225 | | |
1182 | 1226 | | |
1183 | 1227 | | |
1184 | | - | |
| 1228 | + | |
1185 | 1229 | | |
1186 | 1230 | | |
1187 | 1231 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
351 | 351 | | |
352 | 352 | | |
353 | 353 | | |
354 | | - | |
| 354 | + | |
| 355 | + | |
355 | 356 | | |
| 357 | + | |
356 | 358 | | |
357 | 359 | | |
358 | 360 | | |
| |||
366 | 368 | | |
367 | 369 | | |
368 | 370 | | |
| 371 | + | |
369 | 372 | | |
370 | 373 | | |
371 | 374 | | |
| |||
385 | 388 | | |
386 | 389 | | |
387 | 390 | | |
388 | | - | |
| 391 | + | |
389 | 392 | | |
390 | 393 | | |
391 | 394 | | |
392 | | - | |
| 395 | + | |
393 | 396 | | |
394 | 397 | | |
395 | 398 | | |
| |||
0 commit comments