Connection Reset异常

  • 2019 年 12 月 2 日
  • 笔记

最近调用其他服务的HTTP接口偶尔会出现java.net.SocketException: Connection reset异常信息。

异常信息

java.net.SocketException: Connection reset          at java.net.SocketInputStream.read(SocketInputStream.java:210)          at java.net.SocketInputStream.read(SocketInputStream.java:141)          at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)          at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)          at java.io.BufferedInputStream.read(BufferedInputStream.java:345

原因

  • 连接的对方发送了RST包(Reset),表示连接异常,需要立即释放
  • 往一个已经关闭的连接中写数据
  • 关闭一个接收buffer没有读取完毕的socker连接

项目中的原因是因为服务所在Docker内存不足,导致进程被kill。错误日志:Memory cgroup out of memory: Kill process 104609

RST包的场景

  • 发送请求到一个没有被监听的端口
  • 连接被异常终止(没有经过4次挥手)
  • 检测到半开连接