Commit a937b81
Flush pending writes before closing the connection on caught exceptions
Motivation:
When an exception is caught on a connection, VertxHandler#exceptionCaught
dispatches the throwable to the user handlers and then closes the channel
with chctx.close(). A response written from the handler -- for instance a
400 written from request().exceptionHandler() when HttpContentDecompressor
throws a DecoderException on a malformed compressed body -- is enqueued in
the outbound message queue but not flushed, because VertxConnection#write
skips the flush while a read is in progress. chctx.close() then causes Netty
to discard the unflushed entries from the ChannelOutboundBuffer, so the
response never reaches the client: the client observes a connection reset
instead of the intended HTTP response.
Changes:
Route the exception-driven close through the channel so it traverses the
pipeline tail and invokes VertxHandler#close -> VertxConnection#writeClose,
which flushes pending writes (an empty buffer with forceFlush=true) and
closes the channel only once the flush has completed. This is the same
flush-then-close path already used by graceful shutdown, idle close and the
regular close(); the exception path simply was not using it. The bare
chctx.close() is kept as a fallback for the case where no connection has been
set yet (handlerAdded has not run), since VertxHandler#close dereferences it.
Add a regression test, Http1xTest#testRequestDecompressionInvalidBodyDeliversErrorResponse,
that sends a malformed gzip body to a server with decompression enabled and
asserts the client receives the response written from the request exception
handler.
HTTP/2 is unaffected: its codec replaces VertxHandler in the pipeline, so
connection-level exceptions do not go through this path.
Signed-off-by: Alexandru Salajan <alexandru17@gmail.com>1 parent d4e4bfa commit a937b81
2 files changed
Lines changed: 53 additions & 1 deletion
File tree
- vertx-core/src
- main/java/io/vertx/core/net/impl
- test/java/io/vertx/tests/http
Lines changed: 9 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
151 | 151 | | |
152 | 152 | | |
153 | 153 | | |
154 | | - | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
155 | 163 | | |
156 | 164 | | |
157 | 165 | | |
| |||
Lines changed: 44 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2875 | 2875 | | |
2876 | 2876 | | |
2877 | 2877 | | |
| 2878 | + | |
| 2879 | + | |
| 2880 | + | |
| 2881 | + | |
| 2882 | + | |
| 2883 | + | |
| 2884 | + | |
| 2885 | + | |
| 2886 | + | |
| 2887 | + | |
| 2888 | + | |
| 2889 | + | |
| 2890 | + | |
| 2891 | + | |
| 2892 | + | |
| 2893 | + | |
| 2894 | + | |
| 2895 | + | |
| 2896 | + | |
| 2897 | + | |
| 2898 | + | |
| 2899 | + | |
| 2900 | + | |
| 2901 | + | |
| 2902 | + | |
| 2903 | + | |
| 2904 | + | |
| 2905 | + | |
| 2906 | + | |
| 2907 | + | |
| 2908 | + | |
| 2909 | + | |
| 2910 | + | |
| 2911 | + | |
| 2912 | + | |
| 2913 | + | |
| 2914 | + | |
| 2915 | + | |
| 2916 | + | |
| 2917 | + | |
| 2918 | + | |
| 2919 | + | |
| 2920 | + | |
| 2921 | + | |
2878 | 2922 | | |
2879 | 2923 | | |
2880 | 2924 | | |
| |||
0 commit comments