Repository navigation
Commit 18f412e
netty: drain before closing a gracefully shut down server connection (#13086)
Fixes #9566.
After the second GOAWAY of a graceful shutdown (`maxConnectionAge`,
`maxConnectionIdle`, `Server.shutdown()`), `Http2ConnectionHandler`
closes the socket as soon as the last stream's final frame has been
written to the kernel. If the client hasn't read the response yet, the
response is still in the server's send queue. The client's next frame (a
BDP PING or WINDOW_UPDATE) then reaches a socket the server has closed,
so the server's kernel answers with RST and drops the unsent bytes,
trailers included. The client fails an RPC the server completed, with
`UNAVAILABLE: Connection closed after GOAWAY. HTTP/2 error code:
NO_ERROR, debug data: max_age`. The response headers had already
arrived, so the call is committed and isn't retried. The full analysis
and a standalone reproducer are in
#9566 (comment).
### Change
Once the second GOAWAY has been sent and no streams are active,
`NettyServerHandler` keeps the connection open and reading for up to 1
second before closing. The client normally closes first, as soon as it
has read the GOAWAY and its streams have completed, so the drain usually
ends early. This is the approach Go's HTTP/2 server took
(golang/net@cd69bc3).
- It overrides `isGracefulShutdownComplete()`, the hook
`NettyClientHandler` already uses, so the close still goes through
`Http2ConnectionHandler` and works the same with TLS.
- It never extends the grace time: if `maxConnectionAgeGrace` (or the
shutdown grace) runs out first, the connection closes then.
- The drain is cancelled when the channel becomes inactive.
### Tests
- `NettyServerHandlerTest`: 9 tests asserted that the channel closes
immediately after the second GOAWAY. They now advance the fake clock by
the drain time first. Two new tests:
`maxConnectionAge_drainStartsWhenLastStreamCloses` and
`maxConnectionAgeGrace_shorterThanDrain_closesAtGrace`.
- New `NettyServerGracefulCloseTest`: an end-to-end regression test over
a real loopback connection, using only public APIs and the server's
default transport. A unary call outlives the max age, and the client's
event loop stalls 300 ms while the 256 KiB response is sent. Without the
fix it fails on Linux with the error above (3/3 runs); with the fix it
passes (3/3, about 6 s). macOS loopback doesn't lose the data, so there
it passes either way. Note that `maxConnectionAge` has a 1 s floor,
which is why the call runs 1.5 s.
- All 655 `grpc-netty` tests pass.
The standalone reproducer from the issue, on Linux (`master` against
this branch, 20 iterations each, `parkMs` = client event-loop stall):
| Scenario | `master` | this PR |
|---|---|---|
| No hooks | 5/20 fail | 0/20 |
| 100 / 500 / 900 ms client stall | 49/60 fail | 0/60 |
| 1500 ms client stall (longer than the drain) | 1/20 | 3/20 |
A client stalled for longer than the drain can still lose the race, as
in Go.
### Relation to Netty
The root cause is in Netty's `Http2ConnectionHandler`, so I've also
opened netty/netty#17701 (issue netty/netty#17700). It adds an opt-in
`gracefulShutdownDrainMillis`. Once a Netty release includes it, the
override here can be replaced by calling
`gracefulShutdownDrainMillis(1000)`. This PR fixes grpc-java without
waiting for that release.
### Not included
`OkHttpServerTransport` closes the same way after the second GOAWAY and
is probably affected too. I haven't tested it, and I'm happy to follow
up if you want both transports to behave the same.
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>1 parent fe4f145 commit 18f412e
3 files changed
Lines changed: 271 additions & 8 deletions
File tree
- netty/src
- main/java/io/grpc/netty
- test/java/io/grpc/netty
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
122 | 122 | | |
123 | 123 | | |
124 | 124 | | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
125 | 133 | | |
126 | 134 | | |
127 | 135 | | |
| |||
369 | 377 | | |
370 | 378 | | |
371 | 379 | | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
372 | 383 | | |
373 | 384 | | |
374 | 385 | | |
| |||
694 | 705 | | |
695 | 706 | | |
696 | 707 | | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
697 | 711 | | |
698 | 712 | | |
699 | 713 | | |
| |||
745 | 759 | | |
746 | 760 | | |
747 | 761 | | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
748 | 768 | | |
749 | 769 | | |
750 | 770 | | |
| |||
1090 | 1110 | | |
1091 | 1111 | | |
1092 | 1112 | | |
| 1113 | + | |
| 1114 | + | |
| 1115 | + | |
| 1116 | + | |
| 1117 | + | |
| 1118 | + | |
| 1119 | + | |
1093 | 1120 | | |
1094 | 1121 | | |
1095 | 1122 | | |
| |||
1100 | 1127 | | |
1101 | 1128 | | |
1102 | 1129 | | |
| 1130 | + | |
1103 | 1131 | | |
1104 | 1132 | | |
1105 | 1133 | | |
| |||
1142 | 1170 | | |
1143 | 1171 | | |
1144 | 1172 | | |
| 1173 | + | |
| 1174 | + | |
1145 | 1175 | | |
1146 | 1176 | | |
1147 | 1177 | | |
1148 | 1178 | | |
1149 | 1179 | | |
1150 | 1180 | | |
| 1181 | + | |
| 1182 | + | |
| 1183 | + | |
| 1184 | + | |
| 1185 | + | |
| 1186 | + | |
| 1187 | + | |
| 1188 | + | |
| 1189 | + | |
| 1190 | + | |
| 1191 | + | |
| 1192 | + | |
| 1193 | + | |
| 1194 | + | |
| 1195 | + | |
| 1196 | + | |
| 1197 | + | |
| 1198 | + | |
| 1199 | + | |
| 1200 | + | |
| 1201 | + | |
| 1202 | + | |
| 1203 | + | |
| 1204 | + | |
| 1205 | + | |
| 1206 | + | |
| 1207 | + | |
| 1208 | + | |
| 1209 | + | |
| 1210 | + | |
| 1211 | + | |
| 1212 | + | |
1151 | 1213 | | |
1152 | 1214 | | |
1153 | 1215 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
367 | 367 | | |
368 | 368 | | |
369 | 369 | | |
370 | | - | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
371 | 373 | | |
372 | 374 | | |
373 | 375 | | |
| |||
389 | 391 | | |
390 | 392 | | |
391 | 393 | | |
392 | | - | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
393 | 397 | | |
394 | 398 | | |
395 | 399 | | |
| |||
418 | 422 | | |
419 | 423 | | |
420 | 424 | | |
| 425 | + | |
| 426 | + | |
421 | 427 | | |
422 | 428 | | |
423 | 429 | | |
| |||
1058 | 1064 | | |
1059 | 1065 | | |
1060 | 1066 | | |
1061 | | - | |
| 1067 | + | |
| 1068 | + | |
| 1069 | + | |
1062 | 1070 | | |
1063 | 1071 | | |
1064 | 1072 | | |
| |||
1088 | 1096 | | |
1089 | 1097 | | |
1090 | 1098 | | |
1091 | | - | |
| 1099 | + | |
| 1100 | + | |
| 1101 | + | |
1092 | 1102 | | |
1093 | 1103 | | |
1094 | 1104 | | |
| |||
1132 | 1142 | | |
1133 | 1143 | | |
1134 | 1144 | | |
1135 | | - | |
| 1145 | + | |
| 1146 | + | |
| 1147 | + | |
1136 | 1148 | | |
1137 | 1149 | | |
1138 | 1150 | | |
| |||
1176 | 1188 | | |
1177 | 1189 | | |
1178 | 1190 | | |
1179 | | - | |
| 1191 | + | |
| 1192 | + | |
| 1193 | + | |
1180 | 1194 | | |
1181 | 1195 | | |
1182 | 1196 | | |
| |||
1230 | 1244 | | |
1231 | 1245 | | |
1232 | 1246 | | |
1233 | | - | |
| 1247 | + | |
| 1248 | + | |
| 1249 | + | |
1234 | 1250 | | |
1235 | 1251 | | |
1236 | 1252 | | |
| |||
1261 | 1277 | | |
1262 | 1278 | | |
1263 | 1279 | | |
1264 | | - | |
| 1280 | + | |
| 1281 | + | |
| 1282 | + | |
| 1283 | + | |
| 1284 | + | |
| 1285 | + | |
| 1286 | + | |
| 1287 | + | |
| 1288 | + | |
| 1289 | + | |
| 1290 | + | |
| 1291 | + | |
| 1292 | + | |
| 1293 | + | |
| 1294 | + | |
| 1295 | + | |
| 1296 | + | |
| 1297 | + | |
| 1298 | + | |
| 1299 | + | |
| 1300 | + | |
| 1301 | + | |
| 1302 | + | |
| 1303 | + | |
| 1304 | + | |
| 1305 | + | |
| 1306 | + | |
| 1307 | + | |
| 1308 | + | |
| 1309 | + | |
| 1310 | + | |
| 1311 | + | |
| 1312 | + | |
| 1313 | + | |
| 1314 | + | |
| 1315 | + | |
| 1316 | + | |
| 1317 | + | |
| 1318 | + | |
| 1319 | + | |
| 1320 | + | |
| 1321 | + | |
| 1322 | + | |
| 1323 | + | |
| 1324 | + | |
| 1325 | + | |
| 1326 | + | |
| 1327 | + | |
1265 | 1328 | | |
1266 | 1329 | | |
1267 | 1330 | | |
| |||
0 commit comments