表现
Spring Boot版本:2.4.1
条件:服务端由Tomcat提供,并开启HTTP2的服务,通过Multipart上传文件
上传过程中出现报错:
Stream read timeout
解决
通过关闭HTTP2功能解决
原因
这里从StackOverflow上找到了一个回复:
Thanks everyone's reply. I think this is the bug of SpringBoot or its embed Tomcat bug in SpringBoot v2.1.x version. When SpringBoot v2.3.0 formal vesion released. I use the same code to upgrade to v2.3.0 Everything is workable now. I use bulk test or called pressure test in file upload. It's 100% successful now.
Update: The root cause is Tomcat component is not stable with Spring framework - Multipart under the https and/or http2 or Spring security (such like OAuth2) enabled.
In most cases in Spring Boot 2.3.0 - 2.3.2, the fail rate of file upload is 4 ~ 14 ~ 50%. After Spring Boot 2.3.5 to 2.4.0 version, the fail rate of file upload is near 50%. The key different is the Tomcat version and Spring Security version is different among these Spring Boot version.
Another problem is the http2 protocol supported by Tomcat is not stable. If you enable the http2 (such like setup the server.http2.enabled=true in your properties file) and use Multipart or HttpServletRequest for your file upload, the fail rate is rise to near 90% and get connection .
Conclusion: For anyone which face the file upload unstable problem and get ERR_CONNECTION_CLOSED and find error log about
org.apache.catalina.connector.ClientAbortException: org.apache.coyote.CloseNowException: Connection [{0}], Stream [{1}], This stream is not writable
Caused by: org.apache.coyote.CloseNowException: Connection [{0}], Stream [{1}], This stream is not writable
Caused by: org.apache.coyote.http2.StreamException: Connection [{0}], Stream [{1}], This stream is not writable
You can try to upgrade to Spring Boot 2.4.0 and disable http2 support of Tomcat in Spring Boot by setup the server.http2.enabled=false in your properties file
You may find the stability is improved even to 100% success rate. (I tried and implemented to all my clients)
If the disable http2 support is not workable for you. You may try to downgrade to Spring Boot 2.3.0 - 2.3.2 version or implement your file upload by HttpServletRequest (remember to disable the Multipart in Spring Boot first)
You may find related developing document here and there
说是Spring Boot的问题,我们之后再讨论。
